Remove shipping method from WooCommerce cart programmatically


add_filter( ‘woocommerce_package_rates’, ‘aviweb_hide_free_shipping_class’, 10, 2 );

function aviweb_hide_free_shipping_class( $rates, $package ) {
echo $in_cart = false;
foreach( WC()->cart->cart_contents as $key => $values ) {
$product_id = $values[‘product_id’];
if ( $product_id == 2 ) {
$in_cart = true;
break;
}
}
if( $in_cart ) {
unset( $rates[‘flat_rate:7’] );
}
return $rates;
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s