AI Verified

Name

Split Order

Language

PHP

Rating

Voted: 0 by 0 user(s)

Codevault

cloudmeister

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: 6.1

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

24/12/2022

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Found 0 vulnerabilities

Split Order

 
                    
1function cmx65__split_completed_order($order_id) {
2 $order = wc_get_order($order_id);
3 
4 $order_product_items = array_filter(
5 $order->get_items(),
6 function($item){
7 return $item instanceof WC_Order_Item_Product;
8 }
9 );
10 
11 if ($order_product_items <= 1) {
12 return;
13 }
14 
15 // WC_Order: https://woocommerce.github.io/code-reference/classes/WC-Order.html
16 foreach( $order_product_items as $product_item ){
17 // Create New Order For Each Product Item
18 $new_order = wc_create_order([
19 'cart_hash' => $order->get_cart_hash(),
20 'created_via' => $order->get_created_via(),
21 'customer_id' => $order->get_customer_id(),
22 'customer_note' => $order->get_customer_note(),
23 'parent' => $order->get_id(),
24 'status' => $order->get_status(),
25 ]);
26 
27 // Clone Order Information
28 $new_order->set_address($order->get_address('billing'), 'billing');
29 $new_order->set_address($order->get_address('shipping'), 'shipping');
30 $new_order->set_cart_tax($order->get_cart_tax());
31 $new_order->set_created_via($order->get_created_via());
32 $new_order->set_currency($order->get_currency());
33 $new_order->set_customer_ip_address($order->get_customer_ip_address());
34 $new_order->set_customer_user_agent($order->get_customer_user_agent());
35 $new_order->set_payment_method($order->get_payment_method());
36 $new_order->set_meta_data($order->get_meta_data());
37 $order_meta_data = $order->get_meta_data();
38 foreach($order_meta_data as $meta_data){
39 $raw_meta_data = $meta_data->get_data();
40 $new_order->add_meta_data($raw_meta_data['key'], $raw_meta_data['value']);
41 }
42 $new_order->save_meta_data();
43 
44 // Clone Product Item and Assign to New Order
45 $new_product_item = new WC_Order_Item_Product();
46 $new_product_item->set_order_id($new_order->get_id());
47 $new_product_item->set_product($product_item->get_product());
48 $new_product_item->set_subtotal($product_item->get_subtotal());
49 $new_product_item->set_total($product_item->get_total());
50 $new_product_item->save();
51 $product_meta_data = $product_item->get_meta_data();
52 foreach($product_meta_data as $meta_data){
53 $raw_meta_data = $meta_data->get_data();
54 $new_product_item->add_meta_data($raw_meta_data['key'], $raw_meta_data['value']);
55 }
56 $new_product_item->save_meta_data();
57 $new_order->add_item($new_product_item);
58 
59 // Clone Shipping Lines
60 $order_shipping_items = array_filter(
61 $order->get_items('shipping'),
62 function($item){
63 return $item instanceof WC_Order_Item_Shipping;
64 }
65 );
66 foreach( $order_shipping_items as $shipping_item ) {
67 $new_shipping_item = new WC_Order_Item_Shipping();
68 $new_shipping_item->set_order_id($new_order->get_id());
69 $new_shipping_item->save();
70 
71 $new_shipping_item->set_method_title($shipping_item->get_method_title());
72 $new_shipping_item->set_method_id($shipping_item->get_method_id());
73 $new_shipping_item->set_instance_id($shipping_item->get_instance_id());
74 $new_shipping_item->set_total($shipping_item->get_total());
75 $new_shipping_item->set_taxes($shipping_item->get_taxes());
76 
77 $shipping_meta_data = $shipping_item->get_meta_data();
78 foreach($shipping_meta_data as $meta_data){
79 $raw_meta_data = $meta_data->get_data();
80 $new_shipping_item->add_meta_data($raw_meta_data['key'], $raw_meta_data['value']);
81 }
82 $new_shipping_item->save_meta_data();
83 
84 $new_order->add_item($new_shipping_item);
85 }
86 
87 $new_order->calculate_totals();
88 $new_order->save();
89 }
90}
91 
92add_action('plugins_loaded', function(){
93 add_action('woocommerce_order_status_completed', 'cmx65_split_completed_order', 10, 1);
94});

0

Related Snippets

Please see some snippets below related to this snippet..

WooCommerce

AI Verified

0

Apply Coupon Before VAT

Added: 1 year ago

Last Updated: 1 year ago

the product price before VAT is retrieved using the "get_price_excluding_tax()" method, and the discount is applied to this price. The discounted price is then set as the product price using the "se...

WooCommerce

AI Verified

0

Display the mobile phone field

Added: 2 years ago

Last Updated: 1 year ago

WooCommerce

AI Verified

0

WooCommerce Add Product-Unique Content Above Image On Loop Item

Added: 1 year ago

Last Updated: 1 year ago

There are two hooks which can be used to show content above the product image on the loop item: woocommerce_before_shop_loop_item and woocommerce_before_shop_loop_item_title. Each of these actions can...

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WordPress Admin

AI Verified

1

Custom Login 4 Agencies

Added: 1 year ago

Last Updated: 1 year ago

Give your Clients a constant LogIn-Feeling with your CI You can: - add an Image from your Agency Site to the login.form - add a Link to/behind the Image - customize some border-color's - hide t...

WooCommerce

Unverified

0

CLOUD Meister: Checkout shipping inline

Added: 1 year ago

Last Updated: 1 year ago

WordPress Admin

AI Verified

0

enable autoupdater on specific time

Added: 1 year ago

Last Updated: 1 year ago

This enables the Autoupdater only between a specific time range.