How to Redirect to Different Thank You Pages Based on WooCommerce Payment Method

WooCommerce allows developers to create custom payment gateways, which may return specific information or require further processing upon a successful transaction. In such cases, redirecting the user to a custom thank you page instead of the default “Order Received” page is often essential for a seamless user experience.

In this article, we will demonstrate how to implement conditional redirects to different thank you pages based on the customer’s selected payment method.

Core Implementation Code

In the code snippet below, we use the template_redirect hook to check if the current page is the WooCommerce “Order Received” (thank you) endpoint. If it is, we retrieve the order instance and its payment method. Based on this method, we then perform a safe redirect to our desired custom page.

add_action( 'template_redirect', 'rudr_order_received_custom_payment_redirect');

function rudr_order_received_custom_payment_redirect(){
                
        // Check if we are on the 'order-received' endpoint and have an order key
        if( ! is_wc_endpoint_url( 'order-received' ) || empty( $_GET[ 'key' ] ) ) {
                return; 
        }
                
        // Get the order ID from the order key
        $order_id = wc_get_order_id_by_order_key( $_GET[ 'key' ] );
        
        // Retrieve the WC_Order object
        $order = wc_get_order( $order_id );
   
        // Check the payment method of the order
        if( 'cod' === $order->get_payment_method() ) {
                // If the payment method is 'Cash on Delivery', redirect to a custom thank you page
                wp_safe_redirect( site_url( '/custom-thank-you-page-cod/' ) );
                exit;
        }

}

This approach is highly flexible. You can use any data available within the order object (such as order total, customer role, or specific products purchased) to determine the redirect destination. For example, you could redirect customers with a total spend exceeding $10,000 to a “VIP Upgrade” page, offering them immediate access to premium benefits.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *