Jump to content

Recommended Posts

Hey Guys,  So I'm stuck on a particular line of PHP for woocommerce the code I have hear works for doing 2 levels of discount.

1 - The discount code in woocommerce coupon settings gives - 10% off for products in category named "10OFF" - Coupon settings are in the image below 

2. This code here then allows the same coupon to give 15% off off products in category named "15OFF"

3. What I can't seem to work out, is how to add another layer to this, where the same coupon will give 35% off products in a category named "35OFF"

I get the feeling this is something really super obvious to someone who knows more about PHP coding than myself! I've tried multiple IF statements, that breaks the whole thing. I'm confident Creating multiple Categories is the way forward. I just can't work out how to add the new categories to the code that doesn't suddenly break the whole thing again stopping all discounts being taken off each item.

Any advice would be hugely appreciated! I know the Code could be a lot neater in general, but I'm really new to PHP

Kind Regards to all

Liam

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 ){

//Settings//

    $coupon_codes = array('bigsavings');

    // Product categories at 15% 
    $product_category15 = array('15off'); // for 15% discount

    $second_percentage = 0.015; // 15 %

   // Code//

    if ( $coupon->is_type('percent') && in_array( $coupon->get_code(), $coupon_codes ) ) {
        if( has_term( $product_category15, '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;
}

 

coupon.png

Edited by liamhluk
Additional info on what I've tried already
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.