【问题标题】:Woocommerce-If billing phone is already exists checkWoocommerce-如果计费电话已经存在,请检查
【发布时间】:2016-03-22 08:03:42
【问题描述】:

我要求 woocommerce 中的计费电话像电子邮件一样验证。每当我们想在 woocommerce 中创建具有相同电子邮件 ID 的帐户时,它都会抛出错误电子邮件已存在。我希望在计费电话中具有相同的此功能。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    这对我有用 用你的表名更改 wp_usermeta

     add_action( 'woocommerce_register_form', 'wooc_extra_register_fields' );
    
     function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
    
    global $wpdb;
    $billing_phone =$_POST['billing_phone'];
    
      if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
             $validation_errors->add( 'billing_first_name_error', __( ' First name is required!', 'woocommerce' ) );
      }
      if ( isset( $_POST['billing_last_name'] ) && empty( $_POST['billing_last_name'] ) ) {
             $validation_errors->add( 'billing_last_name_error', __( ' Last name is required!.', 'woocommerce' ) );
      }
     if ( isset($billing_phone ) && empty( $billing_phone ) ) {
             $validation_errors->add( 'billing_phone_error', __( ' Prone is required!.', 'woocommerce' ) );
      }
    $results = $wpdb->get_results('select * from `wp_usermeta` where meta_key = "billing_phone" and meta_value = "'.$billing_phone.'"');
    if ( $results ) {
                     $validation_errors->add( 'billing_phone_error', __( 'Phone number already exists..', 'woocommerce' ) );
    }
      return $validation_errors;
    }
    

    【讨论】:

      【解决方案2】:

      这将是您的解决方案:

      add_filter('woocommerce_new_customer_data', 'risbl_custom_customer_data', 10 );
      
      function risbl_custom_customer_data() {
      
        global $wpdb;
        $billing_phone = $_POST['billing_phone'];
      
          $results = $wpdb->get_results('select * from `wp_usermeta` where meta_key = "billing_phone" and meta_value = "'.$billing_phone.'"');
          if ( $results ) {
              wc_add_notice( __( 'Phone number already exists.' ), 'error' );
          }
      
      }
      

      【讨论】:

      • 致命错误:在非对象上调用成员函数 get_results() - 它显示了这个..我已将您的代码粘贴到主题 function.php
      • 不能使用 WP_Error 类型的对象作为数组..使用你更新的代码
      • 实际上我在注册时使用了额外的注册字段 fname、lname 和电话..现在它显示错误电话号码已经存在但错误:错误:无法注册您...如果您请联系我们继续有问题。--这也...用于新鲜注册
      【解决方案3】:

      试试这个:

      function wc_validate_phone_number() {
         $phone = (isset( $_POST['billing_phone'] ) ? trim( 
         $_POST['billing_phone'] ) : '');
         if ( ! preg_match( '/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/',
         $phone ) ) {
            wc_add_notice( __( 'Invalid Phone Number. Please enter with a valid 
            phone number. Eg: (123) 456 7890' ), 'error' );
         }
      }
      

      【讨论】:

      • 其实我在找电话不存在检查不验证电话号码
      【解决方案4】:

      代码完美运行:

        add_filter('woocommerce_billing_fields',
       'phone', 10 );
      
        function phone() {
      
        global $wpdb;
        $billing_phone = $_POST['billing_phone'];
      
      $results = $wpdb->get_results('select * from `wp_usermeta` where meta_key = "billing_phone" and meta_value = "'.$billing_phone.'"');
      if ( $results ) {
          wc_add_notice( __( 'Phone number already exists.' ), 'error' );
      }
      
      }
      

      【讨论】:

      • 这个确切的答案是 5 年前发布的,包含相同的 SQL 注入漏洞。
      • 挂钩 woocommerce_billing_fields 将在您下订单时起作用,它也会检查。从浏览器渲染php文件,然后如何注入。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 2021-03-02
      • 1970-01-01
      • 2020-10-05
      • 2018-03-09
      • 1970-01-01
      • 2021-10-27
      相关资源
      最近更新 更多