php – Remove coupon code for specific product if coupon discount smaller than sales price

Question

I want to remove coupon only for items that has already discount and that is larger than added coupon discount.

I have code that adds regular price to price if coupon is added.
But need also code that removes discount for that already discounted product inside else clause inside below code.

function add_custom_price( $cart_object) {

global $woocommerce;

if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return;

$coupon = False;

if ($coupons = WC()->cart->get_applied_coupons()  == False )
    $coupon = False;
else {
    foreach ( WC()->cart->get_applied_coupons() as $code ) {
        $coupons1 = new WC_Coupon( $code );

        if ($code === 'randomCoupon' && $coupons1->get_discount_type() == 'percent'){
            $coupon = True;
        }
    }
}

if ($coupon == True)
    foreach ( $cart_object->get_cart() as $cart_item )
    {
        $price = $cart_item['data']->get_regular_price();
        $priceSales = $cart_item['data']->get_sale_price();

        $discountPercentage = 0;

        if(is_numeric($priceSales)) {
            $discountPercentage = ($priceSales * 100) / $price;
        }

        if($discountPercentage < $couponPercentage) {
            $cart_item['data']->set_price( $price );
        } else {
          // Here I need a solution that removes coupon for that product
        }
  }
}

0
Shark 4 weeks 2021-08-31T09:02:06-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse