Magento – Magento 2 Rest API get order details from customer id

magento2rest api

I am beginner on a magento rest api, I cannot see in the API files or docs on how to retrieve order details of customer from customer id.

Best Answer

Please find a script that is standolone and can be placed anywhere within your magento files, on my environment it is in the folder

<magento-root>/app/code/Mbs/OrderRepositoryExtension/shell/getlistordersbycustomerid.php 



<?php
    
    $path = __DIR__ . '/../';
    
    while (!file_exists($path. '/vendor/autoload.php') && $i++ < 15) {
        $path .= '../';
    }
    require ($path. '/vendor/autoload.php');
    
    $path = __DIR__ . '/../';
    $zendLibrary = 'vendor/zendframework/zend-http/src/';
    $i = 0;
    
    while (!file_exists($path. $zendLibrary . 'Headers.php') && $i++ < 15) {
        $path .= '../';
    }
    
    require_once $zendLibrary . 'Headers.php';
    require_once $zendLibrary . 'Request.php';
    require_once $zendLibrary . 'Response.php';
    
    
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    // \Magento\Webapi\Model\ServiceMetadata::getServicesConfig to debug the available webapi services
    $params = ['customer_id' => null];
    foreach ($argv as $arg) {
        if (preg_match("%^--(.*?)=(.*?)$%", $arg, $m)) {
            $params[$m[1]] = $m[2];
        }
    }
    
    if (!$params['customer_id'])
        exit("Specify customer id (as --customer_id=_ID_ parameter)\n");
    
    $customer_id = $params['customer_id'];
    
    $token = 'or7ozb3e97x19nk97zylmhspul7niqi3';
    $httpHeaders = new \Zend\Http\Headers();
    $httpHeaders->addHeaders([
        'Authorization' => 'Bearer ' . $token,
        'Accept' => 'application/json',
        'Content-Type' => 'application/json'
    ]);
    
    $request = new \Zend\Http\Request();
    $request->setHeaders($httpHeaders);
    $request->setUri('http://magecertif.test/index.php/rest/V1/orders');
    $request->setMethod(\Zend\Http\Request::METHOD_GET);
    
    //searchCriteria[filterGroups][0][filters][0][field]=created_at&searchCriteria[filterGroups][0][filters][0][value]=2020-06-28&searchCriteria[filterGroups][0][filters][0][conditionType]=gt&searchCriteria[filterGroups][1][filters][0][field]=created_at&searchCriteria[filterGroups][1][filters][0][value]=2020-06-29&searchCriteria[filterGroups][1][filters][0][conditionType]=lt
    
    $params = new \Zend\Stdlib\Parameters([
        'searchCriteria' => [
            'filterGroups' => [
                0 => [
                    'filters' => [
                        0 => [
                            'field' => 'customer_id',
                            'value' => $customer_id,
                            'condition_type' => 'eq'
                        ]
                    ]
                ]
            ],
            'current_page' => 1,
            'page_size' => 10
        ],
    ]);
    
    $request->setQuery($params);
    
    $client = new \Zend\Http\Client();
    $options = [
        'adapter'   => 'Zend\Http\Client\Adapter\Curl',
        'curloptions' => [CURLOPT_FOLLOWLOCATION => true],
        'maxredirects' => 0,
        'timeout' => 30
    ];
    
    $client->setOptions($options);
    
    $response = $client->send($request);
    
    echo $response->getBody();

to call this script: open a terminal and at the root of your magento install, hit php <script_location>/getlistordersbycustomerid.php --customer_id=1

on my environment: php app/code/Mbs/OrderRepositoryExtension/shell/getlistordersbycustomerid.php --customer_id=1

Related Topic