Skip to content

Instantly share code, notes, and snippets.

@munts
Created August 26, 2020 19:37
Show Gist options
  • Save munts/2941c00cd7f8b89c30042e88a2840354 to your computer and use it in GitHub Desktop.
Save munts/2941c00cd7f8b89c30042e88a2840354 to your computer and use it in GitHub Desktop.
Remove 'Free Shipping' option if user applies coupon code to WooCommerce cart. Ie. if using coupon, do not provide free shipping.
add_filter( 'woocommerce_shipping_packages', function( $packages ) {
$applied_coupons = WC()->session->get( 'applied_coupons', array() );
if ( ! empty( $applied_coupons ) ) {
if ( in_array('promocode1', $applied_coupons) ||
in_array('promocode2', $applied_coupons)
) {
$free_shipping_id = 'woodiscountfree';
unset($packages[0]['rates'][$free_shipping_id]);
}
}
return $packages;
} );
@munts
Copy link
Author

munts commented Aug 26, 2020

This is assuming you have some logic somewhere that says something like if Cart total is more than XX, then offer free shipping. However, if they customer uses a coupon code that gets them say 20% off, then remove the free shipping option. Ie. Free Shipping is available when paying msrp.

In my snippet above, $free_shipping_id = 'woodiscountfree'

'woodiscountfree' is set using the WooDiscount plugin by FlyCart where we have a rule that Free Shipping is available if Cart is greater than $250.

This value can be found by inspecting element using dev tools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment