Reputation: 41
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/
Upvotes: 4
Views: 7876
Reputation: 253763
Using a custom function hooked in template_redirect
action hook, here is the way to redirect customer to a custom page for a specific payment method (using the conditional function **is_wc_endpoint_url()
):
add_action( 'template_redirect', 'thankyou_custom_payment_redirect');
function thankyou_custom_payment_redirect(){
global $wp;
$payment_id = 'cod'; // <=== Here define the targeted payment method ID
$endpoint = 'order-received'; // Order received endpoint
if ( is_wc_endpoint_url($endpoint) && isset($wp->query_vars[$endpoint]) ) {
$order_id = absint($wp->query_vars[$endpoint]); // Get the order ID
$order = wc_get_order( $order_id ); // Get the WC_Order object
$order_key = $order->get_order_key(); // The order key if needed
if ( $payment_id === $order->get_payment_method() && in_array($order->get_status(), wc_get_is_paid_statuses()) ) {
wp_redirect( home_url( '/custom-page/' ) ); // Set HERE your custom URL redirection path
exit(); // always exit
}
}
}
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
Addition: How to get the Payment Gateway ID (WC settings > Checkout tab):
Upvotes: 4
Reputation: 11
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
}
}
}
Upvotes: 0