分享笔记:

在结账页面隐藏多余的免费商品字段

去除多余结算字段

我们之前说过,如果是虚拟商品用不到的一堆结账字段最好都隐藏掉,那么如果是免费的商品,那更是没必要填那么多东西的,尤其是那些不发货、纯虚拟的那种。

删除WooCommerce多余的结算信息

下面这段代码可以帮你设置,当购物车里只有免费商品的时候,结账页面只显示你希望保留的字段,让客户只填最基本的。

/* Campos necesarios en productos gratuitos virtuales */
add_filter( 'woocommerce_checkout_fields', 'jhchen_fields_free_products', 99999999 );
function jhchen_fields_free_products( $fields ) { 
if ( WC()->cart && ! WC()->cart->needs_payment() ) {
$free_fields = [];
foreach ( $fields['billing'] as $key => $value ) {
if ( in_array( $key, [ 'billing_email', 'billing_first_name', 'billing_last_name' ] ) ) {
$free_fields['billing'][$key] = $value;
}
}
return $free_fields;
}
return $fields;
}

上面的代码,我们在数组里指定只保留 billing_emailbilling_first_namebilling_last_name ,分别是邮箱和姓名这三个字段。这样看起来是不是合理多了?比起 WooCommerce 默认那堆乱七八糟的字段,干净利落太多了。

你也可以根据自己的需要改这段代码,想加字段就加,想删就删。

为笔记评分

平均评分 5 / 5. 摘星者: 2

有疑问?留个言吧!

更多结果...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
?>

更多结果...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors