Rest API to get thumbnail image in shopping cartHow to get product Tax details in cart without customer loginShare the shopping cart content between Magento multi-store websitesshopping cart rule based on custom attributehow or where can i remove add to wishlist from shopping cart?how to add products to shopping cart using soap v2 api then checkout the products in shopping cartMinimum total of items in shopping cart to allow checkoutCart items not updating properly in Magento 2Magento 2 Load REST API cart quote on frontend via URLRemoveing exist item during update items from cart : magento 2How to see the guest-cart in the webpage
My bank got bought out, am I now going to have to start filing tax returns in a different state?
How do I reattach a shelf to the wall when it ripped out of the wall?
Island of Knights, Knaves and Spies
What is purpose of DB Browser(dbbrowser.aspx) under admin tool?
Older movie/show about humans on derelict alien warship which refuels by passing through a star
Complex numbers z=-3-4i polar form
"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?
Are there moral objections to a life motivated purely by money? How to sway a person from this lifestyle?
Where was the County of Thurn und Taxis located?
Was Dennis Ritchie being too modest in this quote about C and Pascal?
Is there metaphorical meaning of "aus der Haft entlassen"?
Is Diceware more secure than a long passphrase?
Crossed out red box fitting tightly around image
Find the identical rows in a matrix
Multiple fireplaces in an apartment building?
A Note on N!
How much cash can I safely carry into the USA and avoid civil forfeiture?
Does a large simulator bay have standard public address announcements?
Does the damage from the Absorb Elements spell apply to your next attack, or to your first attack on your next turn?
What makes accurate emulation of old systems a difficult task?
Contradiction proof for inequality of P and NP?
Do I need to watch Ant-Man and the Wasp and Captain Marvel before watching Avengers: Endgame?
Should the Product Owner dictate what info the UI needs to display?
A strange hotel
Rest API to get thumbnail image in shopping cart
How to get product Tax details in cart without customer loginShare the shopping cart content between Magento multi-store websitesshopping cart rule based on custom attributehow or where can i remove add to wishlist from shopping cart?how to add products to shopping cart using soap v2 api then checkout the products in shopping cartMinimum total of items in shopping cart to allow checkoutCart items not updating properly in Magento 2Magento 2 Load REST API cart quote on frontend via URLRemoveing exist item during update items from cart : magento 2How to see the guest-cart in the webpage
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
In rest api, I am using "rest/V1/carts/1/items" method to get the magento cart items. It works well and the result is
"item_id": 1,
"sku": "sk1",
"qty": 4,
"name": "test",
"price": 10,
"product_type": "simple",
"quote_id": "1"
Any option to get product image in cart
shopping-cart
add a comment |
In rest api, I am using "rest/V1/carts/1/items" method to get the magento cart items. It works well and the result is
"item_id": 1,
"sku": "sk1",
"qty": 4,
"name": "test",
"price": 10,
"product_type": "simple",
"quote_id": "1"
Any option to get product image in cart
shopping-cart
there is no API for the images. you have to build your own
– Philipp Sander
Jan 22 at 14:44
add a comment |
In rest api, I am using "rest/V1/carts/1/items" method to get the magento cart items. It works well and the result is
"item_id": 1,
"sku": "sk1",
"qty": 4,
"name": "test",
"price": 10,
"product_type": "simple",
"quote_id": "1"
Any option to get product image in cart
shopping-cart
In rest api, I am using "rest/V1/carts/1/items" method to get the magento cart items. It works well and the result is
"item_id": 1,
"sku": "sk1",
"qty": 4,
"name": "test",
"price": 10,
"product_type": "simple",
"quote_id": "1"
Any option to get product image in cart
shopping-cart
shopping-cart
asked Jan 22 at 14:32
GaganGagan
916
916
there is no API for the images. you have to build your own
– Philipp Sander
Jan 22 at 14:44
add a comment |
there is no API for the images. you have to build your own
– Philipp Sander
Jan 22 at 14:44
there is no API for the images. you have to build your own
– Philipp Sander
Jan 22 at 14:44
there is no API for the images. you have to build your own
– Philipp Sander
Jan 22 at 14:44
add a comment |
1 Answer
1
active
oldest
votes
Currently magento2 does not provide any single call for this, Here you have to create new module for this.
Magento 2 rest api : Get cart items with images
Follow the Steps to get Product thumbnail Image in Cart through Rest API without POST any values. It will take current thumbnail Image of a product. Rest Url :
Method : GET -> rest/V1/guest-carts/3f260b6e818d2fe56894ed6222e433f8/items
Create a module : code/Vendor_name/Module_name/
registration.php
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Hopescode_Mobileshop',
__DIR__
);
create module.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode. All rights reserved.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Hopescode_Mobileshop" setup_version="1.0.0" />
</config>
create etc/extension_attributes.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="MagentoQuoteApiDataCartItemInterface">
<attribute code="image_url" type="string" />
</extension_attributes>
</config>
create etc/events.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_quote_load_after">
<observer name="hopescode_mobileshop_sales_quote_load_after" instance="HopescodeMobileshopObserverProductInterface" />
</event>
</config>
Create Observer: Vendor_name/Module_name/Observer/
ProductInterface.php
<?php
/**
* Copyright © 2018-2019 Hopescode, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace HopescodeMobileshopObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoCatalogApiProductRepositoryInterfaceFactory as ProductRepository;
use MagentoCatalogHelperImageFactory as ProductImageHelper;
use MagentoStoreModelStoreManagerInterface as StoreManager;
use MagentoStoreModelAppEmulation as AppEmulation;
use MagentoQuoteApiDataCartItemExtensionFactory;
class ProductInterface implements ObserverInterface
/**
* @var ObjectManagerInterface
*/
protected $_objectManager;
/**
* @var ProductRepository
*/
protected $productRepository;
/**
*@var MagentoCatalogHelperImageFactory
*/
protected $productImageHelper;
/**
*@var MagentoStoreModelStoreManagerInterface
*/
protected $storeManager;
/**
*@var MagentoStoreModelAppEmulation
*/
protected $appEmulation;
/**
* @var CartItemExtensionFactory
*/
protected $extensionFactory;
/**
* @param MagentoFrameworkObjectManagerInterface $objectManager
* @param ProductRepository $productRepository
* @param MagentoCatalogHelperImageFactory
* @param MagentoStoreModelStoreManagerInterface
* @param MagentoStoreModelAppEmulation
* @param CartItemExtensionFactory $extensionFactory
*/
public function __construct(
MagentoFrameworkObjectManagerInterface $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(MagentoFrameworkEventObserver $observer, string $imageType = NULL)
$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->productImageHelper->create()->init($product, 'product_thumbnail_image')->setImageFile($product->getThumbnail())->getUrl();
$itemExtAttr->setImageUrl($imageurl);
$quoteItem->setExtensionAttributes($itemExtAttr);
return;
/**
* Helper function that provides full cache image url
* @param MagentoCatalogModelProduct
* @return string
*/
protected function getImageUrl($product, string $imageType = NULL)
$storeId = $this->storeManager->getStore()->getId();
$this->appEmulation->startEnvironmentEmulation($storeId, MagentoFrameworkAppArea::AREA_FRONTEND, true);
$imageUrl = $this->productImageHelper->create()->init($product, $imageType)->getUrl();
$this->appEmulation->stopEnvironmentEmulation();
return $imageUrl;
Output :
[
"item_id": 5,
"sku": "samplepro",
"qty": 1,
"name": "samplepro",
"price": 1500,
"product_type": "simple",
"quote_id": "3f260b6e818d2fe56894ed6222e433f8",
"extension_attributes":
"image_url": "http://localhost/dashboard/myapi/pub/media/catalog/product/cache//beff4985b56e3afdbeabfc89641a4582/n/u/nutro_crunchy_real_apple.jpg"
]
Before checking your out put, If you installed the correct method you can check your var/generation/Magento/Quote/Api/Data/CartItemExtension.php has the value like this:
<?php
namespace MagentoQuoteApiData;
/**
* Extension class for @see MagentoQuoteApiDataCartItemInterface
*/
class CartItemExtension extends MagentoFrameworkApiAbstractSimpleObject implements MagentoQuoteApiDataCartItemExtensionInterface
null
*/
public function getImageUrl()
return $this->_get('image_url');
/**
* @param string $imageUrl
* @return $this
*/
public function setImageUrl($imageUrl)
$this->setData('image_url', $imageUrl);
return $this;
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "479"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f258769%2frest-api-to-get-thumbnail-image-in-shopping-cart%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Currently magento2 does not provide any single call for this, Here you have to create new module for this.
Magento 2 rest api : Get cart items with images
Follow the Steps to get Product thumbnail Image in Cart through Rest API without POST any values. It will take current thumbnail Image of a product. Rest Url :
Method : GET -> rest/V1/guest-carts/3f260b6e818d2fe56894ed6222e433f8/items
Create a module : code/Vendor_name/Module_name/
registration.php
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Hopescode_Mobileshop',
__DIR__
);
create module.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode. All rights reserved.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Hopescode_Mobileshop" setup_version="1.0.0" />
</config>
create etc/extension_attributes.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="MagentoQuoteApiDataCartItemInterface">
<attribute code="image_url" type="string" />
</extension_attributes>
</config>
create etc/events.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_quote_load_after">
<observer name="hopescode_mobileshop_sales_quote_load_after" instance="HopescodeMobileshopObserverProductInterface" />
</event>
</config>
Create Observer: Vendor_name/Module_name/Observer/
ProductInterface.php
<?php
/**
* Copyright © 2018-2019 Hopescode, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace HopescodeMobileshopObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoCatalogApiProductRepositoryInterfaceFactory as ProductRepository;
use MagentoCatalogHelperImageFactory as ProductImageHelper;
use MagentoStoreModelStoreManagerInterface as StoreManager;
use MagentoStoreModelAppEmulation as AppEmulation;
use MagentoQuoteApiDataCartItemExtensionFactory;
class ProductInterface implements ObserverInterface
/**
* @var ObjectManagerInterface
*/
protected $_objectManager;
/**
* @var ProductRepository
*/
protected $productRepository;
/**
*@var MagentoCatalogHelperImageFactory
*/
protected $productImageHelper;
/**
*@var MagentoStoreModelStoreManagerInterface
*/
protected $storeManager;
/**
*@var MagentoStoreModelAppEmulation
*/
protected $appEmulation;
/**
* @var CartItemExtensionFactory
*/
protected $extensionFactory;
/**
* @param MagentoFrameworkObjectManagerInterface $objectManager
* @param ProductRepository $productRepository
* @param MagentoCatalogHelperImageFactory
* @param MagentoStoreModelStoreManagerInterface
* @param MagentoStoreModelAppEmulation
* @param CartItemExtensionFactory $extensionFactory
*/
public function __construct(
MagentoFrameworkObjectManagerInterface $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(MagentoFrameworkEventObserver $observer, string $imageType = NULL)
$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->productImageHelper->create()->init($product, 'product_thumbnail_image')->setImageFile($product->getThumbnail())->getUrl();
$itemExtAttr->setImageUrl($imageurl);
$quoteItem->setExtensionAttributes($itemExtAttr);
return;
/**
* Helper function that provides full cache image url
* @param MagentoCatalogModelProduct
* @return string
*/
protected function getImageUrl($product, string $imageType = NULL)
$storeId = $this->storeManager->getStore()->getId();
$this->appEmulation->startEnvironmentEmulation($storeId, MagentoFrameworkAppArea::AREA_FRONTEND, true);
$imageUrl = $this->productImageHelper->create()->init($product, $imageType)->getUrl();
$this->appEmulation->stopEnvironmentEmulation();
return $imageUrl;
Output :
[
"item_id": 5,
"sku": "samplepro",
"qty": 1,
"name": "samplepro",
"price": 1500,
"product_type": "simple",
"quote_id": "3f260b6e818d2fe56894ed6222e433f8",
"extension_attributes":
"image_url": "http://localhost/dashboard/myapi/pub/media/catalog/product/cache//beff4985b56e3afdbeabfc89641a4582/n/u/nutro_crunchy_real_apple.jpg"
]
Before checking your out put, If you installed the correct method you can check your var/generation/Magento/Quote/Api/Data/CartItemExtension.php has the value like this:
<?php
namespace MagentoQuoteApiData;
/**
* Extension class for @see MagentoQuoteApiDataCartItemInterface
*/
class CartItemExtension extends MagentoFrameworkApiAbstractSimpleObject implements MagentoQuoteApiDataCartItemExtensionInterface
null
*/
public function getImageUrl()
return $this->_get('image_url');
/**
* @param string $imageUrl
* @return $this
*/
public function setImageUrl($imageUrl)
$this->setData('image_url', $imageUrl);
return $this;
add a comment |
Currently magento2 does not provide any single call for this, Here you have to create new module for this.
Magento 2 rest api : Get cart items with images
Follow the Steps to get Product thumbnail Image in Cart through Rest API without POST any values. It will take current thumbnail Image of a product. Rest Url :
Method : GET -> rest/V1/guest-carts/3f260b6e818d2fe56894ed6222e433f8/items
Create a module : code/Vendor_name/Module_name/
registration.php
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Hopescode_Mobileshop',
__DIR__
);
create module.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode. All rights reserved.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Hopescode_Mobileshop" setup_version="1.0.0" />
</config>
create etc/extension_attributes.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="MagentoQuoteApiDataCartItemInterface">
<attribute code="image_url" type="string" />
</extension_attributes>
</config>
create etc/events.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_quote_load_after">
<observer name="hopescode_mobileshop_sales_quote_load_after" instance="HopescodeMobileshopObserverProductInterface" />
</event>
</config>
Create Observer: Vendor_name/Module_name/Observer/
ProductInterface.php
<?php
/**
* Copyright © 2018-2019 Hopescode, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace HopescodeMobileshopObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoCatalogApiProductRepositoryInterfaceFactory as ProductRepository;
use MagentoCatalogHelperImageFactory as ProductImageHelper;
use MagentoStoreModelStoreManagerInterface as StoreManager;
use MagentoStoreModelAppEmulation as AppEmulation;
use MagentoQuoteApiDataCartItemExtensionFactory;
class ProductInterface implements ObserverInterface
/**
* @var ObjectManagerInterface
*/
protected $_objectManager;
/**
* @var ProductRepository
*/
protected $productRepository;
/**
*@var MagentoCatalogHelperImageFactory
*/
protected $productImageHelper;
/**
*@var MagentoStoreModelStoreManagerInterface
*/
protected $storeManager;
/**
*@var MagentoStoreModelAppEmulation
*/
protected $appEmulation;
/**
* @var CartItemExtensionFactory
*/
protected $extensionFactory;
/**
* @param MagentoFrameworkObjectManagerInterface $objectManager
* @param ProductRepository $productRepository
* @param MagentoCatalogHelperImageFactory
* @param MagentoStoreModelStoreManagerInterface
* @param MagentoStoreModelAppEmulation
* @param CartItemExtensionFactory $extensionFactory
*/
public function __construct(
MagentoFrameworkObjectManagerInterface $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(MagentoFrameworkEventObserver $observer, string $imageType = NULL)
$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->productImageHelper->create()->init($product, 'product_thumbnail_image')->setImageFile($product->getThumbnail())->getUrl();
$itemExtAttr->setImageUrl($imageurl);
$quoteItem->setExtensionAttributes($itemExtAttr);
return;
/**
* Helper function that provides full cache image url
* @param MagentoCatalogModelProduct
* @return string
*/
protected function getImageUrl($product, string $imageType = NULL)
$storeId = $this->storeManager->getStore()->getId();
$this->appEmulation->startEnvironmentEmulation($storeId, MagentoFrameworkAppArea::AREA_FRONTEND, true);
$imageUrl = $this->productImageHelper->create()->init($product, $imageType)->getUrl();
$this->appEmulation->stopEnvironmentEmulation();
return $imageUrl;
Output :
[
"item_id": 5,
"sku": "samplepro",
"qty": 1,
"name": "samplepro",
"price": 1500,
"product_type": "simple",
"quote_id": "3f260b6e818d2fe56894ed6222e433f8",
"extension_attributes":
"image_url": "http://localhost/dashboard/myapi/pub/media/catalog/product/cache//beff4985b56e3afdbeabfc89641a4582/n/u/nutro_crunchy_real_apple.jpg"
]
Before checking your out put, If you installed the correct method you can check your var/generation/Magento/Quote/Api/Data/CartItemExtension.php has the value like this:
<?php
namespace MagentoQuoteApiData;
/**
* Extension class for @see MagentoQuoteApiDataCartItemInterface
*/
class CartItemExtension extends MagentoFrameworkApiAbstractSimpleObject implements MagentoQuoteApiDataCartItemExtensionInterface
null
*/
public function getImageUrl()
return $this->_get('image_url');
/**
* @param string $imageUrl
* @return $this
*/
public function setImageUrl($imageUrl)
$this->setData('image_url', $imageUrl);
return $this;
add a comment |
Currently magento2 does not provide any single call for this, Here you have to create new module for this.
Magento 2 rest api : Get cart items with images
Follow the Steps to get Product thumbnail Image in Cart through Rest API without POST any values. It will take current thumbnail Image of a product. Rest Url :
Method : GET -> rest/V1/guest-carts/3f260b6e818d2fe56894ed6222e433f8/items
Create a module : code/Vendor_name/Module_name/
registration.php
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Hopescode_Mobileshop',
__DIR__
);
create module.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode. All rights reserved.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Hopescode_Mobileshop" setup_version="1.0.0" />
</config>
create etc/extension_attributes.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="MagentoQuoteApiDataCartItemInterface">
<attribute code="image_url" type="string" />
</extension_attributes>
</config>
create etc/events.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_quote_load_after">
<observer name="hopescode_mobileshop_sales_quote_load_after" instance="HopescodeMobileshopObserverProductInterface" />
</event>
</config>
Create Observer: Vendor_name/Module_name/Observer/
ProductInterface.php
<?php
/**
* Copyright © 2018-2019 Hopescode, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace HopescodeMobileshopObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoCatalogApiProductRepositoryInterfaceFactory as ProductRepository;
use MagentoCatalogHelperImageFactory as ProductImageHelper;
use MagentoStoreModelStoreManagerInterface as StoreManager;
use MagentoStoreModelAppEmulation as AppEmulation;
use MagentoQuoteApiDataCartItemExtensionFactory;
class ProductInterface implements ObserverInterface
/**
* @var ObjectManagerInterface
*/
protected $_objectManager;
/**
* @var ProductRepository
*/
protected $productRepository;
/**
*@var MagentoCatalogHelperImageFactory
*/
protected $productImageHelper;
/**
*@var MagentoStoreModelStoreManagerInterface
*/
protected $storeManager;
/**
*@var MagentoStoreModelAppEmulation
*/
protected $appEmulation;
/**
* @var CartItemExtensionFactory
*/
protected $extensionFactory;
/**
* @param MagentoFrameworkObjectManagerInterface $objectManager
* @param ProductRepository $productRepository
* @param MagentoCatalogHelperImageFactory
* @param MagentoStoreModelStoreManagerInterface
* @param MagentoStoreModelAppEmulation
* @param CartItemExtensionFactory $extensionFactory
*/
public function __construct(
MagentoFrameworkObjectManagerInterface $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(MagentoFrameworkEventObserver $observer, string $imageType = NULL)
$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->productImageHelper->create()->init($product, 'product_thumbnail_image')->setImageFile($product->getThumbnail())->getUrl();
$itemExtAttr->setImageUrl($imageurl);
$quoteItem->setExtensionAttributes($itemExtAttr);
return;
/**
* Helper function that provides full cache image url
* @param MagentoCatalogModelProduct
* @return string
*/
protected function getImageUrl($product, string $imageType = NULL)
$storeId = $this->storeManager->getStore()->getId();
$this->appEmulation->startEnvironmentEmulation($storeId, MagentoFrameworkAppArea::AREA_FRONTEND, true);
$imageUrl = $this->productImageHelper->create()->init($product, $imageType)->getUrl();
$this->appEmulation->stopEnvironmentEmulation();
return $imageUrl;
Output :
[
"item_id": 5,
"sku": "samplepro",
"qty": 1,
"name": "samplepro",
"price": 1500,
"product_type": "simple",
"quote_id": "3f260b6e818d2fe56894ed6222e433f8",
"extension_attributes":
"image_url": "http://localhost/dashboard/myapi/pub/media/catalog/product/cache//beff4985b56e3afdbeabfc89641a4582/n/u/nutro_crunchy_real_apple.jpg"
]
Before checking your out put, If you installed the correct method you can check your var/generation/Magento/Quote/Api/Data/CartItemExtension.php has the value like this:
<?php
namespace MagentoQuoteApiData;
/**
* Extension class for @see MagentoQuoteApiDataCartItemInterface
*/
class CartItemExtension extends MagentoFrameworkApiAbstractSimpleObject implements MagentoQuoteApiDataCartItemExtensionInterface
null
*/
public function getImageUrl()
return $this->_get('image_url');
/**
* @param string $imageUrl
* @return $this
*/
public function setImageUrl($imageUrl)
$this->setData('image_url', $imageUrl);
return $this;
Currently magento2 does not provide any single call for this, Here you have to create new module for this.
Magento 2 rest api : Get cart items with images
Follow the Steps to get Product thumbnail Image in Cart through Rest API without POST any values. It will take current thumbnail Image of a product. Rest Url :
Method : GET -> rest/V1/guest-carts/3f260b6e818d2fe56894ed6222e433f8/items
Create a module : code/Vendor_name/Module_name/
registration.php
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'Hopescode_Mobileshop',
__DIR__
);
create module.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode. All rights reserved.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Hopescode_Mobileshop" setup_version="1.0.0" />
</config>
create etc/extension_attributes.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="MagentoQuoteApiDataCartItemInterface">
<attribute code="image_url" type="string" />
</extension_attributes>
</config>
create etc/events.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2018-2019 Hopescode, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_quote_load_after">
<observer name="hopescode_mobileshop_sales_quote_load_after" instance="HopescodeMobileshopObserverProductInterface" />
</event>
</config>
Create Observer: Vendor_name/Module_name/Observer/
ProductInterface.php
<?php
/**
* Copyright © 2018-2019 Hopescode, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace HopescodeMobileshopObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoCatalogApiProductRepositoryInterfaceFactory as ProductRepository;
use MagentoCatalogHelperImageFactory as ProductImageHelper;
use MagentoStoreModelStoreManagerInterface as StoreManager;
use MagentoStoreModelAppEmulation as AppEmulation;
use MagentoQuoteApiDataCartItemExtensionFactory;
class ProductInterface implements ObserverInterface
/**
* @var ObjectManagerInterface
*/
protected $_objectManager;
/**
* @var ProductRepository
*/
protected $productRepository;
/**
*@var MagentoCatalogHelperImageFactory
*/
protected $productImageHelper;
/**
*@var MagentoStoreModelStoreManagerInterface
*/
protected $storeManager;
/**
*@var MagentoStoreModelAppEmulation
*/
protected $appEmulation;
/**
* @var CartItemExtensionFactory
*/
protected $extensionFactory;
/**
* @param MagentoFrameworkObjectManagerInterface $objectManager
* @param ProductRepository $productRepository
* @param MagentoCatalogHelperImageFactory
* @param MagentoStoreModelStoreManagerInterface
* @param MagentoStoreModelAppEmulation
* @param CartItemExtensionFactory $extensionFactory
*/
public function __construct(
MagentoFrameworkObjectManagerInterface $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(MagentoFrameworkEventObserver $observer, string $imageType = NULL)
$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->productImageHelper->create()->init($product, 'product_thumbnail_image')->setImageFile($product->getThumbnail())->getUrl();
$itemExtAttr->setImageUrl($imageurl);
$quoteItem->setExtensionAttributes($itemExtAttr);
return;
/**
* Helper function that provides full cache image url
* @param MagentoCatalogModelProduct
* @return string
*/
protected function getImageUrl($product, string $imageType = NULL)
$storeId = $this->storeManager->getStore()->getId();
$this->appEmulation->startEnvironmentEmulation($storeId, MagentoFrameworkAppArea::AREA_FRONTEND, true);
$imageUrl = $this->productImageHelper->create()->init($product, $imageType)->getUrl();
$this->appEmulation->stopEnvironmentEmulation();
return $imageUrl;
Output :
[
"item_id": 5,
"sku": "samplepro",
"qty": 1,
"name": "samplepro",
"price": 1500,
"product_type": "simple",
"quote_id": "3f260b6e818d2fe56894ed6222e433f8",
"extension_attributes":
"image_url": "http://localhost/dashboard/myapi/pub/media/catalog/product/cache//beff4985b56e3afdbeabfc89641a4582/n/u/nutro_crunchy_real_apple.jpg"
]
Before checking your out put, If you installed the correct method you can check your var/generation/Magento/Quote/Api/Data/CartItemExtension.php has the value like this:
<?php
namespace MagentoQuoteApiData;
/**
* Extension class for @see MagentoQuoteApiDataCartItemInterface
*/
class CartItemExtension extends MagentoFrameworkApiAbstractSimpleObject implements MagentoQuoteApiDataCartItemExtensionInterface
null
*/
public function getImageUrl()
return $this->_get('image_url');
/**
* @param string $imageUrl
* @return $this
*/
public function setImageUrl($imageUrl)
$this->setData('image_url', $imageUrl);
return $this;
answered Apr 21 at 14:40
N.ameenN.ameen
866
866
add a comment |
add a comment |
Thanks for contributing an answer to Magento Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f258769%2frest-api-to-get-thumbnail-image-in-shopping-cart%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
there is no API for the images. you have to build your own
– Philipp Sander
Jan 22 at 14:44