I am using below script under my WordPress child theme functions.php to overwrite price, but below code is affecting to all the products.
Can you please help me "not to run" below code if the product is under some specific "shirts", "games" category?
function calculate_cart_total( $cart_object ) {
foreach ( WC()->cart->get_cart() as $key => $value ) {
if(is_array($valueArray)) {
foreach ( $valueArray as $k => $value ) {
if($value['wccpf_cost']) {
$additionalPrice = $value['wccpf_cost']['user_val'];
}
}
}
}
$additionalPrice = $value['wccpf_cost']['user_val'];
foreach ( WC()->cart->get_cart() as $key => $value ) {
if( method_exists( $value['data'], "set_price" ) ) {
$value['data']->set_price( $additionalPrice );
} else {
$value['data']->price = ($additionalPrice );
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_cart_total', 99 );
Above code is working smoothly for all the products but I don't want to run this code on all products as this code is required
I have tried conditional tags like is_product_category( array( 'shirts', 'games' ) )
, but it's not working.
Or is there any specific way in WordPress instead of functions.php
I can run above code to the specific products or category only where it's required?
I have also done some google search but not able to find perfect solution.