Woocommerce decimal quantity

一笑奈何 提交于 2020-05-12 02:51:45

问题


can you help me to find a way for use decimal value like 3.56 for quantity in woocommerce ?

in calculate price quantity number convert to Integers value but i need price calculate with Values that the user has inputed

i am working with woocommerce 2.2.8


回答1:


You have to add some functions, for some of the hooks i WooCommerce.

// 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');

I have written a tutorial, for this functionality, with some extra fixes an explanation at: http://codeontrack.com/use-decimal-in-quantity-fields-in-woocommerce-wordpress/




回答2:


I've done a bit of modification to an existing plugin to allow decimal quantities. It's called Quantities and Units for WooCommerce and can be found here:

https://wordpress.org/plugins/quantities-and-units-for-woocommerce/




回答3:


I have try this solution but the quantity input min hook don't work for me

my code :

// Add min value to the quantity field (default = 1)
add_filter('woocommerce_quantity_input_min', 'min_decimal');
function min_decimal($val) {
    return 0.75;
}

// 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.75;
}

// 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');

Min quantity stay 1

I can increment by 0.75



来源:https://stackoverflow.com/questions/27417718/woocommerce-decimal-quantity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!