Site logo

Archived topic

Use decimal in quantity fields in WooCommerce

10 replies · Started by Sonia on May 24, 2019

Viewing posts 1–11 of 11

Hello !
I would like to put decimals in my woocommerce product quantities. I have a php code but there are some bug.
I would like quantities ranging from 0.10 -> 0.20 -> 0.30> 0.40 ..... it is a textile shop and I sell fabric 10 cm by 10 cm.
The code causes errors: example: it writes quantities like 0.1000 or it goes from 0.1 to 0.1856 for example
Are there other solutions? Can you help me?

here is the php code in function.php :

// Add min value to the quantity field (default = 1)
add_filter('woocommerce_quantity_input_min', 'min_decimal');
function min_decimal($val) {
    return 0.1;
}
 
// Add step value to the quantity field (default = 1)
add_filter('woocommerce_quantity_input_step', 'nsk_allow_decimal');
function nsk_allow_decimal($val) {
    return 0.1;
}
 
// 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', 'unit_price_fix', 10, 5);
function 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;
}

Hi there,

That code looks like it should work. Does it work if you turn off the quantity buttons in Customize > Layout > WooCommerce?

Hello
excuse me for my translation, I'm french

Thank you very much, yes it works! no more bug.

Can we assign this function.php to a product category?
Finally can we give a minimum quantity?

thank you so much

Hi there,

Not too sure what you mean - do you only want to apply your functions above to a specific product category?

You've set the minimum quantity in the min_decimal() function.

excuse me for the translation

thank you for the solution, it's OK ->"That code looks like it should work. Does it work if you turn off the quantity buttons in Customize > Layout > WooCommerce?"

I would like to have decimals just for only product category. it's possible?

thank you so much

My english it's horrible... sorry :(

I would like this function only One product category.

thank you !!!

Hmm, I'm not sure if that's do-able, as the quantity can be changed in multiple places (including the cart). You might need to check with WooCommerce whether it's possible.

If there's a conditional we can use (is_product_category()) that will work everywhere (the cart too), then I should be able to help.

OK, thanks
my knowledge of php is limited
where should I put this condition?
thank you

I'm not sure that condition will actually work for what you're trying to do. That's why I mentioned you might want to contact WooCommerce support to see if they have any better ideas.

Hello Tom,
Thank you very much

No problem!

This archived topic is closed to new replies.