WooCommerce购物车中只有虚拟商品时,移除不必要的结账字段
如果我们使用 WooCommerce 发布虚拟产品 – 例如付费下载资源WooCommerce购物车中只有虚拟商品时,移除不必要的结账字段,用户可以在购买后直接下载,而无需将产品或账单发送给客户。这时候,结帐页面上的收件人地址字段显然是多余的。 .
如果结账时客户的订单中只有虚拟商品虚拟商品虚拟商品,我们可以去掉收货地址字段WooCommerce购物车中只有虚拟商品时,移除不必要的结账字段,减少用户需要填写的内容,提高结账效率。满足这一要求的关键是确定购物车中只有虚拟物品。
我们可以遍历购物车中的物品进行判断。只要有一件物品不是虚拟物品,删除收货地址字段的条件不成立。详情请参考以下代码。
add_filter('woocommerce_checkout_fields', function ($fields)
{
$only_virtual = true;
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
// Check if there are non-virtual products
if ( ! $cart_item[ 'data' ]->is_virtual()) {
$only_virtual = false;
}
}
if ($only_virtual) {
unset($fields[ 'billing' ][ 'billing_first_name' ]);
unset($fields[ 'billing' ][ 'billing_last_name' ]);
unset($fields[ 'billing' ][ 'billing_email' ]);
unset($fields[ 'billing' ][ 'billing_company' ]);
unset($fields[ 'billing' ][ 'billing_address_1' ]);
unset($fields[ 'billing' ][ 'billing_address_2' ]);
unset($fields[ 'billing' ][ 'billing_city' ]);
unset($fields[ 'billing' ][ 'billing_postcode' ]);
unset($fields[ 'billing' ][ 'billing_country' ]);
unset($fields[ 'billing' ][ 'billing_state' ]);
unset($fields[ 'billing' ][ 'billing_phone' ]);
add_filter('woocommerce_enable_order_notes_field', '__return_false');
}
return $fields;
});
评论前必须登录!
注册