Last active
November 4, 2015 15:32
-
-
Save EvanHerman/21f76a6ac5a46275f66d to your computer and use it in GitHub Desktop.
Disable WooCommerce Free Shipping based on the assigned product category (in this case category with ID #112)
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
<?php | |
/** | |
* Disable free shipping for select WooCommerce product categories | |
* - In this case, free shipping is disabled when a product has the category with ID 112 assigned to it | |
* @param bool $is_available | |
*/ | |
function disable_free_shipping_for_certain_product_categories( $is_available ) { | |
global $woocommerce; | |
// set the product ids that are ineligible | |
$ineligble_categories = array('112'); | |
// get cart contents | |
$cart_items = $woocommerce->cart->get_cart(); | |
// loop through the items looking for one in the ineligible array | |
foreach ( $cart_items as $key => $item ) { | |
$product_categories = wp_get_object_terms( $item['product_id'], 'product_cat', array( 'fields' => 'ids' ) ); | |
if( $product_categories ) { | |
foreach( $product_categories as $category_id ) { | |
// 112 -> Gynamistic Mats | |
if( in_array( $category_id, $ineligble_categories ) ) ) { | |
return false; | |
} | |
} | |
} | |
} | |
// nothing found return the default value | |
return $is_available; | |
} | |
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'disable_free_shipping_for_certain_product_categories', 20 ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment