What can we help you with?
How to exclude Products from rules using Dynamic Pricing and Discounts Plugin for WooCommerce? (Code Snippet)
There may be instances where you configure some rules for dynamic pricing or discounts for products on your store, and would like to exempt some products from these rules. You can achieve this in ELEX Dynamic Pricing and Discounts Plugin for WooCommerce with the following code snippets for different cases.
Case 1: Skipping products from specific rule type
With Dynamic Pricing and Discounts Plugin for WooCommerce, you can create product rules, combinational rules, category rules, cart rules, and buy & get offers. You can skip certain product(s) from these rule types by specifying product ID(s) and rule type in the code snippet.
Consider the following category rule configured in the plugin:
Based on the above category rule, if a minimum of two items of category Clothing are bought, then a percentage discount of value 10 is applied. The discounted rate on the products can be seen on the cart page as shown below:
To skip a product from category rule, say Hoodie (product ID: 2500), include the following code snippet in the functions.php file of your respective website theme folder.
add_filter('eha_dp_skip_product','skip_product_from_discount',1,4); function skip_product_from_discount($return_val,$pid,$rule,$mode) { $pid_to_skip=array(2500); // specify pids to skip if($mode=='category_rules' && in_array($pid,$pid_to_skip)) { return true; // true to skip this product } return $return_val; }
Once the code snippet is added, you can see the applicable changes on the cart page as shown in the screenshot below:
Case 2: Skipping products based on rule number of specific rule type
You can skip products based on rule number of specific rule type by specifying rule number, product ID and respective rule type.
Consider the category rule configured in the plugin, as explained in the earlier part of this article. The discounted rate on the products can be seen on the cart page as shown below:
Category rule applied on Cart page
To skip a product based on rule number from category rule, say rule number 1 of category Clothing and product Hoodie (product ID: 2500), include the following code snippet in the functions.php file of your respective website theme folder.
add_filter('eha_dp_skip_product','skip_product_from_discount',1,4); function skip_product_from_discount($return_val,$pid,$rule,$mode) { $from_rule_no=1; $pid_to_skip=array(2500); // specify pids to skip if($mode=='category_rules' && in_array($pid,$pid_to_skip) && $rule['rule_no']==$from_rule_no) { return true; // true to skip this product } return $return_val; }
Once the code snippet is added, you can see the applicable changes on the cart page as shown in the screenshot below:
To know more about the product, check out ELEX Dynamic Pricing and Discounts Plugin for WooCommerce.
To know more about other features of the plugin, read product setting up article.
Or checkout documentation section for more related articles.
3 Comments. Leave new
I would suggest this as a feature in plugin. Not via code snippet. So, It would be easier to use for shop manager. Thanks.
Thank you for the feedback! We will pass this feedback to the product team.
Hi, 3 years pass 😀
Not sure is there any plan to release this feature into nice UI rather than code snippet?