Skip to content

Instantly share code, notes, and snippets.

@NickGreen
Last active April 11, 2019 01:27
Show Gist options
  • Save NickGreen/e04f3f3c3a70678e0e1985f9aa0dce37 to your computer and use it in GitHub Desktop.
Save NickGreen/e04f3f3c3a70678e0e1985f9aa0dce37 to your computer and use it in GitHub Desktop.
Remove coupons from cart if there's a subscription product in it.
<?php
function auto_remove_coupon_sub( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
if ( method_exists( 'WC_Subscriptions_Cart' , 'cart_contains_subscription' ) ) {
if ( WC_Subscriptions_Cart::cart_contains_subscription() && $cart->has_discount() ) {
$cart->remove_coupons();
wc_clear_notices();
wc_add_notice( __("Coupon may not be applied to a subscription product - coupon removed ","woocommerce"), 'notice');
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'auto_remove_coupon_sub' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment