2
votes

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

For example if customer add a specific coupon I will like to:

  1. If a cart item is from product category A then it will give 10% discount on that item.
  2. if it's in product category B it will give 20% discount on that item
  3. Update total cart price

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

This is my code so far:

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 i need to modify the total cart price , total checkout price , total discount price and discount price need to send to Paypal.

My shop have many hooks that's why woo commerce default coupon calculation is going to wrong. And i noticed that in cart page discount price is coming correctly based on the custom product value, but it not get updated from the original cart amount, so the total price remain the same.

But in checkout page discount price is calculated based on the product original price not the product custom price so the discount is coming wrong and also it is not minimize from the total price also...

2

2 Answers

8
votes

The following is a completely different way to make that works… This answer code will enable a coupon code with 2 different discounts percentage based on 2 specific product categories.

Lest say for example that your related product categories are:

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

(you can use product category Ids, slugs or Names in the code)

That will need 2 Steps:

  1. Coupon settings (Set correctly your coupon code):
    • Discount type: Percentage
    • Amount: 10
    • Restrictions > Product categories (names displayed): "Hoodies" and "T shirts" enter image description here
    • You can have other settings if you need
  2. The settings inside the code function:
    • Coupon code: set your coupon code in lowercase
    • The 't-shirts' product category slug (for 20% of discount).

Now Here comes the code (where you will 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;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


Here is an illustrated real working example (with screen shots):

enter image description here

enter image description here

  • The 1st cart item (from 'hoodies' product category) get 10% of discount $40 x 10% = $4
  • The 2nd cart item (from 't-shirts' product category) get 20% of discount $30 x 20% = $6

So the total discount is $4 + $6 = $10 … That works!

0
votes

I using this code, but it give 100% on the 20% category, but the code you provide does work if you change the 0.2 to 0.02

$second_percentage = 0.2; // 20 %

change too: $second_percentage = 0.02; // 20 %

or: $second_percentage = 0.015; // 15 % If you need 15% discount