2

I do want Order details/view rest API which is "rest/V1/orders/1" with image URL of products in an Order.

Thanks.

0

rest/V1/orders/1 does not provide the Product image, You have to implement extension attribute on Magento\Sales\Api\Data\OrderItemInterface.

Create a module.On that module create at {VendorName}/{ModuleName}/etc/extension_attributes.xml and code:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <extension_attributes for="Magento\Sales\Api\Data\OrderItemInterface">
        <attribute code="product_image" type="string">
        </attribute>
    </extension_attributes>
</config>

After that create pLugin class on Magento\Sales\Api\Data\OrderItemInterface. So, defined plugin class on di.xml at {VendorName}/{ModuleName}/etc/.

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Api\Data\OrderItemInterface">
    <plugin disabled="false" name="StackExchange_Magento_Plugin_Magento_Sales_Api_Data_OrderItemInterface" 
            sortOrder="10" type="{VendorName}\{ModuleName}\Plugin\OrderItemPlugin"/>
</type>
</config>

Define Plugin class at {VendorName}/{ModuleName}/Plugin/OrderItemPlugin.php

<?php
namespace {VendorName}\{ModuleName}\Plugin;


class OrderItemPlugin {

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    private $storeManager;

    /**
     * @var \Magento\Catalog\Api\ProductRepositoryInterface
     */
    private $productRepository;

    /**
     * @var \Magento\Sales\Api\Data\OrderItemExtensionFactory
     */
    private $orderItemExtensionFactory;

    public function __construct(
        \Magento\Sales\Api\Data\OrderItemExtensionFactory $orderItemExtensionFactory,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Store\Model\StoreManagerInterface $storeManager     
    ) {

        $this->orderItemExtensionFactory = $orderItemExtensionFactory;
        $this->productRepository = $productRepository;
        $this->storeManager = $storeManager;
    }
    public function afterGetExtensionAttributes(
        \Magento\Sales\Api\Data\OrderItemInterface $subject,
        $result
    ) {

        $imageUrl = $this->getProductImage($subject);
        if(!$imageUrl){
            return $result;
        }

        if($result=== null){
            $orderItemExtension =$this->orderItemExtensionFactory->create();
            $orderItemExtension->setProductImage($imageUrl);
            $subject->setExtensionAttributes($orderItemExtension);
        }else{
            $result->setProductImage($imageUrl);
        }

        return $result;
    }

    private function getProductImage($orderItem)
    {

        $productId = $orderItem->getProductId();
        $storeId = $orderItem->getStoreId();

        if($productId === null || $storeId === null){
            return false;
        }

        try{
            $product = $this->productRepository->getById($productId, false, $storeId);
            return $this->storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)
                    .'catalog/product' . $product->getImage();
        } catch (\Magento\Framework\Exception\NoSuchEntityException $ex) {

        }
        return false;   
    }
}
  • Thanks for the help, it works. – Ankit Sep 14 at 15:27
0

Try this :

<?php
$adminData = array("username" => "admin_username", "password" => "admin_password");

$ch = curl_init("http://siteurl/rest/V1/integration/admin/token"); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($adminData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Length: " . strlen(json_encode($adminData))));
$token = curl_exec($ch);
echo $token;
curl_close($ch); 


//Order collection Rest API
$request = "http://siteurl/rest/V1/orders?searchCriteria=all";
$ch = curl_init($request);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
  • yes Amit is correct it will not work I already tried. – Ankit Sep 14 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.