自定义注册字段WooCommerce

时间:2017-09-23 作者:Nav

嗨,我正在尝试为woocommerce创建一个自定义注册页面,直到验证它正常工作,但当我尝试在数据库中插入数据时,什么都没有发生。这是我正在使用的示例代码\'

<?php

function wooc_extra_register_fields() {

       ?>



       <p class="form-row form-row-first">

       <label for="reg_billing_first_name"><?php _e( \'First name\', \'woocommerce\' ); ?><span class="required">*</span></label>

       <input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST[\'billing_first_name\'] ) ) esc_attr_e( $_POST[\'billing_first_name\'] ); ?>" />

       </p>



       <p class="form-row form-row-last">

       <label for="reg_billing_last_name"><?php _e( \'Last name\', \'woocommerce\' ); ?><span class="required">*</span></label>

       <input type="text" class="input-text" name="billing_last_name" id="reg_billing_last_name" value="<?php if ( ! empty( $_POST[\'billing_last_name\'] ) ) esc_attr_e( $_POST[\'billing_last_name\'] ); ?>" />

       </p>



       <div class="clear"></div>



       <p class="form-row form-row-wide">

       <label for="reg_billing_phone"><?php _e( \'Phone\', \'woocommerce\' ); ?><span class="required">*</span></label>

       <input type="Number" class="input-text" name="billing_phone" id="reg_billing_phone" value="<?php if ( ! empty( $_POST[\'billing_phone\'] ) ) esc_attr_e( $_POST[\'billing_phone\'] ); ?>" />

       </p>


        <p class="form-row form-row-wide">

       <label for="reg_num"><?php _e( \'MCI Registration Num\', \'woocommerce\' ); ?><span class="required">*</span></label>

       <input type="text" class="input-text" name="reg_num" id="reg_num" value="<?php if ( ! empty( $_POST[\'reg_num\'] ) ) esc_attr_e( $_POST[\'reg_num\'] ); ?>" />

       </p>

        <p class="form-row form-row-wide">

       <label for="Year_of_reg"><?php _e( \'Year of Registration\', \'woocommerce\' ); ?><span class="required">*</span></label>

       <input type="Number" class="input-text" name="Year_of_reg" id="Year_of_reg" value="<?php if ( ! empty( $_POST[\'Year_of_reg\'] ) ) esc_attr_e( $_POST[\'Year_of_reg\'] ); ?>" />

       </p>


        <p class="form-row form-row-wide">

       <label for="qualification"><?php _e( \'Qualification\', \'woocommerce\' ); ?><span class="required">*</span></label>

       <input type="text" class="input-text" name="qualification" id="qualification" value="<?php if ( ! empty( $_POST[\'qualification\'] ) ) esc_attr_e( $_POST[\'qualification\'] ); ?>" />

       </p>




       <?php

}



add_action( \'woocommerce_register_form_start\', \'wooc_extra_register_fields\' );



?>

\'<?php

function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) 
{

       if ( isset( $_POST[\'billing_first_name\'] ) && empty( $_POST[\'billing_first_name\'] ) ) 
       {

              $validation_errors->add( \'billing_first_name_error\', __( \'<strong>Error</strong>: First name is required!\', \'woocommerce\' ) );

       }



       if ( isset( $_POST[\'billing_last_name\'] ) && empty( $_POST[\'billing_last_name\'] ) ) 
       {

              $validation_errors->add( \'billing_last_name_error\', __( \'<strong>Error</strong>: Last name is required!.\', \'woocommerce\' ) );

       }



       if ( isset( $_POST[\'billing_phone\'] ) && empty( $_POST[\'billing_phone\'] ) ) 
       {

              $validation_errors->add( \'billing_phone_error\', __( \'<strong>Error</strong>: Phone is required!.\', \'woocommerce\' ) );

       }


       if ( isset( $_POST[\'reg_num\'] ) && empty( $_POST[\'reg_num\'] ) ) 
       {

              $validation_errors->add( \'reg_num_error\', __( \'<strong>Error</strong>: Registration Number is required! if you are facing any problem give us a call or contact us on support@ozmant.com .\', \'woocommerce\' ) );

       }


       if ( isset( $_POST[\'Year_of_reg\'] ) && empty( $_POST[\'Year_of_reg\'] ) ) 
       {

              $validation_errors->add( \'year_of_reg_error\', __( \'<strong>Error</strong>: MCI Year of Registration is required!.\', \'woocommerce\' ) );

       }




    }


}


add_action( \'woocommerce_register_post\', \'wooc_validate_extra_register_fields\', 10, 5 );




function wooc_save_extra_register_fields( $customer_id ) {

   if ( isset( $_POST[\'billing_first_name\'] ) ) {
        // WordPress default first name field.
        update_user_meta( $customer_id, \'first_name\', sanitize_text_field( $_POST[\'billing_first_name\'] ) );

        // WooCommerce billing first name.
        update_user_meta( $customer_id, \'billing_first_name\', sanitize_text_field( $_POST[\'billing_first_name\'] ) );
    }

    if ( isset( $_POST[\'billing_last_name\'] ) ) {
        // WordPress default last name field.
        update_user_meta( $customer_id, \'last_name\', sanitize_text_field( $_POST[\'billing_last_name\'] ) );

        // WooCommerce billing last name.
        update_user_meta( $customer_id, \'billing_last_name\', sanitize_text_field( $_POST[\'billing_last_name\'] ) );
    }

    if ( isset( $_POST[\'billing_phone\'] ) ) {
        // WooCommerce billing phone
        update_user_meta( $customer_id, \'billing_phone\', sanitize_text_field( $_POST[\'billing_phone\'] ) );
    }

    if ( isset( $_POST[\'reg_num\'] ) ) {
        // WooCommerce billing address
        update_user_meta( $customer_id, \'reg_num\', sanitize_text_field( $_POST[\'reg_num\'] ) );
    }

    if ( isset( $_POST[\'Year_of_reg\'] ) ) {
        // WooCommerce billing postcode
        update_user_meta( $customer_id, \'Year_of_reg\', sanitize_text_field( $_POST[\'Year_of_reg\'] ) );
    }

    if ( isset( $_POST[\'qualification\'] ) ) {
        // WooCommerce billing city
        update_user_meta( $customer_id, \'qualification\', sanitize_text_field( $_POST[\'qualification\'] ) );
    }


}

add_action( \'woocommerce_created_customer\', \'wooc_save_extra_register_fields\' );
?>

1 个回复
SO网友:TJ152

有一个名为“用户注册WC地址字段”的插件,可以满足您的需要。您可以将帐单/发货字段(名称、地址、公司、电话等)添加到正常的Woocommerce客户注册页面-https://en-gb.wordpress.org/plugins/wc-afour/

结束

相关推荐