Hi there,
When adding all of it to the functions.php file, what error message are you getting? Also, would you mind sharing a bit more on what it is you’re trying to do – what’s the use-case for that code?
Finally, please note that the Stack Exchange example you reference is from 2017 and might no longer be compatible with the latest version of WooCommerce.
@riaanknoetze Hey RK, sure let me explain to you the use case.
Background
We have changed the checkout process slightly in WooCommerce
Usually, a user goes: Order Checkout –> Thank you Page (where Order ID appears) –> Upsell page
Now it goes like this: Order Checkout –> Upsell Page
This redirect is achieved using this script:
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://website.com/upsell/' );
exit;
}
}
Question
The issue here is that I can’t extract Order ID from the DOM in the Thank You page because after checkout we immediately redirect users to the Upsell page.
So is there a way to push the Order ID into a Javascript variable on the Upsell Page? Thus, I tried to follow what the Stack Overflow Article did.
Thank you.
-
This reply was modified 6 years ago by
arthurlam1.
Mina
(@purpleberryservices)
Hi,
You should try something like this:
add_action( ‘woocommerce_thankyou’, ‘pbs_redirect_to_custom_thank_you’);
function pbs_redirect_to_custom_thank_you( $order_id ){
$order = wc_get_order( $order_id );
if ( ! $order->has_status( ‘failed’ ) ) {
$url = // Upsell page URL.
wp_redirect( $url.”?id=”.$order_id );
exit;
}
}
@purpleberryservices Thanks.
Believe I should put your code within functions.php?
I get this error message: syntax error, unexpected 'wp_redirect' (T_STRING). Below is the updated code for the upsell page. Can you let me know what went wrong? Thanks.
add_action( 'woocommerce_thankyou', 'pbs_redirect_to_custom_thank_you');
function pbs_redirect_to_custom_thank_you( $order_id ){
$order = wc_get_order( $order_id );
if ( ! $order->has_status( 'failed' ) ) {
$url = 'https://website.com/upsell/'
wp_redirect( $url.'?id='.$order_id );
exit;
}
}
Mina
(@purpleberryservices)
Yes it will go in functions.php file.
There is no comma after $url statement.
IT should be $url = ‘https://website.com/upsell/’;
————-
add_action( ‘woocommerce_thankyou’, ‘pbs_redirect_to_custom_thank_you’);
function pbs_redirect_to_custom_thank_you( $order_id ){
$order = wc_get_order( $order_id );
if ( ! $order->has_status( ‘failed’ ) ) {
$url = ‘https://website.com/upsell/’;
wp_redirect( $url.’?id=’.$order_id );
exit;
}
}
@purpleberryservices Thanks. I removed the comma at the end and replaced it with a ;
The below code is what I am using. I now get an error message syntax error, unexpected '='
add_action( 'woocommerce_thankyou', 'pbs_redirect_to_custom_thank_you');
function pbs_redirect_to_custom_thank_you( $order_id ){
$order = wc_get_order( $order_id );
if ( ! $order->has_status( 'failed' ) ) {
$url = 'https://website.com/upsell/;
wp_redirect( $url.'?id='.$order_id );
exit;
}
}
@purpleberryservices my bad. Managed to solve it!
Thank you! And yes it works 🙂