3

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)?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Marko I.
  • 542
  • 10
  • 38

1 Answers1

4

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');
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I tried this code but I'm still getting: "No shipping method has been selected. Please double check your address, or contact." – Marko I. Aug 24 '19 at 15:44
  • I would like to disable requirement to select shipping method as well... (for this particular product type) – Marko I. Aug 24 '19 at 15:46
  • 1
    @MarkoI. You asked about disabling **shipping checkout fields**, but **not about shipping methods** in your question… so my answer is made for disabling checkout shipping fields. If you want to disable shipping methods, this should be a **new** explicite question that you need to ask (I will answer it, if you notify me here once published). – LoicTheAztec Aug 24 '19 at 15:50
  • https://stackoverflow.com/questions/57639470/how-to-disable-requirement-to-select-shipping-method-for-woocommerce-product-typ – Marko I. Aug 24 '19 at 15:56
  • 1
    @MarkoI. I have answered your new question… It disable shipping completely (shipping methods and shipping checkout fields too). – LoicTheAztec Aug 24 '19 at 16:28
  • could you please check my other question - not sure what could be the problem: https://stackoverflow.com/questions/57639007/fatal-error-when-woocommerce-custom-field-is-triggered – Marko I. Aug 24 '19 at 17:02
  • Do you know how to disable the same shipping fields for downloadable products? I tried switching "booking" with "_downloadable" in your code above but it's not working. – Marko I. Sep 02 '19 at 19:26
  • https://stackoverflow.com/questions/57778144/how-to-disable-shipping-checkout-fields-for-downloadable-products-in-woocommerce – Marko I. Sep 03 '19 at 20:09
  • @MarkoI. I have answered your question… – LoicTheAztec Sep 03 '19 at 20:25