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.