Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Balakrishnan-flycart/94fee7d87c7e4dae860e426b89c71de6 to your computer and use it in GitHub Desktop.
Save Balakrishnan-flycart/94fee7d87c7e4dae860e426b89c71de6 to your computer and use it in GitHub Desktop.
Woo Discount Rules v2 - Apply Discount rule ( remove copon code)
add_filter('advanced_woo_discount_rules_do_apply_price_discount', function($return, $price, $cart_item, $cart_object, $calculated_cart_item_discount){
if (class_exists('\Wdr\App\Helpers\Woocommerce') && class_exists('\Wdr\App\Controllers\ManageDiscount')) {
$cart_item_key = isset($cart_item['key']) ? $cart_item['key'] : '';
$discount_details = isset($calculated_cart_item_discount['total_discount_details']) ? $calculated_cart_item_discount['total_discount_details'] : '';
if(isset($discount_details[$cart_item_key])&& !empty($discount_details[$cart_item_key])){
foreach($discount_details[$cart_item_key] as $rule_id => $rule_data){
$applied_coupons = \Wdr\App\Helpers\Woocommerce::getAppliedCoupons();
if( in_array($rule_id, array(1, 2, 3)) && !empty($applied_coupons)){ //array(1,2,3) -> enter your rule id(s)
foreach($applied_coupons as $coupon_code){
$remove_msg = __( 'Coupon code applied successfully.', 'woocommerce' );
\Wdr\App\Helpers\Woocommerce::removeSpecificNoticeFromSession($remove_msg);
$warning_msg = sprintf(__('Sorry, it is not possible to apply coupon <b>"%s"</b> as you already have a discount applied in cart.', 'woo-discount-rules'), $coupon_code);
\Wdr\App\Helpers\Woocommerce::remove_coupon($coupon_code);
\Wdr\App\Helpers\Woocommerce::wc_add_notice( $warning_msg, 'notice' );
}
}
}
}
}
return $return;
}, 10, 5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment