How to use decimal in quantity fields in WooCommerce for certain categories?

admin2025-01-07  4

I found the code what I want to use on my woocommerce shop, but I want to use it only for certain categories from my shop. Can someone explain how I can do it?

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

Thanks in advance!

I found the code what I want to use on my woocommerce shop, but I want to use it only for certain categories from my shop. Can someone explain how I can do it?

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

Thanks in advance!

Share Improve this question asked Aug 5, 2020 at 9:42 D. F.D. F. 1 2
  • anyone can help me here? – D. F. Commented Aug 6, 2020 at 7:24
  • 1 Try using is_product_category('foo'). See this stackoverflow.com/questions/49501582/… – aravind Commented Aug 14, 2020 at 15:16
Add a comment  | 

1 Answer 1

Reset to default 0

check this sample code :

add_filter('woocommerce_quantity_input_min', 'min_decimal', 10, 2);
function min_decimal($val, WC_Product $product) {
if(get_the_category($product->id) == 12) { // 12 = ID on the category you want it to work with
return 0.1;
}
return 1;
}
add_filter('woocommerce_quantity_input_step', 'step_decimal', 10, 2);
function step_decimal($val, WC_Product $product) {
if(get_the_category($product->id) == 12) { // 12 = ID on the category you want it to work with
return 0.1;
}

return 1;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736261678a760.html

最新回复(0)