字母字符仅用于 WooCommerce 中的账单和运输名称
Alphabet characters only for billing and shipping names in WooCommerce
如何在 WooCommerce 结账时限制在账单名字、账单姓氏、发货名字和发货姓氏中仅包含字母字符?
我正在我的子主题的 functions.php 文件中尝试以下操作,这导致我在 WooCommerce 中结帐时不显示结帐部分。请指教。
add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
$fields['billing']['billing_first_name'] = array(
'label' => __('First name', 'woocommerce'),
'placeholder' => _x('First name', 'placeholder', 'woocommerce'),
'required' => false,
'clear' => false,
'type' => 'text',
'class' => array(
'alpha'
)
);
}
You can do this by validating checkout form submit at server side by
using woocommerce_checkout_process
hook, and PHP
ctype_alpha()
function.
代码如下:
add_action('woocommerce_checkout_process', 'wh_alphaCheckCheckoutFields');
function wh_alphaCheckCheckoutFields() {
$billing_first_name = filter_input(INPUT_POST, 'billing_first_name');
$billing_last_name = filter_input(INPUT_POST, 'billing_last_name');
$shipping_first_name = filter_input(INPUT_POST, 'shipping_first_name');
$shipping_last_name = filter_input(INPUT_POST, 'shipping_last_name');
$ship_to_different_address = filter_input(INPUT_POST, 'ship_to_different_address');
if (empty(trim($billing_first_name)) || !ctype_alpha($billing_first_name)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Billing First Name</strong>.'), 'error');
}
if (empty(trim($billing_last_name)) || !ctype_alpha($billing_last_name)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Billing Last Name</strong>.'), 'error');
}
// Check if Ship to a different address is set, if it's set then validate shipping fields.
if (!empty($ship_to_different_address)) {
if (empty(trim($shipping_first_name)) || !ctype_alpha($shipping_first_name)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Shipping First Name</strong>.'), 'error');
}
if (empty(trim($shipping_last_name)) || !ctype_alpha($shipping_last_name)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Shipping Last Name</strong>.'), 'error');
}
}
}
代码进入您的活动子主题(或主题)的 functions.php
文件。或者在任何插件 PHP 文件中。
代码已经过测试并且可以工作。
希望对您有所帮助!
如何在 WooCommerce 结账时限制在账单名字、账单姓氏、发货名字和发货姓氏中仅包含字母字符?
我正在我的子主题的 functions.php 文件中尝试以下操作,这导致我在 WooCommerce 中结帐时不显示结帐部分。请指教。
add_filter('woocommerce_checkout_fields', 'custom_override_checkout_fields');
function custom_override_checkout_fields($fields) {
$fields['billing']['billing_first_name'] = array(
'label' => __('First name', 'woocommerce'),
'placeholder' => _x('First name', 'placeholder', 'woocommerce'),
'required' => false,
'clear' => false,
'type' => 'text',
'class' => array(
'alpha'
)
);
}
You can do this by validating checkout form submit at server side by using
woocommerce_checkout_process
hook, and PHPctype_alpha()
function.
代码如下:
add_action('woocommerce_checkout_process', 'wh_alphaCheckCheckoutFields');
function wh_alphaCheckCheckoutFields() {
$billing_first_name = filter_input(INPUT_POST, 'billing_first_name');
$billing_last_name = filter_input(INPUT_POST, 'billing_last_name');
$shipping_first_name = filter_input(INPUT_POST, 'shipping_first_name');
$shipping_last_name = filter_input(INPUT_POST, 'shipping_last_name');
$ship_to_different_address = filter_input(INPUT_POST, 'ship_to_different_address');
if (empty(trim($billing_first_name)) || !ctype_alpha($billing_first_name)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Billing First Name</strong>.'), 'error');
}
if (empty(trim($billing_last_name)) || !ctype_alpha($billing_last_name)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Billing Last Name</strong>.'), 'error');
}
// Check if Ship to a different address is set, if it's set then validate shipping fields.
if (!empty($ship_to_different_address)) {
if (empty(trim($shipping_first_name)) || !ctype_alpha($shipping_first_name)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Shipping First Name</strong>.'), 'error');
}
if (empty(trim($shipping_last_name)) || !ctype_alpha($shipping_last_name)) {
wc_add_notice(__('Only alphabets are alowed in <strong>Shipping Last Name</strong>.'), 'error');
}
}
}
代码进入您的活动子主题(或主题)的 functions.php
文件。或者在任何插件 PHP 文件中。
代码已经过测试并且可以工作。
希望对您有所帮助!