- This topic has 10 replies, 2 voices, and was last updated 4 years, 9 months ago by Tom.
-
AuthorPosts
-
May 24, 2019 at 4:23 am #909523Sonia
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; }
May 24, 2019 at 11:15 am #909938TomLead DeveloperLead DeveloperHi there,
That code looks like it should work. Does it work if you turn off the quantity buttons in Customize > Layout > WooCommerce?
May 28, 2019 at 1:22 am #912916SoniaHello
excuse me for my translation, I’m frenchThank 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
May 28, 2019 at 8:26 am #913455TomLead DeveloperLead DeveloperHi 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.May 28, 2019 at 10:47 am #913594Soniaexcuse 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
May 28, 2019 at 10:50 am #913599SoniaMy english it’s horrible… sorry 🙁
I would like this function only One product category.
thank you !!!
May 28, 2019 at 2:54 pm #913755TomLead DeveloperLead DeveloperHmm, 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.May 29, 2019 at 1:25 am #914025SoniaOK, thanks
my knowledge of php is limited
where should I put this condition?
thank youMay 29, 2019 at 8:23 am #914496TomLead DeveloperLead DeveloperI’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.
June 4, 2019 at 11:15 am #919903SoniaHello Tom,
Thank you very muchJune 4, 2019 at 4:03 pm #920089TomLead DeveloperLead DeveloperNo problem!
-
AuthorPosts
- You must be logged in to reply to this topic.