【问题标题】:WooCommerce Phone Number Validation Before Place Order下订单前的 WooCommerce 电话号码验证
【发布时间】:2018-10-07 04:35:16
【问题描述】:

我正在尝试将客户的电话号码转换为 921234456 格式。

这是我在functions.php中的代码

// Format phone number in this format: 923131234567
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    global $woocommerce;

  /**
     *Format phone number in this format: 923131234567
     * If no country code is provided, by default 92 (Pakistan) country code is added.
     * @param $phone
     * @return string
     */
    $phone = $_POST['billing_phone']

    function formatPhoneNumber($phone]) {
        $phone = trim($phone);
        $phone = str_replace([' ','-','_'],'',$phone);
        if(empty($phone)) {
            return NULL;
        }

        $phone = ltrim(ltrim($phone, '0'),'+');
        if(strlen($phone) <= 11) {
            $phone = '92' . ltrim($phone,0);
        }

        return $phone;
    }
}

【问题讨论】:

    标签: php wordpress woocommerce checkout orders


    【解决方案1】:

    使用此代码,我认为它会正常工作。谢谢

    function formatPhoneNumber($phone) {
        $phone = trim($phone);
        $phone = str_replace([' ','-','_'],'',$phone);
        if(empty($phone)) {
            return NULL;
        }
    
        $phone = ltrim(ltrim($phone, '0'),'+');
        if(strlen($phone) < 11) {
            $phone = '92' . ltrim($phone);
        }
    
        return $phone;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-04
      • 2017-03-13
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多