Thank You Page

WooCommerce Redirect To Different Thank You Page Based On Product In Order

Pre-Requisites

You’ll need to have created a custom thank you page and have the URL ready. You’ll also need to the product ID of the product you want to trigger the custom thank you page.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About This Solution

In some cases, you may wish to redirect users to a different thank you page based on a product purchased in the order. Perhaps there’s specific information you need to tell the customer about and the only practical way is through a custom page. This guides shows you how you can redirect users to a custom thank you page based on a product purchased in the order. You will need the custom thank you page URL and appropriate product ID ready.

/**
* Snippet Name:     Redirect users to a custom WooCommerce thank you page based on a product bought in the order.
* Snippet Author:   ecommercehints.com
*/

add_action( 'template_redirect', 'ecommercehints_product_dependant_thank_you_page' );

function ecommercehints_product_dependant_thank_you_page(){

   if( !is_wc_endpoint_url( 'order-received' ) || empty( $_GET['key'] ) ) {
      return;
   }

   $order_id = wc_get_order_id_by_order_key( $_GET['key'] );
   $order = wc_get_order( $order_id );

   foreach( $order->get_items() as $item ) {
      if( $item['product_id'] == 123 ) { // product id here
         wp_redirect( 'YOUR URL' ); // your custom thank you page url here
         exit;
      }
   }

}

Snippet Benefits

  • Tailor your thank you page based on a particular product in the order.

100 WooCommerce Conversion Rate Optimisation Tips

This field is for validation purposes and should be left unchanged.

Let’s collaborate!

Need to outsource WordPress development?

Join forces with UnlimitedWP for an outsourced white label web development service you can truly rely on.

First Timer Here?

Sign up to receive 10% off on your first month with us.

26027
WELCOME OFFER