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



Nevertheless, it doesn’t present an easy mechanism for outlining low cost guidelines for different product taxonomies (e.g. manufacturers and tags).



In fact, we will use a plugin to implement that characteristic, but it surely isn’t that troublesome to implement ourselves!
All we have now to do is reap the benefits 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 by means of some examples!
1. Set a reduction just for merchandise (easy or variable) with out the Inventory Provides tag.
To allow this rule, add the next code within the features.php
file of your theme:
1 |
|
2 |
perform 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 have now out there a welcome10 coupon for a ten% low cost, a buyer with these alternatives will get pleasure from a 2.50€ low cost.



2. Set a reduction just for merchandise (easy or variable) with the Inventory Provides tag.
To allow this rule, add the next code within the features.php
file of your theme:
1 |
|
2 |
perform 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 ) |
12 |
add_filter( 'woocommerce_coupon_is_valid_for_product', 'wc_custom_coupon_rules', 10, 4 ); |
With this rule in place, assuming we have now out there a welcome10 coupon for a ten% low cost, a buyer with these alternatives will get pleasure from a 3.60€ low cost.



3. Set a reduction just for merchandise (easy or variable) with out the Inventory Provides tag or the Nike model.
To allow this rule, add the next code within the features.php
file of your theme:
1 |
|
2 |
perform wc_custom_coupon_rules( $legitimate, $product, $coupon, $values ) has_term( 'apple', 'product_brand', $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 have now out there a welcome10 coupon for a ten% low cost, a buyer with these alternatives will get pleasure from a 2.50€ low cost.



4. Set a reduction just for variable merchandise with out the Giant measurement attribute.
To allow this rule, add the next code within the features.php
file of your theme:
1 |
|
2 |
perform wc_custom_coupon_rules( $legitimate, $product, $coupon, $values ) { |
3 |
if ( 'variation' === $product->get_type() && 'Giant' === $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 have now out there a welcome10 coupon for a ten% low cost, a buyer with these alternatives will get pleasure from a 5€ low cost.



Conclusion
As you possibly can see, with only a small quantity of code, we set customized low cost guidelines for various WooCommerce taxonomies.
You may construct on the offered code and customise it in response to your wants. For instance, you possibly can restrict a few of these circumstances solely to sure coupons or make the filter choices dynamic by including new fields within the admin. Nothing stops you from combining your personal circumstances with those that WooCommerce supplies by default.
As at all times, thanks loads for studying!
Leave a Reply