Ecommerce Shopify WordPress Discussion

WooCommerce multiple redirections after a purchase

The following code below, redirects the customer to a custom page after a purchase in WooCommerce. In this custom page user can buy additional item (modifying order with adding new item): 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; } } } But the problem is when I try to access order-received page I keep getting redirected back to this custom page. How to avoid that issue?
You can try the following instead to avoid your redirection issue on order received: add_action('woocommerce_checkout_order_created', 'set_wc_session_original_order_id'); function set_wc_session_original_order_id( $order ) { WC()->session->set('original_order_id', $order->get_id()); } add_action('template_redirect', 'custom_shop_order_process_redirects'); function custom_shop_order_process_redirects() { if ( is_page('upsell-1') && isset($_GET['woocommerce_checkout_place_order'])) { $order_id = WC()->session->get('original_order_id'); if ( $order_id > 0 ) { $order = wc_get_order($order_id); $order->add_product(wc_get_product('875')); $order->calculate_totals(); // Already includes save() method WC()->session->__unset('original_order_id'); // Remove session variable wp_redirect( $order->get_checkout_order_received_url() ); exit; } } elseif ( is_wc_endpoint_url('order-received') && WC()->session->__isset('original_order_id') ) { wp_redirect( home_url('/upsell-1') ); exit; } } It should work.

December 29, 2023

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.