I am looking forward on how to hide shipping at all from the checkout page of Woocommerce when only certain product (magazine) is in the cart or much better would be, if the only product/s with certain shipping class are in the cart.
If there is a combination of products (magazine + books), then shipping module should be visible.
This is what I have tried, but this always returns false. Can you help me out on this?
add_filter( 'woocommerce_package_rates', 'woocommerce_cart_shipping_total_filter_callback', 11, 2 );
function woocommerce_cart_shipping_total_filter_callback( $show_abon_shipping) {
$product_id = 27733;
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( $product_in_cart === $product_id && count($product_in_cart) == 1) {
$in_cart = true;
}
}
if ( $in_cart ) {
return false;
}
return $show_abon_shipping;
}
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Solved it. Just remeber to add the same shipping class to all variations too.
add_filter( 'woocommerce_package_rates', 'custom_shipping_rates', 100, 2 );
function custom_shipping_rates( $rates, $package ) {
$shipping_class = 506; // HERE set the shipping class ID
$found = false;
$productsincart = count($package['contents']);
foreach( $package['contents'] as $cart_item ) {
if ($productsincart == 1 && $cart_item['data'];->get_shipping_class_id() == $shipping_class){
$found = true;
}
}
if ( $found ) {
return false; // If not found we exit
}else {
return $rates;
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0