本文,我们将介绍如何简化 WooCommerce 结帐页面,以便提升客户的购物体验。
安装子主题
在开始简化 WooCommerce 结帐页面流程之前,我们先创建和安装当前主题的子主题,以便在主题更新后可保留我们的更改。下面所示的所有代码都将放在子主题的 functions.php 文件中。
设置默认国家和州
如果我们的订单只来自特定国家或地区,我们可以在结帐页面上将该地域设置为默认值,这样客户就不必在结帐页面上填写它们。
add_filter( 'default_checkout_billing_country', 'cssigniter_change_default_checkout_billing_country' );
add_filter( 'default_checkout_billing_state', 'cssigniter_change_default_checkout_billing_state' );
function cssigniter_change_default_checkout_billing_country() {
return 'GR';
}
function cssigniter_change_default_checkout_billing_state() {
return 'M';
}
如上所示,我们使用了两个钩子,default_checkout_billing_country 用于默认国家, default_checkout_billing_state 用于默认州。在 return ‘GR’ 中,需要填写国家的代码,在return ‘M’ 中,我们需要填写州/地区的代码。在本示例中,我们选择希腊作为默认国家,克里特岛作为默认州。
在结帐页面取消设置帐单字段
帐单信息表包含许多字段,在某些情况下,其中一些可能对我们没有用,在这种情况下,可以选择隐藏它们,以缩短客户填写字段的时间。
add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
// unset( $fields['billing']['billing_first_name'] );
// unset( $fields['billing']['billing_last_name'] );
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'] );
// unset( $fields['order']['order_comments'] );
// unset( $fields['billing']['billing_email'] );
// unset( $fields['account']['account_username'] );
// unset( $fields['account']['account_password'] );
// unset( $fields['account']['account_password-2'] );
return $fields;
}
在上面的代码中,每一行都取消了某个结帐结算字段,通过它们的名称,我们可以很容易地理解哪一行取消了哪个字段。在本示例中,我们没有注释未设置公司名称、街道地址的第二行和国家/地区的行(例如,如果您只在特定国家/地区运送产品,并且使用设置默认国家/地区,则可以这样做)上一节中描述的代码)。
限制订单备注的长度
WooCommerce 提供了一个订单备注的文本区域,客户可以填写他们想要店主、送货人员知道的任何内容。WooCommerce 未对这些备注的长度进行限制,这可能会导致客户添加过多无效信息。为了避免这种情况,我们可以使用 woocommerce_checkout_fields 挂钩过滤结帐字段,以将订单备注限制在 500 个字符以内。
add_filter( 'woocommerce_checkout_fields', 'filter_checkout_fields' );
function filter_checkout_fields( $fields ) {
$placeholder = $fields['order']['order_comments']['placeholder'] . __( ' 500 characters max.', 'your-text-domain' );
$fields['order']['order_comments']['placeholder'] = $placeholder;
$fields['order']['order_comments']['maxlength'] = 500;
return $fields;
}
在上面的代码片段中,我们为 WooCommerce 的订单备注设置了占位符,以让客户更直观的了解长度限制。
作者:牛奇网,本站文章均为辛苦原创,在此严正声明,本站内容严禁采集转载,面斥不雅请好自为之,本文网址:https://www.niuqi360.com/wordpress/streamline-your-woocommerce-checkout-page/