$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>WooCommerce - Conditionally enable shipping for virtual products|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

WooCommerce - Conditionally enable shipping for virtual products

matteradmin6PV0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 2

You 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;

}
Post a comment

comment list (0)

  1. No comments so far