Home > database >  Woocommerce - Use SKU as unique identifier in the order creation with REST API
Woocommerce - Use SKU as unique identifier in the order creation with REST API

Time:07-24

I have a website where I want to transfer woocommerce orders. Everything is working very well now but I have a strungle with product pairing. Products ids on testing are not the same as on live website. What do I need to add products to order and pair them not with their ID but with the SKU which is the same on both websites. My code is bellow.

I'm following this structure over here but I have no clue how to pair line items by the SKU 'line_items': https://woocommerce.github.io/woocommerce-rest-api-docs/?php#create-an-order

add_action( 'woocommerce_order_status_processing_to_gls', 'transfer_order_woo_api', 9999, 3 );
function transfer_order_woo_api( $order_id, $order ) {
   $live_ck = 'ck_some ID';
   $live_cs = 'cs_SOME ID';
   $live_url = 'https://testsite.com/wp-json/wc/v3/orders?consumer_key=' . $live_ck . '&consumer_secret=' . $live_cs;
   $order = wc_get_order( $order_id );
   
    // Line items - HERE IS THE PROBLEM 
    $lineitems = array();
    foreach ( $order->get_items() as $item_id => $item ) {
     $product_id = $item->get_product_id();
     $quantity = $item->get_quantity();
     $product = $item->get_product();
     
    $lineitem = array('quantity' => $quantity,
                     'product_id' => $product_id //I need to pair theese line items by the SKU because the product ID is not the same on different sites but the SKU is the same.
                
                      );
    $lineitems[] = $lineitem;
    }
   
   $body = array(
      'status' => 'processing',
      'meta_data' => array( 
      array( 
         'key' => 'carrier',
         'value' => ''.get_post_meta($order->id, 'ywot_carrier_id', true).''
      ),
      array( 
         'key' => 'order_number',
         'value' => ''.get_post_meta($order->id, '_alg_wc_custom_order_number', true).''
      ),
      array( 
         'key' => 'orderid',
         'value' => $order->get_id(),
      ),
      array( 
         'key' => 'shipped_date',
         'value' => ''.get_post_meta($order->id, 'shipped_date', true).''
      ),
      array( 
         'key' => 'ywot_tracking_code',
         'value' => ''.get_post_meta($order->id, 'ywot_tracking_code', true).''
      )),
      
      'billing' => array(
         'first_name' => $order->get_billing_first_name(),
         'last_name' => $order->get_billing_last_name(),
         'address1' => $order->get_billing_address_1(),
         'city' => $order->get_billing_city(),
         'postcode' => $order->get_billing_postcode(),
         'country' => $order->get_billing_country(),
         'phone' => $order->get_billing_phone(),
      ),
      'shipping' => array(
         'first_name' => $order->get_shipping_first_name(),
         'last_name' => $order->get_shipping_last_name(),
         'address1' => $order->get_shipping_address_1(),
         'city' => $order->get_shipping_city(),
         'postcode' => $order->get_shipping_postcode(),
         'country' => $order->get_shipping_country(),
      ),
      'line_items' => $lineitems,
     
   );
   
   
   $raw_response = wp_remote_post( $live_url, 
      array(
         'headers' => array( 'Content-Type' => 'application/json' ),
         'timeout' => 30,                    
         'body' => json_encode( $body ),
      )
   );

Thanks in advance for your ideas.


UPDATE

add_action( "woocommerce_order_status_processing_to_gls", "transfer_order_woo_api", 9999, 3 );
function transfer_order_woo_api( $order_id, $order ) {
   $remote_keys = "consumer_key=ck_xxxx&consumer_secret=cs_xxxx";
   $live_url = "https://web.com/wp-json/wc/v3/orders?{$remote_keys}";
   $line_items = array();

   foreach ( $order->get_items() as $item_id => $item ) {
      $quantity = $item->get_quantity();
      $product = $item->get_product();
      $product_sku = $product->get_sku();

      // retrieve product ID by SKU, return product properties
      $remote_get = wp_remote_get("https://web.com/wp-json/wc/v3/products?sku={$product_sku}&{$remote_keys}");
      $remote_product = json_decode($remote_get['body'])[0];
      
      $line_item = array(
                       "quantity" => $quantity,
                       "product_id" => $remote_product->id
                      );

      $line_items[] = $line_item;
   }

 $body = array(
      'status' => 'processing',
      'meta_data' => array( 
      array( 
         'key' => 'carrier',
         'value' => ''.get_post_meta($order->id, 'ywot_carrier_id', true).''
      ),
      array( 
         'key' => 'order_number',
         'value' => ''.get_post_meta($order->id, '_alg_wc_custom_order_number', true).''
      ),
      array( 
         'key' => 'orderid',
         'value' => $order->get_id(),
      ),
      array( 
         'key' => 'shipped_date',
         'value' => ''.get_post_meta($order->id, 'shipped_date', true).''
      ),
      array( 
         'key' => 'ywot_tracking_code',
         'value' => ''.get_post_meta($order->id, 'ywot_tracking_code', true).''
      )),
      
      'billing' => array(
         'first_name' => $order->get_billing_first_name(),
         'last_name' => $order->get_billing_last_name(),
         'address1' => $order->get_billing_address_1(),
         'city' => $order->get_billing_city(),
         'postcode' => $order->get_billing_postcode(),
         'country' => $order->get_billing_country(),
         'phone' => $order->get_billing_phone(),
      ),
      'shipping' => array(
         'first_name' => $order->get_shipping_first_name(),
         'last_name' => $order->get_shipping_last_name(),
         'address1' => $order->get_shipping_address_1(),
         'city' => $order->get_shipping_city(),
         'postcode' => $order->get_shipping_postcode(),
         'country' => $order->get_shipping_country(),
      ),
      'line_items' => $lineitems,
     
   );
   
   
   $raw_response = wp_remote_post( $live_url, 
      array(
         'headers' => array( 'Content-Type' => 'application/json' ),
         'timeout' => 30,                    
         'body' => json_encode( $body ),
      )
   );

}

CodePudding user response:

You could retrieve each product ID from the other site using the API /wp-json/wc/v3/products?sku=xxxxx (GET) inside your loop. The example as below.

add_action( "woocommerce_order_status_processing_to_gls", "transfer_order_woo_api", 9999, 3 );
function transfer_order_woo_api( $order_id, $order ) {
   $remote_keys = "consumer_key=xxxxxx&consumer_secret=xxxxxx";
   $remote_url = "https://testsite.com/wp-json/wc/v3/orders?{$remote_keys}";
   $line_items = array();

   foreach ( $order->get_items() as $item_id => $item ) {
      $quantity = $item->get_quantity();
      $product = $item->get_product();
      $product_sku = $product->get_sku();

      // retrieve product ID by SKU, return product properties
      $remote_get = wp_remote_get("https://testsite.com/wp-json/wc/v3/products?sku={$product_sku}&{$remote_keys}");
      $remote_product = json_decode($remote_get['body'])[0];
      
      $line_item = array(
                       "quantity" => $quantity,
                       "product_id" => $remote_product->id
                      );

      $line_items[] = $line_item;
   }

   // rest of your code here...
}
  • Related