4

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/
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
McNaldo
  • 41
  • 1
  • 3

2 Answers2

4

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • your action will not worked correctly, because you always **exit** from `template_redirect`, you need exit after you redirected to another page `wp_redirect`. – Brotheryura Jul 29 '20 at 02:47
0

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
        }
    }
}
  • Interested to know why my amendment was down-voted without comment, given that the original solution will produce a 500 error across an entire Woocommerce installation upon second action. – Peter Silva-Jankowski Jul 24 '18 at 18:36