Support

Account

Home Forums ACF PRO Custom fields for registration woocommerce Reply To: Custom fields for registration woocommerce

  • Well, with you advices, it seems to work.

    If someone is interested :

    /**
     * Add custom fields to user / checkout
     */
    add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
    
    function my_custom_checkout_field( $checkout ) {
    
        echo '<div id="bv_custom_checkout_field"><h2>Measurements</h2>';
    
        /* Weight */
        woocommerce_form_field( 'weight_customer', array(
            'type'          => 'text',
            'class'         => array('my-class form-row-wide'),
            'label'         => __('Your weight'),
            'placeholder'   => __('Your weight'),
        ), get_user_meta(  get_current_user_id(),'weight_customer' , true  ) );
    
        echo '</div>';
    }
    
    /**
     * Verification 
     */
    add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
    
    function my_custom_checkout_field_process() {
        // Check 
        if ( ! $_POST['weight_customer'] )
            wc_add_notice( __( 'Do not forget weight.' ), 'error' );
    }
    
     * Update field
     */
    add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
    
    function my_custom_checkout_field_update_order_meta( $order_id ) {
        if ( ! empty( $_POST['weight_customer'] ) ) {
            update_user_meta( get_current_user_id(), 'weight_customer', sanitize_text_field( $_POST['weight_customer'], '' ));
        }
    }

    I just don’t know how to tell him that it is a number (the weight) but it works.