Customize the WooCommerce order received thank you page

Customise the WooCommerce thank-you page without template overrides

The order received page is seen by every customer right after paying, which makes it a good place for a personal message, pickup instructions, a discount for the next order or a few related products. WooCommerce has hooks around its content, so none of this needs a copy of the thankyou.php template.

Problem: After checkout, customers should see a personal thank-you message and three products related to what they just bought, without overriding WooCommerce templates.

Solution: Print the message on woocommerce_before_thankyou, the products on woocommerce_thankyou, and change the default text with the woocommerce_thankyou_order_received_text filter.

A personal message

<?php
add_action( 'woocommerce_before_thankyou', function ( $order_id ) {
    $order = wc_get_order( $order_id );

    if ( ! $order || $order->has_status( 'failed' ) ) {
        return;
    }

    printf(
        '<div class="ha-thankyou"><h2>%s</h2><p>%s</p></div>',
        esc_html( sprintf( __( 'Thank you, %s!', 'textdomain' ), $order->get_billing_first_name() ) ),
        esc_html( sprintf( __( 'Order #%s is confirmed. We will email you when it ships.', 'textdomain' ), $order->get_order_number() ) )
    );
} );

Use get_order_number(), not get_id(): plugins for sequential order numbers change the number shown to customers, not the ID. Failed payments land on the same page, so the check avoids thanking a customer whose payment did not go through.

The default sentence

<?php
add_filter( 'woocommerce_thankyou_order_received_text', function ( $text, $order ) {
    return __( 'We have received your order and are already preparing it.', 'textdomain' );
}, 10, 2 );

Related products

<?php
add_action( 'woocommerce_thankyou', function ( $order_id ) {
    $order = wc_get_order( $order_id );
    if ( ! $order ) {
        return;
    }

    $bought = array();
    foreach ( $order->get_items() as $item ) {
        $bought[] = $item->get_product_id();
    }

    if ( ! $bought ) {
        return;
    }

    $related = wc_get_related_products( $bought[0], 3, $bought ); // exclude what was just bought

    if ( ! $related ) {
        return;
    }

    echo '<section class="ha-thankyou-related"><h2>' . esc_html__( 'You may also like', 'textdomain' ) . '</h2><ul class="products columns-3">';
    foreach ( $related as $product_id ) {
        $product = wc_get_product( $product_id );
        if ( $product && $product->is_visible() ) {
            printf(
                '<li class="product"><a href="%s">%s<h3>%s</h3></a>%s</li>',
                esc_url( $product->get_permalink() ),
                $product->get_image( 'woocommerce_thumbnail' ),
                esc_html( $product->get_name() ),
                wp_kses_post( $product->get_price_html() )
            );
        }
    }
    echo '</ul></section>';
}, 20 );

The woocommerce_thankyou action runs after the order details table; priority 20 places the products below anything a payment gateway prints there (gateways use the same hook for bank details or payment instructions).

Tracking a conversion once

The page can be reloaded or opened again from the order email. Mark the order when a tracking script or a one-time action has run:

<?php
if ( ! $order->get_meta( '_ha_conversion_tracked' ) ) {
    // print the tracking code...
    $order->update_meta_data( '_ha_conversion_tracked', 1 );
    $order->save();
}

NOTE: The thank-you page URL contains the order key, and anybody with the link can see the order details. Do not print more personal data there than WooCommerce already shows, and never sensitive data such as full addresses of other customers or internal notes.