Clone woocommerce order on other subsite, wp multisite

Question

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(
         'first_name'=>$original_order->get_billing_first_name(),
         'last_name'=>$original_order->get_billing_last_name(),
         'company'=>$original_order->get_billing_company(),
         'address_1'=>$original_order->get_billing_address_1(),
         'address_2'=>$original_order->get_billing_address_2(),
         'city'=>$original_order->get_billing_city(),
         'state'=>$original_order->get_billing_state(),
         'postcode'=>$original_order->get_billing_postcode(),
         'country'=>$original_order->get_billing_country(),
         'email'=>$original_order->get_billing_email(),
         'phone'=>$original_order->get_billing_phone(),
     );
      

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

    // SWITCH TO BLOG:: 3
     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);
     restore_current_blog();
     
 }

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

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

     foreach ($original_order_items as $item){

         if($item->get_data()['variation_id']>0){
             $product_id=$item->get_data()['variation_id'];
         }else{
             $product_id=$item->get_data()['product_id'];
         }

         $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){
         $order->apply_coupon($code);
     }

     $order->set_order_key($original_order_key);


     // 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.

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

Leave an answer

Browse
Browse