I have created the API code for transfering orders from one site to other site. Problem is that if we have for example 3 products in the order and 2 matches the SKU and 1 does not, the order contains 2 products instead of 3 on transfer website so in the warehouse the order may be packed incomplete because API skip the not paired product.
The question is if is it possible to SKIP the whole order if it does not match all of the products by the SKU between the both websites.
Thanks in advance
My code is bellow:
add_action( "woocommerce_order_status_processing_to_post", "transfer_order_woo_api", 9999, 3 );
function transfer_order_woo_api( $order_id, $order ) {
$order=wc_get_order($order_id);
$my_var = get_post_meta($order->id, 'warehouse', true);
$my_varparam = 'transfered';
if ($my_var !==$my_varparam){
$remote_keys = "consumer_key=ck_xxx&consumer_secret=cs_xxx";
$live_url = "https://test.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://test.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' => $line_items,
);
$raw_response = wp_remote_post( $live_url,
array(
'headers' => array( 'Content-Type' => 'application/json' ),
'timeout' => 30,
'body' => json_encode( $body ),
)
);
$order->update_meta_data( 'warehouse', 'transfered' );
$order->save();
}
}
CodePudding user response:
add_action( "woocommerce_order_status_processing_to_post", "transfer_order_woo_api", 9999, 3 );
function transfer_order_woo_api( $order_id, $order ) {
$order=wc_get_order($order_id);
$my_var = get_post_meta($order->id, 'warehouse', true);
$my_varparam = 'transfered';
$stop_transfer = false;
if ($my_var !==$my_varparam){
$remote_keys = "consumer_key=ck_xxx&consumer_secret=cs_xxx";
$live_url = "https://test.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://test.com/wp-json/wc/v3/products?sku={$product_sku}&{$remote_keys}");
$remote_product = json_decode($remote_get['body'])[0];
// if product not found at transfer site, set flag and exit loop
if(!$remote_product){
$stop_transfer = true;
break;
}
$line_item = array(
"quantity" => $quantity,
"product_id" => $remote_product->id
);
$line_items[] = $line_item;
}
// check if flag set to true
if($stop_transfer){
// update order metadata here before exit function
return;
}
$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' => $line_items,
);
$raw_response = wp_remote_post( $live_url,
array(
'headers' => array( 'Content-Type' => 'application/json' ),
'timeout' => 30,
'body' => json_encode( $body ),
)
);
$order->update_meta_data( 'warehouse', 'transfered' );
$order->save();
}
}