Skip to content
Advertisement

Avoid cart items price update for specific product categories in Woocommerce

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?

JavaScript

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.

Advertisement

Answer

Your code has some errors and mistakes… As $valueArray is not defined in your code the first foreach loop doesn’t have any effect and code become active after it. To target cart items product categories, you can use WordPress conditional function has_term().

Try the following instead (for Woocommerce 3+):

JavaScript

Code goes in function.php file of your active child theme (or active theme). It should works.


But if you want to target parent product categories you will need to use has_product_categories() custom conditional function instead in your code:

JavaScript

Code goes in function.php file of your active child theme (or active theme). It should works.


Related threads:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement