userphp10 Posted December 28, 2023 Share Posted December 28, 2023 This code checks firstly if user enters order-received url so that they won't be redirected to upsell-1 page all the time. But the problem is that after purchase i'm being redirected to order received page instead of upsell-1 page. Maybe validation must be written in different way (if (is_wc_endpoint_url('order-received'))) add_action('woocommerce_thankyou', 'upsell_redirect'); function upsell_redirect($order_id) { if (is_wc_endpoint_url('order-received')) { return; } WC()->session->set('original_order_id', $order_id); $upsell_page_url = home_url('/upsell-1'); wp_redirect($upsell_page_url); exit; } Quote Link to comment Share on other sites More sharing options...
Olumide Posted December 28, 2023 Share Posted December 28, 2023 You can try checking if the current request is for the 'order-received' endpoint using the is_wc_endpoint_url function with the specific order ID. Here's an updated version of your code: add_action('woocommerce_thankyou', 'upsell_redirect'); function upsell_redirect($order_id) { // Check if the current request is for the 'order-received' endpoint with the specific order ID if (is_wc_endpoint_url('order-received') && get_query_var('order-received') == $order_id) { return; } WC()->session->set('original_order_id', $order_id); $upsell_page_url = home_url('/upsell-1'); wp_redirect($upsell_page_url); exit; } Quote Link to comment Share on other sites More sharing options...
userphp10 Posted December 28, 2023 Author Share Posted December 28, 2023 Alright redirection to upsell works now after purchase after editing this part from if (is_wc_endpoint_url('order-received')) to if (!is_wc_endpoint_url('order-received')) but when in upsell-1 page after submitting button new item is added but I keep getting redirected back to upsell-1 page I should be redirected to order-received, I echoed out the url it is correct, but redirection is wrong. add_action('woocommerce_thankyou', 'upsell_redirect'); function upsell_redirect($order_id) { if (!is_wc_endpoint_url('order-received')) { return; } WC()->session->set('original_order_id', $order_id); $upsell_page_url = home_url('/upsell-1'); wp_redirect($upsell_page_url); exit; } add_action('template_redirect', 'process_upsell'); function process_upsell() { if (is_page('upsell-1') && isset($_GET['woocommerce_checkout_place_order'])) { $original_order_id = WC()->session->get('original_order_id'); if ($original_order_id) { $original_order = wc_get_order($original_order_id); $original_order->add_product(wc_get_product('875')); $original_order->calculate_totals(); $original_order->save(); $order_received_url = $original_order->get_checkout_order_received_url(); wp_redirect($order_received_url); exit; } } } Quote Link to comment Share on other sites More sharing options...
userphp10 Posted December 28, 2023 Author Share Posted December 28, 2023 3 minutes ago, Olumide said: You can try checking if the current request is for the 'order-received' endpoint using the is_wc_endpoint_url function with the specific order ID. Here's an updated version of your code: add_action('woocommerce_thankyou', 'upsell_redirect'); function upsell_redirect($order_id) { // Check if the current request is for the 'order-received' endpoint with the specific order ID if (is_wc_endpoint_url('order-received') && get_query_var('order-received') == $order_id) { return; } WC()->session->set('original_order_id', $order_id); $upsell_page_url = home_url('/upsell-1'); wp_redirect($upsell_page_url); exit; } With your changes, after purchase I'm being redirected to order-received page straight away, but I need to be redirected to upsell-1 page Quote Link to comment Share on other sites More sharing options...
Olumide Posted December 28, 2023 Share Posted December 28, 2023 Try it this way add_action('woocommerce_thankyou', 'upsell_redirect'); function upsell_redirect($order_id) { // Check if not on the 'order-received' page if (!is_wc_endpoint_url('order-received')) { WC()->session->set('original_order_id', $order_id); $upsell_page_url = home_url('/upsell-1'); wp_redirect($upsell_page_url); exit; } } Quote Link to comment Share on other sites More sharing options...
userphp10 Posted December 28, 2023 Author Share Posted December 28, 2023 4 minutes ago, Olumide said: Try it this way add_action('woocommerce_thankyou', 'upsell_redirect'); function upsell_redirect($order_id) { // Check if not on the 'order-received' page if (!is_wc_endpoint_url('order-received')) { WC()->session->set('original_order_id', $order_id); $upsell_page_url = home_url('/upsell-1'); wp_redirect($upsell_page_url); exit; } } Did not work - I'm redirected straight to order-received page Quote Link to comment Share on other sites More sharing options...
Olumide Posted December 28, 2023 Share Posted December 28, 2023 I adjusted it this way add_action('woocommerce_thankyou', 'upsell_redirect'); function upsell_redirect($order_id) { // Check if not on the 'order-received' page if (!is_order_received_page()) { WC()->session->set('original_order_id', $order_id); $upsell_page_url = home_url('/upsell-1'); wp_redirect($upsell_page_url); exit; } } Quote Link to comment Share on other sites More sharing options...
Olumide Posted December 28, 2023 Share Posted December 28, 2023 1 minute ago, Olumide said: I adjusted it this way add_action('woocommerce_thankyou', 'upsell_redirect'); function upsell_redirect($order_id) { // Check if not on the 'order-received' page if (!is_order_received_page()) { WC()->session->set('original_order_id', $order_id); $upsell_page_url = home_url('/upsell-1'); wp_redirect($upsell_page_url); exit; } } Instead of relying on the is_wc_endpoint_url function, you can use the is_order_received_page function to check if you are on the order received page. Quote Link to comment Share on other sites More sharing options...
userphp10 Posted December 28, 2023 Author Share Posted December 28, 2023 (edited) 11 minutes ago, Olumide said: I adjusted it this way add_action('woocommerce_thankyou', 'upsell_redirect'); function upsell_redirect($order_id) { // Check if not on the 'order-received' page if (!is_order_received_page()) { WC()->session->set('original_order_id', $order_id); $upsell_page_url = home_url('/upsell-1'); wp_redirect($upsell_page_url); exit; } } I think with this code i'm still gonna be redirected to order-received page nevertheless because this is just an if check. By the way are you using GPT? I tried it but to no avail. Edited December 28, 2023 by userphp10 Quote Link to comment Share on other sites More sharing options...
Olumide Posted December 28, 2023 Share Posted December 28, 2023 I use a different approach by using the `wp_get_referer()` function to check the referring page. If the referring page is not the 'order-received' page, then it will proceed with the redirection. ```php add_action('woocommerce_thankyou', 'upsell_redirect'); function upsell_redirect($order_id) { // Get the referring page $referer = wp_get_referer(); // Check if the referring page is not the 'order-received' page if ($referer && false === strpos($referer, 'order-received')) { WC()->session->set('original_order_id', $order_id); $upsell_page_url = home_url('/upsell-1'); wp_redirect($upsell_page_url); exit; } } ``` Quote Link to comment Share on other sites More sharing options...
userphp10 Posted December 28, 2023 Author Share Posted December 28, 2023 (edited) If someone more knowledgeable could help, I would appreciate. Edited December 28, 2023 by userphp10 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.