Magento – Magento 1.9 API Performance – Retrieving All Sales Order Info

apimagento-1.9PHPsales-order

I'm creating a page that shows order sales data such as sales revenue, total quantity of orders and also best sellers which consist of products from all sales orders.

The problem is that this takes 20 minutes to run for a database with only 1200 orders. I'm certain this is because an API call needs to be made for each order separately to retrieve the item information:

$orderDetails[] = array($client->salesOrderInfo($sessionId, $orderNumbers[$d]));

Here's an example of my code:

    // API Connection.
    $client = new SoapClient('htps://www.mysite.com/api/v2_soap/?wsdl');
    $sessionId = $client->login('API Username', 'API Password');

    // Filter orders for this year only.
    $filter = array('complex_filter' => array(
           array('key' => 'created_at', 'value' => array('key' => 'gt', 'value' => $currentYear . '2017-01-01 00:00:00'))
       )
    );

    // Call the API and retrieve the orders.
    $orders = $client->salesOrderList($sessionId, $filter);

    // Loop through orders and store order numbers against the $orderNumbers array.
    $orderNumbers = array();
    for ($i = 0; $i < count($orders); $i++) {
        $orderNumbers[] = $orders[$i]->increment_id;
    }

   // Loop through order numbers and store the order details against the $orderDetails array (this seems to be the problem area).
   $orderDetails = array();
   for ($d = 0; $d < count($orderNumbers); $d++) {
      $orderDetails[] = array($client->salesOrderInfo($sessionId, $orderNumbers[$d]));
   }

Is there a way of improving my script so that it performs much better? I cannot find a way of retrieving sales order info for more than one order at a time.

Thanks so much for any help.

Best Answer

Running 1200 individual queries is indeed not great for performance. As it seems you require the information only available via salesOrderInfo I suggest taking a look at creating an extension to Magento which provides an additional API endpoint for you to interact with. In the extension you can structure the mysql query to only provide you with the information you require.

More information on how to create a custom API endpoint can be found here.

Related Topic