I'm trying to work on something new for displaying 2 different thank you pages for the woocommerce plugin. When a user has selected a different shipping method like local pickups I want to redirect them to different thank you pages after checkout.
I have added the following code but it doesn't works redirects to the default. What am I doing wrong
// Thank you page redirect
add_action( 'template_redirect', 'woo_custom_redirect_after_purchase' );
function woo_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && !empty( $wp->query_vars['order-received'] ) ) {
wp_redirect( 'https://mywebsite.com/thank-you-page-1/' );
exit;
}
elseif( $order->has_shipping_method('local_pickup:7') ) {
wp_redirect( 'https://mywebsite.com/thank-you-page-2/' );
exit;
}
elseif( $order->has_shipping_method('flat_rate:1') ) {
wp_redirect( 'https://mywebsite.com/thank-you-page-3/' );
exit;
}
}
Not sure what's happening, might be I'm missing or doing something wrong.
CodePudding user response:
Does it have to be a redirect? I don't know exactly what you're up to, but maybe it is enough to display different content, depending on the shippment method? If so, you can edit the thankyou.php template.
First copy the file plugins/woocommerce/templates/checkout/thankyou.php
to your theme directory themes/(your-theme)/woocommerce/checkout/tankyou.php
. Here you can change the thankyou template, without losing the changes after a woocommerce update. (Template structure & Overriding templates via a theme)
Now you can adjust the thankyou.php
(inside your theme directory) and add your conditions at the right place:
if ( $order->has_shipping_method('local_pickup:7') ) {
echo "thank you content 2";
}
elseif ( $order->has_shipping_method('flat_rate:1') ) {
include 'path-to/my-tankyou-content3-file.php';
}
CodePudding user response:
//Use this hook to target your thankyou page.
<?php
add_action( 'woocommerce_thankyou', 'your_function_name');
function your_function_name($order_get_id){
// Here comes your code
}