Redirect to origin shop on Order Received, when using Single Checkout with Split
When utilizing the Single Checkout option, all products within the global cart undergo processing within a designated shop. Conversely, when opting for the Split function, individual orders are generated within each of the shops that contributed products to the cart.
Upon the completion of the checkout process, a page titled ‘Order Received’ is presented to the customer.
To seamlessly redirect the customer to the ‘Order Received’ page hosted on the shop where the origin product was located, you can make use of the following code snippet. Simply update the variable $ProductsOriginShop with the appropriate shop ID for redirection.
add_action('woocommerce_thankyou', '__woocommerce_thankyou' ); function __woocommerce_thankyou() { global ${"order-received"}; $order_received = ${"order-received"}; if ( empty ( $order_received ) ) return; $current_order = new WC_Order ( $order_received ); $order_items = $current_order->get_items(); //loop the items if Products are located on the same shop $products_shops = array(); foreach ( $order_items as $order_item ) { $product_meta_shop_id = $order_item->get_meta( 'blog_id' ); if ( $product_meta_shop_id ) $products_shops[] = $product_meta_shop_id; } $products_shops = array_unique ( $products_shops ); //if more shops, we won't redirect anywhere if ( count ( $products_shops ) > 1 ) return; reset ( $products_shops ); $redirect_to_shop = current ( $products_shops ); global $wpdb, $blog_id, $WooGC; if ( $blog_id == $redirect_to_shop ) return; $WooGC->functions->remove_anonymous_object_filter('woocommerce_get_checkout_url', 'WooGC_Template', 'woocommerce_get_checkout_url', 999); $origin_blog_id = $blog_id; switch_to_blog( $redirect_to_shop ); $mysql_query = $wpdb->prepare ( "SELECT PM.post_id from " . $wpdb->postmeta ." as PM JOIN " . $wpdb->postmeta ." AS PM2 ON PM.post_id = PM2.post_id WHERE PM.`meta_key` = 'checkout_order_id' and PM.`meta_value` = %s AND PM2.`meta_key` = 'checkout_blog_id' and PM2.`meta_value` = %s", $order_received, $origin_blog_id ); $remote_order_id = $wpdb->get_var( $mysql_query ); $remote_order = new wc_order ( $remote_order_id ); $order_received_url = wc_get_endpoint_url( 'order-received', $remote_order->get_id(), wc_get_checkout_url() ); $order_received_url = add_query_arg( 'key', $remote_order->get_order_key(), $order_received_url ); $order_received_url = add_query_arg( 'checkout_split_redirect', '1', $order_received_url ); restore_current_blog(); wp_redirect( $order_received_url ); die(); } add_filter ( 'wp_loaded', '__wp_loaded' ) ; function __wp_loaded() { global $WooGC; if ( isset ( $_GET['checkout_split_redirect'] ) && $_GET['checkout_split_redirect'] == '1' ) $WooGC->functions->remove_anonymous_object_filter( 'wp', 'WooGC_Template', 'check_checkout_location' ); }