1

I have come across

Coupon with 2 different percentage discounts based on product category in Woocommerce

Which shows how to alter % discounts for set categories. I want to implement a summer discount code on my store which gives £20 off to a selection of products.

The coupon code summer21 is set to apply a fixed product discount on only the following allowed product IDs

  • 2523, 119800, 119886 & 120253

On 3 of the 4 a £20 discount should apply

But if the product ID = 119886, then only £10 should be discounted


I've tried modifying the code to the following, but unfortunately without the desired result. What am I missing?

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('Summer21');

    // Product categories for only £10 discount (IDs, Slugs or Names)
    $product_category10 = array('119886'); // for £10 discount

    $second_discount = 10; // £10

    ## ---- The code: Changing the discount to £10 for specific products ---- ##

    if ( $coupon->is_type('fixed_product') && in_array( $coupon->get_code(), $coupon_codes ) ) {
        if( has_term( $product_category10, 'product_cat', $cart_item['product_id'] ) ){
            $discount = $discounting_amount - $second_discount;
            $round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
        }
    }
    return $round;
}

This code also only changes the cart price, but does not show the correct display prices on the product pages.

Henry Aspden
  • 1,863
  • 3
  • 23
  • 45

1 Answers1

3

Your code contains several mistakes

  • has_term() is not applicable, we are using in_array() instead
  • Cart item quantity per product is not taken into account.

To make this answer work, we apply some steps

First step, coupon settings:

  • Title: summer21
  • General > Discount type: Fixed product discount
  • General > Coupon amount: 20
  • Usage restriction > Products: where productID: 2523, 119800, 119886, 120253

Second step, settings inside the code function:

  • Coupon codes: set your coupon code in lowercase
  • Product IDs, for second discount: 119886
  • Set the second discount: 10

So you get:

function filter_woocommerce_coupon_get_discount_amount( $round, $discounting_amount, $cart_item, $single, $coupon ) {   
    // Related coupons codes to be defined in this array
    $coupon_codes = array( 'summer21' );

    // Product IDs, for second discount
    $product_ids = array( 119886 ); 

    // Second discount
    $second_discount = 10;

    // Changing the discount for specific product Ids
    if ( $coupon->is_type('fixed_product') && in_array( $coupon->get_code(), $coupon_codes ) ) {                
        // Search for product ID in array product IDs
        if ( in_array( $cart_item['product_id'], $product_ids ) ) {
            // Get cart item quantity
            $cart_item_qty = is_null( $cart_item ) ? 1 : $cart_item['quantity'];
            
            // Discount
            $discount = $coupon->get_amount() - $second_discount;           
            $discount = $single ? $discount : $discount * $cart_item_qty;
            
            $round = round( min( $discount, $discounting_amount ), wc_get_rounding_precision() );
        }       
    }
    
    return $round;
}
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • YOU STAR ! Thanks that works a charm. What would be the function to show not just on cart but also on product pages / shop etc? Currently the shop displays £75 (£20 discount) whilst the cart now shows the correct £10 discount of £85. – Henry Aspden Jul 09 '21 at 10:39
  • just noticed that this also shows the £95 product as £55 in the sidecart / minicart whilst £85 correctly on basket / checkout – Henry Aspden Jul 09 '21 at 11:14
  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. – 7uc1f3r Jul 09 '21 at 18:00
  • I'd say this was part of the original question – Henry Aspden Jul 13 '21 at 08:58
  • @HenryAspden my answer is based on the answer code you referenced in your question, as you can see in that answer, no extra code is provided for adjustments on other pages, so ask a new question or adjust your question where necessary – 7uc1f3r Jul 13 '21 at 09:15
  • Sorry @7uc1f3r i thought that it was self explanatory that I would also want the prices showing correctly not just on the cart but globally across all product pages / single item pages etc. I see now how this may have been confusing. Sorry. Did you also see how the discount is doubly applied in the minicart when using your formula? – Henry Aspden Jul 13 '21 at 09:48
  • @HenryAspden A coupon code is entered on the cart/checkout page and only applies there. **By default**, the price of a product is not adjusted by means of a coupon code and **would require much more detail in your question if this were the case for you**. I've never seen it before on any website. By default only the cart total is calculated based on the coupon code amount. Even when using a coupon, without my code answer. The price of the product will not change elsewhere by default. I don't have any issues in the minicart either, it could be theme/other plugins you using related – 7uc1f3r Jul 13 '21 at 12:29
  • This isn't strictly true. if you input on any woocommerce site the product code at basket or checkout, then navigate back to the product pages ```/shop/``` etc you will see that the discounted prices are displayed, and on the individual product page too – Henry Aspden Jul 13 '21 at 13:45
  • please see https://stackoverflow.com/questions/68363967/1-coupon-code-to-give-2-different-fixed-product-discounts-based-on-product-ids-i @7uc1f3r – Henry Aspden Jul 13 '21 at 14:04
  • You indicate "on any woocommerce site", I tested it on 6 different websites, and this is not the case. I am therefore unable to reproduce the problem and can't help you any further – 7uc1f3r Jul 13 '21 at 14:36