Issue
I am developing a theme for a client with woocommerce selling mobile phones wholesale. The client has an account with mobileshop.bz and they have their own system called NATM. I am able to import the products really easily but I need to find a way to send order details from my clients site to his account on mobileshop.
It seems I have to first reserve articles and then create a sales order, the guy's at mobileshop provided me with this code snippet to reserve an article
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://restful.mobileshop.bz/reserveArticle/new/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array('sku' => '','qty' => ''),
CURLOPT_HTTPHEADER => array(
"Authorization: paste in your API key"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
and this to createSalesOrder
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://restful.mobileshop.bz/createSalesOrder/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array('reservation[]' => '','reservation[]' => '','pay_method' => '','insurance' => '','drop_shipping' => '0','drop_ship[name]' => '','drop_ship[address]' => '','drop_ship[postcode]' => '','drop_ship[city]' => '','drop_ship[country]' => '','drop_ship[contact]' => ''),
CURLOPT_HTTPHEADER => array(
"Authorization: paste in your API key"
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
I am just asking how do I integrate this into my custom theme itself, Do I modify the code examples and add it to my functions.php file.
Many thanks, Phillip Dews
Solution
If you want this to fire on every order use one of WooCommerces hooks and place it in your functions.php file.
add_action( 'woocommerce_thankyou', 'so_woocommerce_thankyou' );
function so_woocommerce_thankyou( $order_id ) {
$Order = new WP_Order( $order_id );
// Build your item sku and qty array
$payloadItems = [];
foreach( $Order->get_items() as $item ) {
$product = wc_get_product( $item->get_product_id );
$payloadItems[] = [$product->get_sku(), $item->get_quantity()];
}
// Reserve
$reservations = [];
if( count( $payloadItems ) ) {
foreach( $payloadItems as $item ) {
$reservations[] = reserveArticle( $item[0], $item[1] );
}
}
// Send sales order
$salesOrder = false;
if( count( $reservations ) ) {
$salesOrder = sendSalesOrder( $Order, $reservations );
}
if( $salesOrder !== false ) {
// Success
} else {
// Something went wrong
}
}
function reserveArticle( $sku, $qty ) {
// The cURL request to reserve an article.
// Pipe in the required information into your postfields value return response
}
function sendSalesOrder( $reservation, $Order ) {
// The cURL request to send a sales order.
// Pipe in the required information into your postfields value return response or false on error
}
That's how I would approach it. Might need to be tweaked for specific needs and any errors as it's completely not tested.
Answered By - InvictusMKS