Last active
June 17, 2016 09:34
-
-
Save ChromeOrange/9486596 to your computer and use it in GitHub Desktop.
Check the cart for specific shipping classes, remove Free Shipping if they are present
This file contains 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
/** | |
* Check the cart for specific shipping classes, remove Free Shipping if they are present | |
* | |
* This code will remove free shipping if a product is in the cart that can not be delivered for free | |
* Assign a shipping class to those products and add the slug to the $freeshipping_array | |
* Multiple shipping classes can be added to the array | |
* | |
* Add the code to your theme functions.php file | |
*/ | |
add_filter( 'woocommerce_package_rates', 'unset_free_shipping_when_product_in_cart' , 10, 1 ); | |
function unset_free_shipping_when_product_in_cart( $available_methods ) { | |
/** | |
* Set free shipping variable to true | |
* | |
* Free shipping is allowed unless we find a product with a shipping class that | |
* does not allow free shipping | |
*/ | |
$freeshipping = true; | |
// Setup an array or shipping classes that do not allow free shipping | |
$freeshipping_array = array( 'local-delivery' ); | |
// loop through the cart checking the shipping classes | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { | |
$shipping_class = get_the_terms( $values['product_id'], 'product_shipping_class' ); | |
if( in_array( $shipping_class[0]->slug, $freeshipping_array ) && $freeshipping ) { | |
$freeshipping = false; | |
break; | |
} | |
} | |
// Unset Free Shipping if necessary | |
if( $freeshipping == false ) { | |
unset( $available_methods['free_shipping'] ); | |
} | |
return $available_methods; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should help re Woo update to 2.6.
I amended part of the original code above to that as below. I removed the var_dump comment to see on the front end what is used and find the names (note; you'll need to check instance ID as will be different). Then I Unset as normal (though I needed to add two in as I've got free delivery in more than one zone). Hope this helps.
// Developers!: Dump this to see what you can unset // var_dump( $available_methods ); // Unset Free Shipping if necessary if( $freeshipping == false ) { unset( $available_methods['free_shipping:5'], $available_methods['free_shipping:6'] ); }