2

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

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

1 Answer 1

6

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
16
  • hi @manish i am getting an error prntscr.com/g96i11 Commented Aug 16, 2017 at 12:51
  • I have updated my answer with the "use" statements I used above the class. Commented Aug 16, 2017 at 13:02
  • $this->itemExtensionFactory doesn't exist? Commented Aug 16, 2017 at 13:19
  • 1
    Please run the setup:di:compile command and flush the cache. Have you added the attribute in /etc/extension_attributes.xml file? Commented Aug 16, 2017 at 14:29
  • 1
    Thanks @manish_khot and Nagaraju, it worked for me as well. I am trying to use it for order items as well. Commented Aug 17, 2017 at 7:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.