WooCommerce order-received redirect based on payment method

7.5k views Asked by At

Usually in WooCommerce submitted orders are redirect to /order-received/ once payment is completed.

Is it possible to redirect customer to a custom page for a particular payment method?

For example:

Payment method 1 -> /order-received/
Payment method 2 -> /custom-page/
Payment method 3 -> /order-received/
2

There are 2 answers

1
LoicTheAztec On

With a custom function hooked in template_redirect action hook using the conditional function is_wc_endpoint_url() and targeting your desired payment method to redirect customer to a specific page:

add_action( 'template_redirect', 'thankyou_custom_payment_redirect');
function thankyou_custom_payment_redirect(){
    if ( is_wc_endpoint_url( 'order-received' ) ) {
        global $wp;

        // Get the order ID
        $order_id =  intval( str_replace( 'checkout/order-received/', '', $wp->request ) );

        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );

        // Set HERE your Payment Gateway ID
        if( $order->get_payment_method() == 'cheque' ){
            
            // Set HERE your custom URL path
            wp_redirect( home_url( '/custom-page/' ) );
            exit(); // always exit
        }
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.

How to get the Payment Gateway ID (WC settings > Checkout tab):

enter image description here

1
Peter Silva-Jankowski On

A small correction.

"exit" needs to be within the last condition

add_action( 'template_redirect', 'thankyou_custom_payment_redirect');
    function thankyou_custom_payment_redirect(){
    if ( is_wc_endpoint_url( 'order-received' ) ) {
        global $wp;

        // Get the order ID
        $order_id =  intval( str_replace( 'checkout/order-received/', '', $wp->request ) );

        // Get an instance of the WC_Order object
        $order = wc_get_order( $order_id );

        // Set HERE your Payment Gateway ID
        if( $order->get_payment_method() == 'cheque' ){

            // Set HERE your custom URL path
            wp_redirect( home_url( '/custom-page/' ) );
            exit(); // always exit
        }
    }
}