skip to Main Content

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

2

Answers


  1. I can see this being one in two ways.

    1. Use JS and make an AJAX call once that is complete you can submit the form.
    2. Encapsulate the Curl request on methods and chuck them in when a user press submit.

    Not a concrete idea but hopefully it can give you can idea on how to approach it.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search