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.
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.
Share Improve this question asked Nov 16, 2018 at 11:01 fightstarr20fightstarr20 1,1358 gold badges26 silver badges47 bronze badges1 Answer
Reset to default 2You can try following code:
add_filter('woocommerce_cart_needs_shipping_address','fun_return_shipping_param');
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;
}