Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sarancartrabbit/95054199c5dcad65b7dd4a7bfba617c0 to your computer and use it in GitHub Desktop.
Save sarancartrabbit/95054199c5dcad65b7dd4a7bfba617c0 to your computer and use it in GitHub Desktop.
Discount rules v2: Customization - Remove zero coupon
add_action('woocommerce_before_cart', function(){
if(class_exists('\Wdr\App\Router') && class_exists('\Wdr\App\Controllers\DiscountCalculator') && class_exists('\Wdr\App\Helpers\Woocommerce')){
$manage_discount = \Wdr\App\Router::$manage_discount;
$calculator = $manage_discount::$calculator;
$used_coupons = $calculator::getUsedCoupons();
$applied_coupons = \Wdr\App\Helpers\Woocommerce::getAppliedCoupons();
foreach ($applied_coupons as $applied_coupon) {
if (in_array($applied_coupon,$used_coupons)) {
$coupon = new WC_Coupon($applied_coupon);
// Check if the coupon has zero value
if ($coupon->get_amount() == 0) {
$manage_discount->removeAppliedCoupon($applied_coupon);
}
}
}
}
});
add_filter('woocommerce_coupon_message', function ($message, $msg_code, $coupon) {
if(class_exists('\Wdr\App\Router') && class_exists('\Wdr\App\Controllers\DiscountCalculator') && class_exists('\Wdr\App\Helpers\Woocommerce')) {
$manage_discount = \Wdr\App\Router::$manage_discount;
$calculator = $manage_discount::$calculator;
$used_coupons = $calculator::getUsedCoupons();
if (in_array($coupon->get_code(), $used_coupons) && $coupon->get_amount() == 0) {
wc_add_notice(__('Sorry, it is not possible to apply coupon . '.$coupon->get_code().' as you already have a discount applied in cart. ', 'woo-discount-rules'), 'error');
$message = '';
}
return $message;
}
}, 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment