简体   繁体   中英

Add a WooCommerce function for a particular category

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.

is_product_category function is working only on archive pages. In order to achieve what you are after, you can add your filters in the archive page wrapped by your if statement.

You can use the code below in your archive woocommerce page:

if(is_product_category('apple-phones')) {
 add_filter('woocommerce_quantity_input_min', 'min_decimal');
 add_filter('woocommerce_quantity_input_step', 'nsk_allow_decimal');
 // 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);
}

And the functions.php:

function min_decimal($val) {
    return 0.5;
}
// Add step value to the quantity field (default = 1)
function nsk_allow_decimal($val) {
    return 0.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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM