Search code examples
wordpresswoocommercehook-woocommerceelementorcustom-fields

Show custom fields into thank you page woocommerce don't work


I have created a series of custom fields in the woocommerce checkout. They are successfully saved in the order and displayed in the email but I can't get them to appear on the thank you page.

Thank you page is custom but created with Elementor and passed to woocommerce as a specific page. Everything works, I can view order and billing details but not the custom fields. I tried with hook but it fails. Anyone know how I can fix it? Thanks in advance!


Solution

  • Before showing customer fields, you should follow three steps like this:

    1. Add custom meta fields on the checkout page from where customers can pass their message
    2. Save your custom field data when placing an order from the checkout page
    3. Show custom fields value on the welcome page

    Like this:

    /**
    * Display Custom Checkout Fields Data on Thankyou page
    */
    function custom_display_order_data( $order_id ){  ?>
        <table class="shop_table shop_table_responsive additional_info">
            <tbody>
                <tr>
                    <th><?php _e( 'Your Custom Field Message:' ); ?></th>
                    <td><?php echo get_post_meta( $order_id, 'your_custom_field_name', true ); ?></td>
                </tr>
            </tbody>
        </table>
    <?php }
    add_action( 'woocommerce_thankyou', 'custom_display_order_data', 20 );
    

    My sample code reference is - woocommerce add line item meta to main order meta

    You should open the above link and code/past code in your active theme's functions.php.