Magento 2 how to add image url for the rest api's called V1/carts/mine & V1/carts/cartId
Please find the below screenshots.
enter image description here Please suggest me thanks.
 shivashankar m
 
 2,5064 gold badges29 silver badges53 bronze badges
 
 
 asked Jul 20, 2017 at 13:33
 
 
 
 Nagaraju Kasa 
 
 5,9518 gold badges59 silver badges116 bronze badges
 
 1 Answer 1
Create extension attributes in extension_attributes.xml file like below.
<extension_attributes for="Magento\Quote\Api\Data\CartItemInterface">
 <attribute code="image_url" type="string" />
 </extension_attributes>
Add one observer for event "sales_quote_load_after" in observer add below code.
 use Magento\Framework\Event\ObserverInterface;
 use Magento\Catalog\Api\ProductRepositoryInterfaceFactory as ProductRepository;
 use Magento\Catalog\Helper\ImageFactory as ProductImageHelper;
 use Magento\Store\Model\StoreManagerInterface as StoreManager;
 use Magento\Store\Model\App\Emulation as AppEmulation;
 use Magento\Quote\Api\Data\CartItemExtensionFactory;
 class SalesQuoteLoadAfter implements ObserverInterface
 { 
 /**
 * @var ObjectManagerInterface
 */
 protected $_objectManager;
 /**
 * @var ProductRepository
 */
 protected $productRepository;
 /**
 *@var \Magento\Catalog\Helper\ImageFactory
 */
 protected $productImageHelper;
 /**
 *@var \Magento\Store\Model\StoreManagerInterface
 */
 protected $storeManager;
 /**
 *@var \Magento\Store\Model\App\Emulation
 */
 protected $appEmulation;
 /**
 * @var CartItemExtensionFactory
 */
 protected $extensionFactory;
 /**
 * @param \Magento\Framework\ObjectManagerInterface $objectManager
 * @param ProductRepository $productRepository
 * @param \Magento\Catalog\Helper\ImageFactory
 * @param \Magento\Store\Model\StoreManagerInterface
 * @param \Magento\Store\Model\App\Emulation
 * @param CartItemExtensionFactory $extensionFactory
 */
 public function __construct(
 \Magento\Framework\ObjectManagerInterface $objectManager,
 ProductRepository $productRepository,
 ProductImageHelper $productImageHelper,
 StoreManager $storeManager,
 AppEmulation $appEmulation,
 CartItemExtensionFactory $extensionFactory
 ) {
 $this->_objectManager = $objectManager;
 $this->productRepository = $productRepository;
 $this->productImageHelper = $productImageHelper;
 $this->storeManager = $storeManager;
 $this->appEmulation = $appEmulation;
 $this->extensionFactory = $extensionFactory;
 }
 public function execute(\Magento\Framework\Event\Observer $observer)
 {
 $quote = $observer->getQuote();
 /**
 * Code to add the items attribute to extension_attributes
 */
 foreach ($quote->getAllItems() as $quoteItem) {
 $product = $this->productRepository->create()->getById($quoteItem->getProductId());
 $itemExtAttr = $quoteItem->getExtensionAttributes();
 if ($itemExtAttr === null) {
 $itemExtAttr = $this->extensionFactory->create();
 }
 $imageurl = $this->getImageUrl($product, 'product_thumbnail_image');
 $itemExtAttr->setImageUrl($imageurl);
 $quoteItem->setExtensionAttributes($itemExtAttr);
 }
 return;
 }
 /**
 * Helper function that provides full cache image url
 * @param \Magento\Catalog\Model\Product
 * @return string
 */
 protected function getImageUrl($product, string $imageType = '')
 {
 $storeId = $this->storeManager->getStore()->getId();
 $this->appEmulation->startEnvironmentEmulation($storeId, \Magento\Framework\App\Area::AREA_FRONTEND, true);
 $imageUrl = $imageUrl = $this->productImageHelper->create()->init($product, $imageType)->getUrl();
 $this->appEmulation->stopEnvironmentEmulation();
 return $imageUrl;
 }
 }
You will able to see response something like below
"items": [
 {
 "item_id": 215,
 "sku": "simple",
 "qty": 2,
 "name": "Simple product",
 "price": 840,
 "product_type": "simple",
 "quote_id": "184",
 "extension_attributes": {
 "image_url": "http://local.magento.com/media/catalog/product/cache/image/25x25/e9c3970ab036de70892d86c6d221abfe/c/4/c4.jpg"
 }
 }
 ]
 Nagaraju Kasa
 
 5,9518 gold badges59 silver badges116 bronze badges
 
 
 answered Aug 16, 2017 at 12:00
 
 
 
 manish_khot 
 
 4305 silver badges21 bronze badges
 
 - 
 hi @manish i am getting an error prntscr.com/g96i11Nagaraju Kasa– Nagaraju Kasa2017年08月16日 12:51:13 +00:00Commented Aug 16, 2017 at 12:51
- 
 I have updated my answer with the "use" statements I used above the class.manish_khot– manish_khot2017年08月16日 13:02:02 +00:00Commented Aug 16, 2017 at 13:02
- 
 $this->itemExtensionFactory doesn't exist?Nagaraju Kasa– Nagaraju Kasa2017年08月16日 13:19:00 +00:00Commented Aug 16, 2017 at 13:19
- 
 1Please run the setup:di:compile command and flush the cache. Have you added the attribute in /etc/extension_attributes.xml file?manish_khot– manish_khot2017年08月16日 14:29:12 +00:00Commented Aug 16, 2017 at 14:29
- 
 1Thanks @manish_khot and Nagaraju, it worked for me as well. I am trying to use it for order items as well.mshakeel– mshakeel2017年08月17日 07:04:46 +00:00Commented Aug 17, 2017 at 7:04
default