John Davidson

php - Woocommerce REST API - Skip order transfer if order on the live website does not match with the product on other website

0 comments
Message:


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();
}
}


My Answer:
To skip the whole order transfer if any of the products in the order do not match by SKU between the two websites, you can add a check before initiating the transfer process. Here's how you can modify your existing code:

php
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();
$skip_order_transfer = false; // Flag to skip order transfer

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 (empty($remote_product)) {
$skip_order_transfer = true; // Set flag to skip order transfer
break; // Exit loop if any product does not match
}

$line_item = array(
"quantity" => $quantity,
"product_id" => $remote_product->id
);

$line_items[] = $line_item;
}

if (!$skip_order_transfer) {
// Proceed with order transfer
$body = array(
'status' => 'processing',
// Add other order data here
'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();
}
}
}


In the above code, I have added a flag `$skip_order_transfer` which is set to `true` if any product in the order does not match by SKU. If the flag is `true`, the order transfer process is skipped. This way, the whole order will be skipped if any product does not match between the websites.

Rate this post

4 of 5 based on 3094 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us