nevercrox.com

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

Pre-Requisites

There are no pre-requisites for this code snippet to work. Keep in mind this solution consists of two code snippets, one to display the content and one to style it.

How To Implement This Solution?

Add these two code snippets as separate entries in either your active child theme’s functions.php file or the Code Snippets plugin.

About The Snippet

In some situations, you might want to redirect users to a different thank you page depending on the product they purchased. This can be useful for delivering specific information best shared on a custom page. This guide shows you how to set up a redirect to a custom thank you page based on the product in the order. Just make sure you have the URL of the custom thank you page and the appropriate product ID ready.

Code Snippet 1( Paste This Code In functions.php or Code Snippets Plugin )
/**
* Snippet Name:     Redirect users to a custom WooCommerce thank you page based on a product bought in the order.
* Snippet Author:   Nevercrox Solutions
*/

add_action( 'template_redirect', 'nevercrox_product_dependant_thank_you_page' );

function nevercrox_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;
      }
   }

}

 

Just Added Blogs

Need A Specifc Blog ?

Request A Blog

Check Out More Related Snippets

0
Would love your thoughts, please comment.x
()
x