How to add custom fields to WooCommerce registration form

后端 未结 3 782
暗喜
暗喜 2020-12-10 09:32

I have seen similar questions but couldn\'t find a solution for me.

I am trying to add custom fields to WooCommerce registration form, specifically first and last na

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 09:54

     'text',
                'required'    => true, // just adds an "*"
                'label'       => 'First name'
            ),
            ( isset($_POST['billing_first_name']) ? $_POST['billing_first_name'] : '' )
        );
        woocommerce_form_field(
            'billing_last_name',
            array(
                'type'        => 'text',
                'required'    => true, // just adds an "*"
                'label'       => 'Last name'
            ),
            ( isset($_POST['billing_last_name']) ? $_POST['billing_last_name'] : '' )
        );
        woocommerce_form_field(
            'billing_phone',
            array(
                'type'        => 'tel',
                'required'    => true, // just adds an "*"
                'label'       => 'Phone'
            ),
            ( isset($_POST['billing_phone']) ? $_POST['billing_phone'] : '' )
        );
     
    }
    /**
     * To validate WooCommerce registration form custom fields.
     */
    add_action( 'woocommerce_register_post', 'misha_validate_fields', 10, 3 );
     
    function misha_validate_fields( $username, $email, $errors ) {
     
        if ( empty( $_POST['billing_first_name'] ) ) {
            $errors->add( 'billing_first_name_error', 'First name is required!' );
        }
        if ( empty( $_POST['billing_last_name'] ) ) {
            $errors->add( 'billing_last_name_error', 'Last name is required!' );
        }
        if ( empty( $_POST['billing_phone'] ) ) {
            $errors->add( 'billing_phone_error', 'Phone is required!' );
        }
     
    }
    /**
     * To save WooCommerce registration form custom fields.
     */
    add_action( 'woocommerce_created_customer', 'misha_save_register_fields' );
     
    function misha_save_register_fields( $customer_id ){
       //First name field
        if ( isset( $_POST['billing_first_name'] ) ) {
            //update_user_meta( $customer_id, 'country_to_visit', wc_clean( $_POST['country_to_visit'] ) );
            update_user_meta($customer_id, 'first_name', sanitize_text_field($_POST['billing_first_name']));
            update_user_meta($customer_id, 'billing_first_name', sanitize_text_field($_POST['billing_first_name']));
        }
        //Last name field
        if (isset($_POST['billing_last_name'])) {
            update_user_meta($customer_id, 'last_name', sanitize_text_field($_POST['billing_last_name']));
            update_user_meta($customer_id, 'billing_last_name', sanitize_text_field($_POST['billing_last_name']));
        }
        // WooCommerce billing phone
        if ( isset( $_POST['billing_phone'] ) ) {
            update_user_meta( $customer_id, 'billing_phone', sanitize_text_field( $_POST['billing_phone'] ) );
        }
     
    }
    ?>
    

提交回复
热议问题