Skip to content

Instantly share code, notes, and snippets.

@wpflames
Created February 15, 2022 18:32
Show Gist options
  • Select an option

  • Save wpflames/6d7f4f8ba8070c0d27974ca81199a94e to your computer and use it in GitHub Desktop.

Select an option

Save wpflames/6d7f4f8ba8070c0d27974ca81199a94e to your computer and use it in GitHub Desktop.
Disable Specific Shipping Method based on Category
<?php
// =========================================================================
// UNSET SPECIFIC SHIPPING METHOD BASED ON CATEGORY IN THE CART
// =========================================================================
function unset_shipping_based_on_category( $rates) {
$gift_in_cart = false;
// Gift
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( has_term( 'nem-virag', 'product_cat', $cart_item['product_id'] ) ) {
$gift_in_cart = true;
break;
}
}
if($gift_in_cart) {
unset( $rates['flat_rate:20'] );
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'unset_shipping_based_on_category');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment