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.
Magento – Magento 2 Rest API get order details from customer id
magento2rest api
Related Solutions
Below is the script for order api.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require ('/vendor/autoload.php');
require('/vendor/zendframework/zend-http/src/Headers.php');
require('/vendor/zendframework/zend-http/src/Request.php');
require('/vendor/zendframework/zend-http/src/Response.php');
/*Token Generated from SYstem Integratio*/
$token = '9kvn45tbopelidqj9ddita9rvyujk1tl';
$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://1270.0.1/magentoce27/index.php/rest/V1/orders/1');
$request->setMethod(\Zend\Http\Request::METHOD_GET);
$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 "<pre>".print_r(json_decode($response->getBody()),true)."</pre>";
?>
Here, is the script for customer api,
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require ('/vendor/autoload.php');
require('/vendor/zendframework/zend-http/src/Headers.php');
require('/vendor/zendframework/zend-http/src/Request.php');
require('/vendor/zendframework/zend-http/src/Response.php');
/*Token Generated from SYstem Integratio*/
$token = '9kvn45tbopelidqj9ddita9rvyujk1tl';
$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://1270.0.1/magentoce27/index.php/rest/V1/customers/1');
$request->setMethod(\Zend\Http\Request::METHOD_GET);
$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 "<pre>".print_r(json_decode($response->getBody()),true)."</pre>";
?>
Kindly note that you might need to do changes in API as per your requirement.
Also you will need to generate token from admin panel and put that token into script and also change url as per your project setup.
Hello Please find the below screen shot for Your Requirement.
First You need to Override the Core files in Your module di.xml
To display those description, meta_description and meta_title.
Note:Copy whole default core file and add the below part in respective File code.
File Path:app/code/extension_Name/Module_name/Model/Product.php
/**
* Get product name
*
* @return string
* @codeCoverageIgnoreStart
*/
public function getDescription()
{
return $this->_getData(self::DESCRIPTION);
}
/**
* Get product name
*
* @return string
* @codeCoverageIgnoreStart
*/
public function getMetaDescription()
{
return $this->_getData(self::META_DESCRIPTION);
}
/**
* Get product name
*
* @return string
* @codeCoverageIgnoreStart
*/
public function getMetaTitle()
{
return $this->_getData(self::META_TITLE);
}
/**
* Set product description
*
* @param string $description
* @return $this
*/
public function setDescription($description)
{
return $this->setData(self::DESCRIPTION, $description);
}
/**
* Set product meta_description
*
* @param string $meta_description
* @return $this
*/
public function setMetaDescription($meta_description)
{
return $this->setData(self::META_DESCRIPTION, $meta_description);
}
/**
* Set product meta_title
*
* @param string $meta_title
* @return $this
*/
public function setMetaTitle($meta_title)
{
return $this->setData(self::META_TITLE, $meta_title);
}
File Path:app/code/extension_Name/Module_name/Api/Data/ProductInterface.php
in the above File just place the below code as per requirement
const DESCRIPTION = 'description';
const META_DESCRIPTION = 'meta_description';
const META_TITLE = 'meta_title';
/**
* Product description
*
* @return string|null
*/
public function getDescription();
/**
* Set product description
*
* @param string $description
* @return $this
*/
public function setDescription($description);
/**
* Product description
*
* @return string|null
*/
public function getMetaDescription();
/**
* Set product meta_description
*
* @param string $meta_description
* @return $this
*/
public function setMetaDescription($meta_description);
/**
* Product description
*
* @return string|null
*/
public function getMetaTitle();
/**
* Set product meta_title
*
* @param string $meta_title
* @return $this
*/
public function setMetaTitle($meta_title);
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
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