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.
In your function, you don't define $order
. You need to get the $order Object before you can access any of the methods. Since $order
is not defined, the other cases elseif
are never true.
// Thank you page redirect
add_action( 'template_redirect', 'woo_custom_redirect_after_purchase' );
function woo_custom_redirect_after_purchase() {
global $wp;
// Get the order object
$order = new WC_Order($wp->query_vars['order-received']);
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;
}
}