Coupon with 2 different percentage discounts on product category in Woocommerce

I am looking for a Woocommerce hook that will help change the discount percentage based on 2 different product category restrictions when a particular coupon is applied.

For example, if a customer adds a specific coupon, I will like:

  1. If the basket item belongs to product category A, it will give a 10% discount on this product.
  2. if he is in product category B, he will give a 20% discount on this product
  3. Update final price

Is there any hook that may be available to achieve this? Any action hook or filter hook available?

This is my code:

add_filter( 'woocommerce_get_discounted_price', 'apply_coupon', 10); function apply_coupon($price) { global $woocommerce; $product=$woocommerce->cart->product; if(has_term( 'duplex-blinds', 'A' ,$product->id)){ get_product_cart_price; 10% DISCOUNT } if(has_term( 'duplex-blinds', 'A' ,$product->id)){ 20% DISCOUNT } upadte total_discunt_incart($new_discount); upadte new_price_in_cart($new_price); upadte new_price_in_checkout($new_price); return $price; } 

The important thing is that I need to change the total price of the goods , the total redemption value , the total discount price and the discount price to send to Paypal.

There are a lot of hooks in my store, so if you consider that the calculation of the default coupon in trading will be incorrect. And I noticed that the price of the basket in the basket goes correctly based on the user value of the product, but it does not update from the original basket amount, so the total price remains unchanged.

But on the discount page, the discount price is calculated based on the original price of the product, and not the commodity price of the product, so the discount goes wrong and it is also not minimized from the total price ...

+1
source share
1 answer

The following is a completely different way to make it work ... This response code will allow you to get a coupon code with 2 different percentage discounts based on two specific product categories.

Do not say, for example, that your related product categories:

  • For a 10% coupon discount, the product category slug will be 'hoodies'
  • For a 20% discount coupon, the product category slug will be 't-shirts'

(you can use product category identifiers, bullets or names in code)

This will require 2 steps:

  1. Coupon settings (set the coupon code correctly):
    • Discount Type: Percentage
    • Quantity: 10
    • Restrictions> Product Categories (Display Names): Hoodies and T-Shirts enter image description here
    • You may have other settings if you need
  2. Settings inside the code function:
    • Coupon code: enter your lowercase coupon code
    • Product category ' 't-shirts' (for a 20% discount).

Now here is the code (where you add your settings):

 add_filter( 'woocommerce_coupon_get_discount_amount', 'alter_shop_coupon_data', 20, 5 ); function alter_shop_coupon_data( $round, $discounting_amount, $cart_item, $single, $coupon ){ ## ---- Your settings ---- ## // Related coupons codes to be defined in this array (you can set many) $coupon_codes = array('10percent'); // Product categories at 20% (IDs, Slugs or Names) for 20% of discount $product_category20 = array('hoodies'); // for 20% discount $second_percentage = 0.2; // 20 % ## ---- The code: Changing the percentage to 20% for specific a product category ---- ## if ( $coupon->is_type('percent') && in_array( $coupon->get_code(), $coupon_codes ) ) { if( has_term( $product_category20, 'product_cat', $cart_item['product_id'] ) ){ $original_coupon_amount = (float) $coupon->get_amount(); $discount = $original_coupon_amount * $second_percentage * $discounting_amount; $round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() ); } } return $round; } 

The code goes in the function.php file of your active child theme (or active theme). Tested and working.


Here is an illustrated real working example (with screenshots):

enter image description here

enter image description here

  • The 1st position of the basket (from the 'hoodies' product category) receives a 10% discount $40 x 10% = $4
  • The 2nd position of the basket (from the 't-shirts' category) receives a 20% discount $30 x 20% = $6

Thus, the total discount is $4 + $6 = $10 ... It works!

+5
source

All Articles