下订单前WooCommerce电话号码验证

时间:2018-04-26 12:58:48

标签: php wordpress woocommerce checkout orders

我正在尝试转换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;
    }
}

1 个答案:

答案 0 :(得分:0)

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

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