如果你的WooCommerce店铺有卖虚拟产品,那么在结算界面你一定会发现还是会出现像街道地址、邮政编码、城市、国家这些信息。为毛需要这些?明明是虚拟的产品,根本不需要发货。
这样做肯定会让人觉得买东西太麻烦,一个稍微敏感点的客户,很可能会觉得你这是在“瞎要信息”。让人觉得这个电商网站不靠谱,非得多填一堆没必要的东西。
删除WooCommerce多余的结算信息
下面就是结算页面默认的信息需求,包括虚拟产品的。

把下面的代码用自定义代码插件或者用子主题的functions.php文件加到网站上就行。
/* 去除虚拟产品不需要的结算字段 */
add_filter( 'woocommerce_checkout_fields' , 'jhchen_checkput_virtual_simple' );
function jhchen_checkput_virtual_simple( $fields ) {
$only_virtual = true;
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// 检测非虚拟产品
if ( ! $cart_item['data']->is_virtual() ) $only_virtual = false;
}
if( $only_virtual ) {
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;
}