I am currently enabling shipping for virtual product in WooCommerce by inserting the following in my functions file

add_filter( 'woocommerce_cart_needs_shipping_address', '__return_true', 50 );

Is there a way to modify this so I can exclude a product? I have one specific virtual product that does not need a shipping address.


You can try following code:

function fun_return_shipping_param($needs_shipping_address)

    $items = WC()->cart->get_cart();
    $product_ids = array();
    foreach($items as $item => $values) 
            $product_ids[] = $values['data']->get_id(); //You can get product id of product added in cart
    if(in_array($your_product_id, $product_ids)) // check whether your product is in cart
       $needs_shipping_address = true;

    return $needs_shipping_address;

  • Thats brilliant thank you. I modified your example to fit my needs and all works great. – fightstarr20 Nov 16 '18 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.