WooCommerce tip: Tips on how to handle reductions based mostly on taxonomies

By default, WooCommerce permits us to exclude merchandise that belong to particular classes from low cost provides or to use reductions solely to merchandise that belong to specific classes.
Nonetheless, 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 function, but it surely isn’t that troublesome to implement ourselves!
All we have now 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 by way of some examples!
1. Set a reduction just for merchandise (easy or variable) with out the Inventory Affords 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 ) ) : |
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 accessible 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 Affords 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 ) ) : |
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 accessible 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 Affords 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 accessible 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 dimension 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 accessible 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’ll be able to construct on the supplied code and customise it based on 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 individual circumstances with those that WooCommerce supplies by default.
As all the time, thanks quite a bit for studying!