Создайте пользовательскую форму входа в woocommerce с проверкой OTP - PullRequest
0 голосов
/ 19 июня 2020

Я создаю пользовательскую форму входа в woocommerce с проверкой OTP. Вот мой код

add_action( 'woocommerce_customer_register_form', 'devsol_customer_register_form' );
function devsol_customer_register_form() { 
    if ( is_account_page() ) { 
        if (isset( $_POST['hxs_name'] ) && empty( $_POST['hxs_name'])) {
            wc_print_notice( __( '<strong>Error:</strong> Name is required', 'woocommerce' ), 'error' ); 
        }
        if (isset( $_POST['hxs_phone'] ) && empty( $_POST['hxs_phone'])) {
            wc_print_notice( __( '<strong>Error:</strong> Mobile Number is required', 'woocommerce' ), 'error' );
            }
        if ((!preg_match('/^[0-9]{11}$/D', $_POST['hxs_phone'] )) && !empty( $_POST['hxs_phone'])) {
            wc_print_notice( __( '<strong>Error Format:</strong> 03038518000 .', 'woocommerce' ), 'error' ); 
            }  
    ?>

    <div id="devsol-register">
        <h3 class="register-label"><?php esc_html_e( 'Regiser & Get Discount', 'woocommerce' ); ?></h3> 
        <form class="woocommerce-form woocommerce-form-login login" method="post">
        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
            <label for="hxs_name"><?php esc_html_e( 'Name', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
            <input type="text" class="woocommerce-Input woocommerce-Input--text input-text" name="hxs_name" id="hxs_name" autocomplete="hxs_name" placeholder="Your Full Name" value="<?php echo ( ! empty( $_POST['hxs_name'] ) ) ? esc_attr( wp_unslash( $_POST['hxs_name'] ) ) : ''; ?>"/>
        </p>
        <p class="woocommerce-form-row woocommerce-form-row--wide form-row form-row-wide">
            <label for="hxs_phone"><?php esc_html_e( 'Mobile Number', 'woocommerce' ); ?>&nbsp;<span class="required">*</span></label>
            <input type="tel" class="woocommerce-Input woocommerce-Input--text input-text" name="hxs_phone" id="hxs_phone" autocomplete="hxs_phone" placeholder="03038518000" value="<?php echo ( ! empty( $_POST['hxs_phone'] ) ) ? esc_attr( wp_unslash( $_POST['hxs_phone'] ) ) : ''; ?>"/>
        </p> 
        <button type="submit" value="submit" class="woocommerce-Button button devsol-button">Send OTP</button>
        </form>
    </div>    

    <?php 
        if ( !empty($_POST['hxs_name']) && !empty($_POST['hxs_phone']) && preg_match('/^[0-9]{11}$/D', $_POST['hxs_phone'] ) ) {
            $customer_name = sanitize_text_field( $_POST['hxs_name'] );
            $customer_phone = sanitize_text_field( $_POST['hxs_phone'] );
            $api_token = "*******";
            $api_secret = "*******";
            $to = $customer_phone;
            $from = "SMS Alert";
            $code = rand ( 1000 , 9999 );
            $_SESSION['otp'] = $code;
            $message = "Dear " .$customer_name. ", your 4 digit code is ".$code;
            $url = "http://sms.devsol.pk/plain";
            $parameters = "api_token=".urlencode($api_token)."&api_secret=".urlencode($api_secret)."&to=".$to."&from=".urlencode($from)."&message=".urlencode($message).""; 
            $ch = curl_init();
            $timeout  =  30;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
            curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
            $response = curl_exec($ch);
            curl_close($ch);?>
        }
    }     
}

С приведенным выше кодом я успешно получил 4 di git OTP, но я застрял в том, как получить ввод OTP от клиента и проверить клиента?

Кто-нибудь, пожалуйста, помогите.

1 Ответ

0 голосов
/ 19 июня 2020

Вы должны подтвердить свой одноразовый пароль, поместив код в Проверку. Обычно это находится на странице оформления заказа.

...