WooCommerce is a great e-commerce platform for WordPress. I've been working with this for such a long time and it is a reliable plugin-in for sure.
One of my customers needed to redirect the buyer to specific different thank you pages on his WordPress website which uses WooCommerce. The redirect should have happened after the checkout process. During the checkout the buyer had multiple payment options. The buyer should have been redirected to a specific thank you page depending on the chosen payment gateway.
I will explain the whole process just to be sure it is clear. As I said above, the website uses different payment methods for the listed products. The website has the option to pay via PayPal, but also has the option to pay via a normal bank transfer. The plan was to redirect the buyer to a custom thank you page after he has chosen the bank transfer. This page was different than the thank you page in the case of a PayPal payment. On it the buyer could see all the information needed for the payment (name of the bank, bank account etc.).
Activate the WooCommerce payment methods you need
So, after you have installed WooCommerce and created your products you will need to select the payment methods. Just go to WooCommerce > Payments and just select, for example, "Direct bank transfer" and "PayPal Checkout" (of course, on your website you can have other payment methods, but I'm selecting these just for demo purpose).
Create two different thank you pages
One thank you page which will have the slug /direct-bank-transfer-thank-you-page/ will be the page on which the buyers will land after they made the payment using "Direct bank transfer" as a payment method. The other thank you page will have the slug /paypal-checkout-thank-you-page/ on which the buyers will land after they made the payment using "PayPal Checkout" as a payment method.
Test your checkout process
Just go to one of your products pages, add the product to cart and go to Cart page. I'll tell you why we need to do this.
On the cart page just click on Proceed to checkout.
Then you will land on the checkout page which, at the bottom, has the two payment options, like in the below image:
Get the value of the payment methods
What does this mean? You need to find the values of these two radio buttons in the HTML code. So, all you have to do is Inspect the code (right click on the radio buttons and click Inspect) and you'll see the HTML code like in the image below:
So the value of "Direct bank transfer" is bacs and the value of "PayPal Checkout" is ppec_paypal.
Why do we need this values? Because we will add a php code which will actually will redirect users to different thank you pages based on the chosen payment method (gateway) in WooCommerce checkout.
Adding the redirect code to your functions.php
Open your functions.php file which it is in your theme folder and add the below code:
add_action( 'woocommerce_thankyou', 'zeninv_redir_based_on_payment_method' );
function zeninv_redir_based_on_payment_method(){
/* do nothing if we are not on the appropriate page */
if( !is_wc_endpoint_url( 'order-received' ) || empty( $_GET['key'] ) ) {
return;
}
$order_id = wc_get_order_id_by_order_key( $_GET['key'] );
$order = wc_get_order( $order_id );
if( 'bacs' == $order->get_payment_method() ) { /* WC 3.0+ */
wp_redirect( 'https://yourwebsite.com/direct-bank-transfer-thank-you-page/' );
exit;
}
if( 'ppec_paypal' == $order->get_payment_method() ) { /* WC 3.0+ */
wp_redirect( 'https://yourwebsite.com/paypal-checkout-thank-you-page/' );
exit;
}
}
Basically the above code uses a function to redirect users to the specific thank you pages. It is important to use woocommerce_thankyou WooCommerce hook, because this means that the redirect will happen after the order is registered and will not create any problems to processing that. Don't forget to replace yourwebsite.com with your domain name!
We are done! Please let me know how is this actually working for you.
Comments closed
Please contact me, if you have any questions or suggestions.