I know that I can automatically disable shipping fields by checking "virtual" on the product submission form - but how could I by default disable shipping fields on the checkout for Woocommerce Booking products (for "booking" product type)?
The following will disable checkout shipping fields when a specific product type is in cart (here "booking" product type):
add_filter( 'woocommerce_cart_needs_shipping_address', 'filter_cart_needs_shipping_address_callback' );
function filter_cart_needs_shipping_address_callback( $needs_shipping_address ){
// Loop through cart items
foreach ( WC()->cart->get_cart() as $item ) {
if ( $item['data']->is_type('booking') ) {
$needs_shipping_address = false;
break; // Stop the loop
}
}
return $needs_shipping_address;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Reminder: to hide “Ship to a different address” in Woocommerce we just use:
add_filter( 'woocommerce_cart_needs_shipping_address', '__return_false');