Created
July 7, 2018 15:52
-
-
Save codeagencybe/f7ee93916cfef12eb0d555476a16b632 to your computer and use it in GitHub Desktop.
hide free shipping if shipping class is in cart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @snippet Disable free shipping option if a specific shipping class is added to cart | |
| * @author Fabio Tielen // Code Agency // https://codeagency.be | |
| * @testedwith WooCommerce 3.2+ | |
| */ | |
| add_filter( 'woocommerce_package_rates', 'ca_hide_free_shipping_for_shipping_class', 10, 2 ); | |
| function ca_hide_free_shipping_for_shipping_class( $rates, $package ) { | |
| $shipping_class_target = XXX; // search for shipping class ID in your settings | |
| $in_cart = false; | |
| foreach( WC()->cart->cart_contents as $key => $values ) { | |
| if( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) { | |
| $in_cart = true; | |
| break; | |
| } | |
| } | |
| if( $in_cart ) { | |
| unset( $rates['free_shipping:YYY'] ); // search for the shipping method ID in your settings, | |
| } | |
| return $rates; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment