WooCommerce 订单评论 - 禁用特殊字符的使用
Posted
技术标签:
【中文标题】WooCommerce 订单评论 - 禁用特殊字符的使用【英文标题】:WooCommerce order comments - disabling use of special characters 【发布时间】:2022-01-09 01:16:17 【问题描述】:我正在使用以下代码将 WooCommerce 结帐输入限制为仅限字母字符(感谢这篇文章:Alphabet characters only for billing and shipping names in WooCommerce)。但是,这也会将每个结帐字段标记为“必填”,例如,即使客户不想添加任何订单 cmets,他们也必须添加一些内容才能完成订单。如何编辑它以便不需要所有字段?非常感谢您的帮助!
/* remove special charaters from checkout ? */
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');
$order_comments = filter_input(INPUT_POST, 'order_comments');
$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');
if (empty(trim($order_comments)) || !ctype_alpha($order_comments))
wc_add_notice(__('Only alphabets are alowed in <strong>Delivery
Instructions</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');
【问题讨论】:
我的回答有帮助吗? 嗨 Bhautik,它似乎确实有效,但现在代码也禁止使用我需要允许的单词之间的空格。可以更改为在单词之间包含空格吗?非常感谢! 我找到了答案!将 Order cmets 行更改为以下内容: if ( !empty( trim( $order_cmets ) ) && !ctype_alpha(str_replace(' ', '', $order_cmets)) ) 欢迎...很高兴为您提供帮助。如果这个答案对你有帮助,那么你可以accept 答案,如果你喜欢/想要你也可以upvote 答案,谢谢。 【参考方案1】:改变这个条件
if (empty(trim($order_comments)) || !ctype_alpha($order_comments))
到这里
if ( !empty( trim( $order_comments ) ) && !ctype_alpha( $order_comments ) )
完整代码
/* remove special charaters from checkout ? */
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');
$order_comments = filter_input(INPUT_POST, 'order_comments');
$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');
if ( !empty( trim( $order_comments ) ) && !ctype_alpha( $order_comments ) )
wc_add_notice(__('Only alphabets are alowed in <strong>Delivery Instructions</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');
【讨论】:
以上是关于WooCommerce 订单评论 - 禁用特殊字符的使用的主要内容,如果未能解决你的问题,请参考以下文章