Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Enable Decimal Quantities and Stock for WooCommerce Products?

By default, WooCommerce supports product quantities only in whole numbers. However, some businesses also need to sell their products in decimal quantities. Let’s say an online store selling groceries might need to sell bulk food items such as rice flour, vegetables, fruits, etc, in decimal quantities. In such cases, if you showcase the product quantities in decimals, customers can buy the quantities they need rather than in fixed packaging.

Solution: Enable Decimal Quantities and Stock for WooCommerce Products

This customization allows store owners to enable decimal quantities for specific product categories, and restocking is also operated the same way in decimal quantities. Make a note that you need to find the right category ID and define the category IDs as specified in the code.

// Custom conditional function (check for product categories)
function ts_enabled_decimal_quantities( $product ) {
    $targeted_terms = array(25, 79); // Here define your product category terms (names, slugs, or IDs)

    return has_term( $targeted_terms, 'product_cat', $product->get_id() );
}

// Defined quantity arguments 
add_filter( 'woocommerce_quantity_input_args', 'ts_custom_quantity_input_args', 9000, 2 );
function ts_custom_quantity_input_args( $args, $product ) {
    if( ts_enabled_decimal_quantities( $product ) ) {
        if( ! is_cart() ) {
            $args['input_value'] = 0.5; // Starting value
        }
        $args['min_value']   = 0.5; // Minimum value
        $args['step']        = 0.5; // Quantity steps
    }
    return $args;
}

// For Ajax add to cart button (define the min value)
add_filter( 'woocommerce_loop_add_to_cart_args', 'ts_custom_loop_add_to_cart_quantity_arg', 10, 2 );
function ts_custom_loop_add_to_cart_quantity_arg( $args, $product ) {
    if( ts_enabled_decimal_quantities( $product ) ) {
        $args['quantity'] = 0.5; // Min value
    }
    return $args;
}

// For product variations (define the min value)
add_filter( 'woocommerce_available_variation', 'ts_filter_wc_available_variation_price_html', 10, 3);
function ts_filter_wc_available_variation_price_html( $data, $product, $variation ) {
    if( ts_enabled_decimal_quantities( $product ) ) {
        $data['min_qty'] = 0.5;
    }
    return $data;
}

// Enable decimal quantities for stock (in frontend and backend)
remove_filter('woocommerce_stock_amount', 'intval');
add_filter('woocommerce_stock_amount', 'floatval');

Output

Let’s consider a grocery store selling fresh produce like fruits and vegetables. These products are categorized under ‘Food’, which has the category ID: 79. When a customer visits the product page of these particular categories mentioned in the code, they will be shown the decimal values of the product quantities.

How to Enable Decimal Quantities and Stock for WooCommerce Products? - Tyche Softwares

Making sure customers can buy products in the right quantities is important, especially for items sold by weight, like fruits or vegetables. With this simple tweak, store owners can allow decimal quantities and keep stock accurate. Likewise, you can also restrict the quantity field to selected numbers in WooCommerce. Small adjustments to quantity fields like these can make shopping easier and more flexible for customers.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x