duanjianlu0506 2017-03-17 00:20
浏览 63
已采纳

字母字符仅用于WooCommerce中的结算和发货名称

How can I restrict only alphabet characters in the billing first name, billing last name, shipping first name and shipping last name in WooCommerce checkout?

I am trying the following in the my Child Theme's functions.php file and it is causing the checkout section to not appear when I go to checkout in WooCommerce. Please advise.

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'
        )
    );
}
  • 写回答

1条回答 默认 最新

  • donglun7151 2017-03-17 03:35
    关注

    You can do this by validating checkout form submit at server side by using woocommerce_checkout_process hook, and PHP ctype_alpha() function.

    Here is the code:

    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');
            }
        }
    }
    

    Code goes in functions.php file of your active child theme (or theme). Or also in any plugin PHP files.
    Code is tested and works.

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line