Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Adding Custom Field to Woo Checkout Page

  • Hi,

    My demo site: https://shop.posmay.com/

    I’d like to add

    1. The company name field to the checkout page (I’ve read up here that this is added by default, but is not showing up on my shop’s checkout page) and …

    2. A custom text form field to my Woo checkout page to capture the customers VAT/Tax number. Ideally it should show up under the Payment Info section.

    I’ve researched and tried various ways of doing this both using plugins (e.g. Flexible Checkout Fields) and PHP code, but after adding code or adding the field using a plugin I just cannot seem to get it to work/show up.

    I understand that I need to:

    1. Create the custom field
    2. Add it to the checkout page
    3. And make sure it is captured as part of the order meta data

    Ideally I’d like to add it using PHP code (if possible) but am also open to using a plugin.

    Some of the resources that I’ve reviewed and tried:

    https://aovup.com/woocommerce/vat-number-field/
    https://www.businessbloomer.com/woocommerce-add-custom-checkout-field-php/
    https://wordpress.org/support/topic/how-to-insert-conditional-cusotm-form-field-on-checkout-page/

    In case I need to hook it into the checkout page:
    https://www.businessbloomer.com/woocommerce-visual-hook-guide-checkout-page/

    I generally keep “Distraction-free mode” checked … in case that is causing this issue. Should I rather leave that unchecked?

    Please let me know if this is beyond the scope of GP and GB support. Appreciate any guidance and support.

    Update

    I’ve also tried the adding following code to my functions file from: https://developer.woocommerce.com/docs/customizing-checkout-fields-using-actions-and-filters/, which also does not seem to work.

    // ADD CUSTOM FIELD TO CHECKOUT PAGE
    
    /**
     * Add the field to the checkout
     */
     
    add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
    
    function my_custom_checkout_field( $checkout ) {
    
        echo '' . esc_html__( 'My Field' ) . '';
    
        woocommerce_form_field(
            'my_field_name',
            array(
                'type'        => 'text',
                'class'       => array( 'my-field-class form-row-wide' ),
                'label'       => __( 'Fill in this field' ),
                'placeholder' => __( 'Enter something' ),
            ),
            $checkout->get_value( 'my_field_name' )
        );
    
        echo '';
    
    }
    
    /**
     * 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['my_field_name'] ) {
            wc_add_notice( esc_html__( 'Please enter something into this new shiny field.' ), '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['my_field_name'] ) ) {
            $order = wc_get_order( $order_id );
            $order->update_meta_data( 'My Field', sanitize_text_field( $_POST['my_field_name'] ) );
            $order->save_meta_data();
        }
    }
    
    /**
     * 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 '' . esc_html__( 'My Field' ) . ': ' . esc_html( $order->get_meta( 'My Field', true ) ) . '';
    }
    
    add_filter( 'woocommerce_billing_fields', 'wc_npr_filter_phone', 10, 1 );
    
  • Hi there,

    This is somewhat beyond the scope of our support, but we’ll try to assist where possible. 😊

    Have you tried using this plugin:
    https://wordpress.org/plugins/woocommerce-checkout-manager/

  • Hi Alvind,

    Thanks for the suggestion. Really appreciate the help. I’ve just tried that plugin. I did the following:

    1. Installed the plugin
    2. Added a custom field under Additional for “VAT Number”
    3. Made sure that it’s not disabled or hidden
    4. Tested the checkout page in incognito browser – and the field does not show up
    5. Set half the fields under shipping to hidden/disabled … tested again.
    6. All fields are still showing up

    Somehow whatever I try, I cannot change the checkout page fields. It’s very strange.

    Any ideas?

    BTW: I have managed to show/enable the company field. I did not know I had to enable/show it inside the block editor under “Address Fields” settings.

  • Hi there,

    your checkout page is using the Woocommerce checkout block.
    And blocks don’t use the core woocommerce PHP templates and therefore are absent of most of the hooks ( if not all of them ).

    See here for a list of legacy hooks that woo blocks uses:

    https://github.com/woocommerce/woocommerce-blocks/blob/trunk/docs/third-party-developers/extensibility/hooks/migrated-hooks.md

    You may want to consider not using the block, and using the original shortcode

  • Hi David,

    Thanks for your reply. I’ve just added the legacy checkout page layout and the checkout shortcode, which both output the same checkout page design. Wow, compared to the block checkout page, the legacy option is so so ugly (i.e. “un-styled”).

    If possible, I would really love to use the block version, as it looks really slick and professional. Is the block checkout option not the newer and recommended option to use in WooCommerce?

    Is there no way to use the block checkout option and make customisations? Would I need to use the official WooCommerce plugin to do that? I guess this is perhaps a question for Woo support or the Woo forum.

    I do appreciate your help in this forum, as there is definitely a lot to learn about these intricacies.

  • Is the block checkout option not the newer and recommended option to use in WooCommerce?

    It is newer, but less customizing options if you want to modify the template.

    I would recommend checking with Woocommerce’s support, as GP is a PHP theme, we do not have control over blocks from 3rd party plugins, unfortunately.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.