Redirect to a Different Thank You Page based on Payment Method

In this tutorial I will show how to perform a redirect to a custom order received paged depending on a payment gateway used by customer.

Bellow is the code snippet that allows to do that. The code can be inserted to your current or child theme functions.php file or to a custom plugin.

/**
 * @snippet       Custom Order Received Redirect based on Payment Gateway
 * @author        Misha Rudrastyh
 * @url           https://rudrastyh.com/woocommerce/custom-thank-you-page-based-on-payment-gateway.html
 */
add_action( 'template_redirect', 'rudr_order_received_custom_payment_redirect');

function rudr_order_received_custom_payment_redirect(){
		
	// do nothing if we are not on the order received page
	if( ! is_wc_endpoint_url( 'order-received' ) || empty( $_GET[ 'key' ] ) ) {
		return;	
	}
		
	// Get the order ID
	$order_id = wc_get_order_id_by_order_key( $_GET[ 'key' ] );
	
	// Get an instance of the WC_Order object
	$order = wc_get_order( $order_id );
   
	// Now we can check what payment method was used for order
	if( 'cod' === $order->get_payment_method() ) {
		// if cash of delivery, redirecto to a custom thank you page
		wp_safe_redirect( site_url( '/custom-page/' ) );
		exit; // always exit
	}

}

Do not forget to replace cod with the payment gateway ID you would like to check, if you don’t know where to get it, I recommend to add an additional payment gateway ID column to the payment gateways table.

If you want to perform a redirect outside of your website, please change the function wp_safe_redirect() to wp_redirect() or add a safe redirect rule.

If you need to check against multiple payment gateways, I think the best way is to do it with the help of in_array() function.

$payment_methods_to_check = array(
	'cod',
	'cheque',
);

if( in_array( $order->get_payment_method(), $payment_methods_to_check ) ) {

Another example – what if we have multiple conditions and have to redirect “payment method 1” to “custom page 1”, “payment method 2” to “custom page 2” etc.

if( 'payment-method-1' === $order->get_payment_method() ) {
	wp_safe_redirect( site_url( '/custom-page-1/' ) );
	exit;
}

if( 'payment-method-2' === $order->get_payment_method() ) {
	wp_safe_redirect( site_url( '/custom-page-2/' ) );
	exit;
}

if( 'payment-method-3' === $order->get_payment_method() ) {
	wp_safe_redirect( site_url( '/custom-page-3/' ) );
	exit;
}

Need custom coding help with WooCommerce payment gateways? – Contact me.

Misha Rudrastyh

Misha Rudrastyh

I develop websites since 2008, so it is total of 13 years of experience, oh my gosh. Most of all I love love love to create websites with WordPress and Gutenberg, some ideas and thoughts I share throughout my blog.

Need some developer help? Contact me

Follow me on Twitter