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.

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"
            }
        }
    ]
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.
    – mshakeel
    Commented Aug 17, 2017 at 7:04

Your Answer

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

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