Tuesday, April 22, 2025
HomePHPWooCommerce tip: How you can handle reductions primarily based on taxonomies

WooCommerce tip: How you can handle reductions primarily based on taxonomies


By default, WooCommerce permits us to exclude merchandise that belong to particular classes from low cost gives or to use reductions solely to merchandise that belong to specific classes.

The default WooCommerce discount rules for product categoriesThe default WooCommerce discount rules for product categoriesThe default WooCommerce discount rules for product categories

Nonetheless, it doesn’t present a simple mechanism for outlining low cost guidelines for different product taxonomies (e.g. manufacturers and tags).

WooCommerce tag and brand taxonomiesWooCommerce tag and brand taxonomiesWooCommerce tag and brand taxonomies

In fact, we will use a plugin to implement that function, but it surely isn’t that tough to implement ourselves!

All we now have to do is make the most of the woocommerce_coupon_is_valid_for_product filter. If you wish to dig deeper into it, you’ll discover it contained in the class-wc-coupon.php file of the WooCommerce plugin information.

Let’s get a greater understanding via some examples!

1. Set a reduction just for merchandise (easy or variable) with out the Inventory Gives tag.

To allow this rule, add the next code within the features.php file of your theme:

1
<?php
2
operate wc_custom_coupon_rules( $legitimate, $product, $coupon, $values ) {
3
    $product_id = $product->get_ID();
4
	if ( 'variation' === $product->get_type() ) :
5
		$product_id = $product->get_parent_id();
6
	endif;
7
	if ( has_term( 'stock-offers', 'product_tag', $product_id ) ) :
8
		$legitimate = false;
9
	endif;
10
	return $legitimate;
11
}
12
add_filter( 'woocommerce_coupon_is_valid_for_product', 'wc_custom_coupon_rules', 10, 4 );

With this rule in place, assuming we now have obtainable a welcome10 coupon for a ten% low cost, a buyer with these alternatives will get pleasure from a 2.50€ low cost.

An example of a discount ruleAn example of a discount ruleAn example of a discount rule

2. Set a reduction just for merchandise (easy or variable) with the Inventory Gives tag.

To allow this rule, add the next code within the features.php file of your theme:

1
<?php
2
operate wc_custom_coupon_rules( $legitimate, $product, $coupon, $values ) {
3
    $product_id = $product->get_ID();
4
	if ( 'variation' === $product->get_type() ) :
5
		$product_id = $product->get_parent_id();
6
	endif;
7
	if ( ! has_term( 'stock-offers', 'product_tag', $product_id ) ) :
8
		$legitimate = false;
9
	endif;
10
	return $legitimate;
11
}
12
add_filter( 'woocommerce_coupon_is_valid_for_product', 'wc_custom_coupon_rules', 10, 4 );

With this rule in place, assuming we now have obtainable a welcome10 coupon for a ten% low cost, a buyer with these alternatives will get pleasure from a 3.60€ low cost.

An example of a discount ruleAn example of a discount ruleAn example of a discount rule

3. Set a reduction just for merchandise (easy or variable) with out the Inventory Gives tag or the Nike model.

To allow this rule, add the next code within the features.php file of your theme:

1
<?php
2
operate wc_custom_coupon_rules( $legitimate, $product, $coupon, $values ) 
12
add_filter( 'woocommerce_coupon_is_valid_for_product', 'wc_custom_coupon_rules', 10, 4 );

With this rule in place, assuming we now have obtainable a welcome10 coupon for a ten% low cost, a buyer with these alternatives will get pleasure from a 2.50€ low cost.

An example of a discount ruleAn example of a discount ruleAn example of a discount rule

4. Set a reduction just for variable merchandise with out the Massive measurement attribute.

To allow this rule, add the next code within the features.php file of your theme:

1
<?php
2
operate wc_custom_coupon_rules( $legitimate, $product, $coupon, $values ) {
3
    if ( 'variation' === $product->get_type() && 'Massive' === $product->get_attribute( 'pa_size' ) ) :
4
		$legitimate = false;
5
	endif;
6
	return $legitimate;
7
}
8
add_filter( 'woocommerce_coupon_is_valid_for_product', 'wc_custom_coupon_rules', 10, 4 );

With this rule in place, assuming we now have obtainable a welcome10 coupon for a ten% low cost, a buyer with these alternatives will get pleasure from a 5€ low cost.

An example of a discount ruleAn example of a discount ruleAn example of a discount rule

Conclusion

As you’ll be able to see, with only a small quantity of code, we set customized low cost guidelines for various WooCommerce taxonomies.

You possibly can construct on the offered code and customise it based on your wants. For instance, you’ll be able to restrict a few of these situations solely to sure coupons or make the filter choices dynamic by including new fields within the admin. Nothing stops you from combining your personal situations with those that WooCommerce supplies by default.

As all the time, thanks rather a lot for studying!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments