How to add custom fields to WooCommerce registration form

后端 未结 3 779
暗喜
暗喜 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 10:06

    Adding Woocommerce registration form custom fields using filter hook woocommerce_forms_field and saving data with nonce verification.

    /**
     * Adding more fields to Woocommerce Registration form and My account page
     */
    
    function how_woocommerce_registration_form_fields() {
        return apply_filters( 'woocommerce_forms_field', array(
                'billing_company' => array(
                        'type'        => 'text',
                        'label'       => __( 'Company Name', ' how' ),
                        'required'    => false,
                ),
                'how_user_job_title' => array(
                    'type'        => 'text',
                    'label'       => __( 'Job Title', ' how' ),
                    'required'    => false,
                ),
                'how_user_industry'     => array(
                    'type'    => 'select',
                    'label'   => __( 'Industry', 'how' ),
                    'options' => array(
                            ''                                      => __( 'Select an Industry', 'how' ),
                            'Lending'                       => __( 'Lending', 'how' ),
                            'Real Estate'               => __( 'Real Estate', 'how' ),
                            'Investment'                => __( 'Investment', 'how' ),
                            'Servicing'                     => __( 'Servicing', 'how' ),
                            'Other'                             => __( 'Other', 'how' ),
                            'Mortgage Servicing'    => __( 'Mortgage Servicing', 'how' ),
                            'Mortgage Lending'      => __( 'Mortgage Lending', 'how' ),
                    )
                )
        ) );
    }
    function how_woocommerce_edit_registration_form() {
        $fields = how_woocommerce_registration_form_fields();
        foreach ( $fields as $key => $field_args ) {
                woocommerce_form_field( $key, $field_args );
        }
    }
    add_action( 'woocommerce_register_form', 'how_woocommerce_edit_registration_form', 15 );
    
    /**
     * Save registration form custom fields
     */
    
    function wooc_save_extra_register_fields( $customer_id ) {
        if( wp_verify_nonce( sanitize_text_field( $_REQUEST['woocommerce-register-nonce'] ), 'woocommerce-register' ) ) {
            if ( isset( $_POST['billing_company'] ) ) {
                update_user_meta( $customer_id, 'billing_company', sanitize_text_field( $_POST['billing_company'] ) );
                update_user_meta( $customer_id, 'shipping_company', sanitize_text_field( $_POST['billing_company'] ) );
            }
            if ( isset( $_POST['how_user_job_title'] ) ) {
                update_user_meta( $customer_id, 'how_user_job_title', sanitize_text_field( $_POST['how_user_job_title'] ) );
            }
            if ( isset( $_POST['how_user_industry'] ) ) {
                update_user_meta( $customer_id, 'how_user_industry', sanitize_text_field( $_POST['how_user_industry'] ) );
            }
        }
    }
    add_action( 'woocommerce_created_customer', 'wooc_save_extra_register_fields' );
    

提交回复
热议问题