I am using the below code in my theme's functions.php file to increase the quantity of a WooCommerce product by 0.5 instead of 1.
add_filter('woocommerce_quantity_input_min', 'min_decimal');
function min_decimal($val) {
return 0.5;
}
// 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.5;
}
// 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;
}
But I want the code to affect only a particular product category, not one.
So, I grabbed the above code inside this code:
if( is_product_category( 'apple-phones' ) ) {
//Above code here
}
But it is not working at all. But if I add only the first code, then it is working fine for all the products.
Can anyone please help me with what I am doing wrong?
I want the above code to work only for one of my product categories.
Thank you for your answer.