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:

Dynamic Pricing and Discounts - Category Rule
Category Rule

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:

Dynamic Pricing and Discounts - Prices in Cart
Category rule applied on Cart page

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:

Dynamic Pricing and Discounts - Applied Category rule
Product skipped from Category rule

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:

Dynamic Pricing and Discounts - Prices in Cart

Category rule applied on Cart page

To skip a product based on rule number from category rule, say rule number 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:

Dynamic Pricing and Discounts - Prices in Cart
Product skipped from rule 1 of Category rule(s)

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.

Previous How to alter price HTML for Variable products using Dynamic Pricing and Discounts Plugin for WooCommerce? (Code Snippet)
Next How to hide striked out price on Product and Shop page in Dynamic Pricing and Discounts Plugin for WooCommerce? (Code Snippet)

3 Comments. Leave new

You must be logged in to post a comment.