Magento 2 – Add Image to Cart API Response Object apicartcheckoutmagento2rest api Magento 2 how to add image url for the rest api's called V1/carts/mine & V1/carts/cartId Please find the below screenshots. Please suggest me thanks. Best Answer 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" } } ] Related SolutionsMagento 2 – Fix Error in Rest API Guest-Cart [POST] V1/guest-carts/{cart-id}/items Add item to guest cart [POST] http://.../Magento2/index.php/rest/V1/guest-carts/ - gives cart Id. [POST] http://.../Magento2/index.php/rest/V1/guest-carts/a03da1c69a5b1b57a405d05a31e16ce5/items – Adds an item to cart. Body - { "cartItem": { "quote_id": "a03da1c69a5b1b57a405d05a31e16ce5", "sku": "Mobile1", "qty": 10 } } Magento – MAGENTO 2 REST API Checkout payment using credit card Please try the below code. "paymentMethod": { "method": "custom_pay", "additional_information": { "cc_cid": "000", "cc_type": "VI", "cc_exp_year": "2017", "cc_exp_month": "2", "cc_number": "xxxxxxxxxxxxxxxx" } } I hope it will be help you. Related TopicMagento – Magento 2.2 REST API Add Item to cart issueMagento – Magento 2 regsitered user add to cart APIMagento – Zero cart price Issue in M2 ADD TO CART rest API in case of new customersMagento – Magento2 REST API – Cannot create cart/quote for customerMagento – Magento 2 REST API: Empty cart or delete multiple items from the cart
Best Answer
Create extension attributes in extension_attributes.xml file like below.
Add one observer for event "sales_quote_load_after" in observer add below code.
You will able to see response something like below