Search
Include additional data within Split Orders - WP Global Cart
17620
documentation-template-default,single,single-documentation,postid-17620,theme-awake,eltd-core-1.1,woocommerce-no-js,awake child-child-ver-1.0.0,awake-ver-1.0,eltd-smooth-scroll,eltd-smooth-page-transitions,eltd-mimic-ajax,eltd-grid-1200,eltd-blog-installed,eltd-default-style,eltd-fade-push-text-top,eltd-header-standard,eltd-sticky-header-on-scroll-down-up,eltd-default-mobile-header,eltd-sticky-up-mobile-header,eltd-menu-item-first-level-bg-color,eltd-dropdown-slide-from-top,eltd-,eltd-fullscreen-search eltd-search-fade,eltd-side-menu-slide-from-right,wpb-js-composer js-comp-ver-6.3.0,vc_responsive
 

Include additional data within Split Orders

WP Global Cart / Include additional data within Split Orders
Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInShare on TumblrPin on PinterestEmail this to someonePrint this page

Include additional data within Split Orders

The Split function in the WooCommerce Global Cart plugin is a powerful feature that enables the division of a main order into separate orders across various shops containing products in the cart. This functionality is particularly beneficial in multi-vendor environments, ensuring each shop processes its respective products independently.

The split engine’s flexibility allows for programmatic extensions, primarily through the woogc/single_checkout/split_order/order_created filter. This filter is instrumental in customizing the behavior of split orders during their creation. By hooking into this filter, developers can modify order properties, append metadata, or implement custom logic tailored to specific requirements. Incorporating additional data into split orders is often essential for proper processing within individual shops. This data can encompass various elements, such as custom order statuses, unique identifiers, or specialized processing instructions.

In the following example, the tax data from the original order is seamlessly transferred to each split order, ensuring accurate tax calculations and consistency across all individual shop orders.

    add_action ( 'woogc/single_checkout/split_order/order_created', '_custom_woogc_single_checkout_split_order_order_created' );
    function _custom_woogc_single_checkout_split_order_order_created( $new_order )
        {
            
            $origin_blog_id     =   $new_order->get_meta( 'checkout_blog_id' );    
            $origin_order_id    =   $new_order->get_meta( 'checkout_order_id' );
            
            if ( empty ( $origin_blog_id )  ||  empty ( $origin_order_id ) )
                return;
                
            switch_to_blog( $origin_blog_id );
            
            $default_order  =   new WC_Order ( $origin_order_id );
            $tax_items      =   $default_order->get_items( 'tax' );
            
            restore_current_blog();
            
            if ( count ( $tax_items ) > 0 )
                {
                    foreach ( $tax_items as $key   =>  $tax_item )
                        {
                        
                            $item                       = new WC_Order_Item_Tax();
                            $tax_item_data         = $tax_item->get_data();
                            $item->set_props(
                                                array(
                                                    'rate_id'            => $tax_item_data['rate_id'],
                                                    'tax_total'          => $tax_item_data['tax_total'],
                                                    'shipping_tax_total' => $tax_item_data['shipping_tax_total'],
                                                    'rate_code'          => $tax_item_data['rate_code'],
                                                    'label'              => $tax_item_data['label'],
                                                    'compound'           => $tax_item_data['compound'],
                                                    'rate_percent'       => $tax_item_data['rate_percent'],
                                                )
                                            );
                            // Add item to the order
                            $new_order->add_item( $item );
                        }
                }
                
            $new_order->save();
        }

The above code should be placed within a custom file on /wp-content/mu-plugins/ folder or the theme functions.php or a custom plugin.

0
Would love your thoughts, please comment.x
()
x