Clone woocommerce order on other subsite, wp multisite


Cordial greetings.

Context: I have a multisite, with the Multilingualpress plugin and Woocommerce, each site in a different language: EN – FR – DE – ZC

Requirement: Synchronize purchase orders between all subsites. If a customer buys on the site in ENGLISH, they should also be able to see this same order on the other three sites (FR-DE-CZ)

public function sbd_sync_orders($order_id){

     $original_blog = get_current_blog_id();

     $original_order = wc_get_order($order_id);
     //Initial args
     $new_args = array(
         'customer_id'   => $original_order->get_user_id(),
         'customer_note' => $original_order->get_customer_note(),
         'parent'        => $original_order->get_parent_id(),
         'created_via'   => "programmatically",
         'cart_hash'     => $original_order->get_cart_hash(),

     //Order Key
     $original_order_key = $original_order->get_order_key();

     // Coupons
     $new_coupons = $original_order->get_coupon_codes();

     // Address
      $new_billing = array(

    //Here the data from $ original_order-> get_items () is OK

    // SWITCH TO BLOG:: 3
     //Here the data from $ original_order-> get_items () becomes wrong
     $this->sbd_duplicate($original_blog,$new_args,$original_order, $original_order_key,$new_coupons, $new_billing, $order_id);

function sbd_duplicate($original_blog,$new_args,$original_order, $original_order_key,$new_coupons, $new_billing, $order_id){
     $order = wc_create_order($new_args);

     //ADD:: Products
     $original_order_items = $original_order->get_items();         

     foreach ($original_order_items as $item){


         $order->add_product(wc_get_product($product_id), $item->get_data()['quantity']);

     //SET:: Address
     $order->set_address( $new_billing, 'billing' );

     //SET:: Coupons
     foreach ($new_coupons as $code){


     // FINISH
     $order->add_order_note('Duplicated programmatically from:: '.$original_blog);
     $order->update_status("completed", 'Duplicated programmatically', TRUE);
     var_dump('NUEVA ORDEN::'.$order->get_id());
     var_dump('NUEVOS PROD::'.$order->get_items());


1:: Problem When I do the switch_to_blog(3), the data of the variable $original_order->get_items() changes (it shows products that are not)

2:: Problem I’m not sure if this is the proper way to do this, as I’m not sure if I’m taking all the data from the original order (the metadata topic confuses me a lot).
I’m thinking of trying to clone through woocommerce API.

I appreciate any opinion you have about it, thank you.

Yulian David 3 months 2021-07-11T11:17:35-05:00 0 Answers 0 views 0

Leave an answer