Ecommerce Shopify WordPress Discussion

Woocommerce, redirection sequence and validation of sessions

I have woocommerce code php. It's purpose is that after checkout user should be redirected to upsell-1 page. When user adds additional item to order from upsell-1 then they're redirected to upsell-2 page same principle applies till they reach upsell-3. After submitting item from upsell-3 to existing order, then they should be redirected to order-received page also the order session then is unset. Also this code is doing validations so that if user has already added items from upsell pages. For example, if already added item from upsell-1, they should be redirected to upsell-2, if already added item from upsell-2 redirected to upsell-3, If already added item from upsell-3, then they should be redirected to order-received page. It also should check if order exists if it doesn't and if trying to access any upsell page they should be redirected to home_url, but it seems that none works. I'm sure this implementation is not correct. 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(); WC()->session->set('upsell-1-viewed', 'true'); wp_redirect('/upsell-2'); exit; } } elseif(is_page('upsell-2') && 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(); WC()->session->set('upsell-2-viewed', 'true'); wp_redirect('/upsell-3'); exit; } } elseif(is_page('upsell-3') && 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(); WC()->session->__unset('original_order_id'); //We then do unset so that user won't be able to modify order 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; } } //need to check if user has already bought item from upsells, for instance, if bought from upsell-1 redirect to upsell-2, if bought from upsell-2, redirect to upsell-3, if bought from upsell-3 redirect to order received page only if original order id session exists. Also need to check if order doesn't exist, user won't be able to access upsells pages. add_action('template_redirect', 'upsells_redirects'); function upsells_redirects(){ if(is_page('upsell-1') && WC()->session->__isset('upsell-1-viewed' && WC()->session->__isset('original_order_id') )){ wp_redirect('/upsell-2'); } elseif(is_page('upsell-2') && WC()->session->__isset('upsell-2-viewed') && WC()->session->__isset('original_order_id')){ wp_redirect('/upsell-3'); } elseif(is_page('upsell-3') && WC()->session->__isset('upsell-3-viewed') && WC()->session->__isset('original_order_id')){ $order = wc_get_order($order_id); wp_redirect( $order->get_checkout_order_received_url() ); } }
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.