我目前正在WooCommerce中通过在我的功能文件中插入以下内容来启用虚拟产品的发货
add_filter( \'woocommerce_cart_needs_shipping_address\', \'__return_true\', 50 );
是否有办法修改此选项,以便排除某个产品?我有一个特定的虚拟产品,不需要发货地址。
最合适的回答,由SO网友:Aparna_29 整理而成
您可以尝试以下代码:
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;
}