Decimal Values in the Product Quantity Field

Decimal Values in Product Quantity Field

In some industries products are often sold in units less than 1, an example of this would selling groceries online. Many of the fruit and vegetable items are purchased by users in quantities less than 1kg in store.

This pricing can be reflected online, so the customer knows that they purchasing the same product online as they would in store. With each unit of the product representing a kilogram in weight. We have previously discussed how to add a unit abbreviation to the product pricing on your WooCommerce store.

If we are selling in 1kg units, how does a customer purchase less than 1kg of a product. As I am sure not all customers want a full kilogram of every product.

Using Decimal Values in the Product Quantity Field

To resolve this problem, we can change the incremental unit steps for the products add to cart buttons. So instead of operating in full units ie 1,2,3 … and so on. We can change the incremental steps within the product quantity field to a decimal value like 0.5 or 0.2.

This allows your customers to order to their discretion in values less than a full unit.

Adding Decimal Values to the Product Quantity Field

To do this, add the following code to your theme’s function file or add it as a snippet using the snippet plugin.

<?php // Do no copy this line

// Add min value to the quantity field (default = 1)
add_filter('woocommerce_quantity_input_min', 'min_decimal');
function min_decimal($val) { 
    return 0.5;  //Change value to the required incremental unit step. 
}
 
// Add step value to the quantity field (default = 1)
add_filter('woocommerce_quantity_input_step', 'dp_allow_decimal');
function dp_allow_decimal($val) {
    return 0.5; //Change value to the required incremental unit step.
}
 
// Removes the WooCommerce filter, that is validating the quantity to be an int
remove_filter('woocommerce_stock_amount', 'intval');
 
// Add a filter, that validates the quantity to be a float
add_filter('woocommerce_stock_amount', 'floatval');
 
// Add unit price fix when showing the unit price on processed orders
add_filter('woocommerce_order_amount_item_total', 'dp_unit_price_fix', 10, 5);
function dp_unit_price_fix($price, $order, $item, $inc_tax = false, $round = true) {
	
    $qty = (!empty($item['qty']) && $item['qty'] != 0) ? $item['qty'] : 1;
    if($inc_tax) {
        $price = ($item['line_total'] + $item['line_tax']) / $qty;
    } else {
        $price = $item['line_total'] / $qty;
    }
    $price = $round ? round( $price, 2 ) : $price;
    return $price;
}

The above snippet changes the incremental unit step to 0.5, to change this value to another integer replace the “0.5” on the sixth and twelve line to the desired value.

This code can also be used if you wish to change the incremental value to a number large than 1 unit, ie 2 or 3.

If you have any questions, please use the comments form below.

Nicholas Duell Avatar

2 responses to “Decimal Values in the Product Quantity Field”

  1. ataraxie Avatar
    ataraxie

    Hi and thanks for your help.

    How can I apply those filters only on a specific variation of a product ?

    Thanks per advance,

    ataraxie

    1. Nicholas Duell Avatar

      Hi ataraxie,

      Unfortunately, one is not able to apply the filter to a specific variation of a product? This is because the input field step values are controlled on a page level.

      The variable products will need to be split into separate products to achieve what you are after.

      Cheers,

      Nick

Leave a Reply

Your email address will not be published. Required fields are marked *