• Resolved curiousforge

    (@curiousforge)


    I’m trying to add additional email fields to the checkout page. I’ve followed the process listed on the “Customizing checkout fields using actions and filters” page on the WooCommerce website, however the custom field will not display. We’re using the checkout blocks as our checkout page. The code I’m using is below:

    add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' ); 
    
    function custom_override_checkout_fields( $fields ) { $fields['billing']['additional_email'] = array( 
    'label' => __('Email', 'woocommerce'), 
    'placeholder' => _x('Email', 'placeholder', 'woocommerce'), 
    'required' => false, 
    'class' => array('form-row-wide'), 
    'clear' => true ); 
    
    return $fields; } 
    
    add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 ); 
    
    function my_custom_checkout_field_display_admin_order_meta($order){ 
    echo '<p><strong>'.__('Additional Participant Email').':</strong> ' . get_post_meta( $order->get_id(), '_additional_email', true ) . '</p>'; } 

    Ideally, the additional email field would appear in the “Contact Information” section. We’re selling classes with our WooCommerce store, so these additional email fields will be used to send confirmation to all of the participants who will be taking the class.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Some steps are missing from your code. Go back to:
    https://woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/
    and look at the validation and save functions. These are near the bottom of the page.

    Thread Starter curiousforge

    (@curiousforge)

    The following code works for the default checkout pages, however not for the checkout blocks. I’m in contact with WooCommerce support, however I haven’t received a clear solution yet. Here’s the code for the default checkout pages, in case it’s of use to anyone…

    /**
     * Add the field to the checkout
     */
    add_action( 'woocommerce_after_checkout_billing_form', 'my_custom_checkout_field' );
    
    function my_custom_checkout_field( $checkout ) {
    
        echo '<div id="my_custom_checkout_field"><h2>' . __('Additional Emails') . '</h2>';
    
        woocommerce_form_field( 'additional_emails', array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __('Enter Additonal Participant Emails'),
            'placeholder'   => __('Enter Emails'),
            ), $checkout->get_value( 'additional_emails' ));
    
        echo '</div>';
    
    }
    
    /**
     * Process the checkout
     */
    add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
    
    function my_custom_checkout_field_process() {
        // Check if set, if its not set add an error.
        if ( ! $_POST['additional_emails'] )
            wc_add_notice( __( 'Please enter the emails of additional participants.' ), 'error' );
    }
    
    /**
     * Update the order meta with field value
     */
    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['additional_emails'] ) ) {
            update_post_meta( $order_id, 'Additional Emails', sanitize_text_field( $_POST['additional_emails'] ) );
        }
    }
    
    /**
     * Display field value on the order edit page
     */
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
    
    function my_custom_checkout_field_display_admin_order_meta($order){
        echo '<p><strong>'.__('Additional Emails').':</strong> ' . get_post_meta( $order->id, 'Additional Emails', true ) . '</p>';}
    Plugin Support mouli a11n

    (@mouli)

    @curiousforge
    We can confirm that, currently the code snippet you offer does not work with the Checkout Block.
    It does however work with the default shortcode so, at this point we recommend that you use the shortcode version of the Checkout.
    [woocommerce_checkout]

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Email Field Not Appearing in Checkout’ is closed to new replies.