How to Get Product Image data in Cart API Magento 2?Magento 2.2 REST API Add Item to cart issueMagento 2 regsitered user add to cart APIMagento 2 Add Image To cart API response objectHow to get cart items bundle options (set sku, name) and get in which extension of rest API v1/carts/mine/items in magento 2Magento2 REST API - Cannot create cart/quote for customerHow to edit cart item in Mageto 2 REST API?Get cart info REST APIMagento 2 Get cart detail API doesn't show subtotal and totalHow to get Image data in MagentoQuoteApiCartTotalRepositoryInterface using extension_attributes?Magento API for mobile application
Importance sampling estimation of power function
How do I write "Show, Don't Tell" as an Asperger?
What happened to all the nuclear material being smuggled after the fall of the USSR?
On the Twin Paradox Again
Is it possible for people to live in the eye of a permanent hypercane?
Will TSA allow me to carry a Continuous Positive Airway Pressure (CPAP)/sleep apnea device?
Why did a party with more votes get fewer seats in the 2019 European Parliament election in Denmark?
How do photons get into the eyes?
Are the AT-AT's from "Empire Strikes Back" a deliberate reference to Mecha?
Did Darth Vader wear the same suit for 20+ years?
Accidentally renamed tar.gz file to a non tar.gz file, will my file be messed up
Completing the square to find if quadratic form is positive definite.
What can plausibly explain many of my very long and low-tech bridges?
How can I instantiate a lambda closure type in C++11/14?
Does Peach's float negate shorthop knockback multipliers?
In this example, which path would a monster affected by the Dissonant Whispers spell take?
Smooth switching between 12v batteries, with toggle switch
How bad would a partial hash leak be, realistically?
How to skip replacing first occurrence of a character in each line?
My coworkers think I had a long honeymoon. Actually I was diagnosed with cancer. How do I talk about it?
Traffic law UK, pedestrians
What is the advantage of carrying a tripod and ND-filters when you could use image stacking instead?
What happens if you do emergency landing on a US base in middle of the ocean?
Why is the relationship between frequency and pitch exponential?
How to Get Product Image data in Cart API Magento 2?
Magento 2.2 REST API Add Item to cart issueMagento 2 regsitered user add to cart APIMagento 2 Add Image To cart API response objectHow to get cart items bundle options (set sku, name) and get in which extension of rest API v1/carts/mine/items in magento 2Magento2 REST API - Cannot create cart/quote for customerHow to edit cart item in Mageto 2 REST API?Get cart info REST APIMagento 2 Get cart detail API doesn't show subtotal and totalHow to get Image data in MagentoQuoteApiCartTotalRepositoryInterface using extension_attributes?Magento API for mobile application
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
GET /V1/carts/mine
This API Returns information for the cart for a specified customer.
- How to get Cart Item Images
- Because in Mobile Application we show Product Images in CART - so how
Can I get Product Images - Item wise in/V1/carts/mine
REST API?
magento2 cart product-images rest-api
add a comment |
GET /V1/carts/mine
This API Returns information for the cart for a specified customer.
- How to get Cart Item Images
- Because in Mobile Application we show Product Images in CART - so how
Can I get Product Images - Item wise in/V1/carts/mine
REST API?
magento2 cart product-images rest-api
add a comment |
GET /V1/carts/mine
This API Returns information for the cart for a specified customer.
- How to get Cart Item Images
- Because in Mobile Application we show Product Images in CART - so how
Can I get Product Images - Item wise in/V1/carts/mine
REST API?
magento2 cart product-images rest-api
GET /V1/carts/mine
This API Returns information for the cart for a specified customer.
- How to get Cart Item Images
- Because in Mobile Application we show Product Images in CART - so how
Can I get Product Images - Item wise in/V1/carts/mine
REST API?
magento2 cart product-images rest-api
magento2 cart product-images rest-api
asked Dec 8 '18 at 6:37
AadityaAaditya
4,94321243
4,94321243
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can try below code for this. Although answer of @Supravat is correct but I am posting this as per my approach.
extension_attributes.xml
<?xml version="1.0"?>
<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" type="string" />
</extension_attributes>
</config>
di.xml //Define it under webapi_rest folder so that it will come in action for API calls only
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoQuoteApiCartRepositoryInterface">
<plugin name="add_more_info" type="VendorModulePluginQuotePlugin" sortOrder="10" />
</type>
</config>
QuotePlugin.php
<?php
/**
* Copyright © 2018 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace VendorModulePlugin;
use MagentoQuoteApiDataCartInterface;
class QuotePlugin
/**
* @var MagentoQuoteApiDataCartItemExtensionFactory
*/
protected $cartItemExtension;
/**
* @var MagentoCatalogApiProductRepositoryInterface
*/
protected $productRepository;
/**
* @param MagentoQuoteApiDataCartItemExtensionFactory $cartItemExtension
* @param MagentoCatalogApiProductRepositoryInterface $productRepository
*/
public function __construct(
MagentoQuoteApiDataCartItemExtensionFactory $cartItemExtension,
MagentoCatalogApiProductRepositoryInterfaceFactory $productRepository )
$this->cartItemExtension = $cartItemExtension;
$this->productRepository = $productRepository;
/**
* Add attribute values
*
* @param MagentoQuoteApiCartRepositoryInterface $subject,
* @param $quote
* @return $quoteData
*/
public function afterGet(
MagentoQuoteApiCartRepositoryInterface $subject, $quote
)
$quoteData = $this->setAttributeValue($quote);
return $quoteData;
/**
* Add attribute values
*
* @param MagentoQuoteApiCartRepositoryInterface $subject,
* @param $quote
* @return $quoteData
*/
public function afterGetActiveForCustomer(
MagentoQuoteApiCartRepositoryInterface $subject, $quote
)
$quoteData = $this->setAttributeValue($quote);
return $quoteData;
/**
* set value of attributes
*
* @param $product,
* @return $extensionAttributes
*/
private function setAttributeValue($quote)
$data = [];
if ($quote->getItemsCount())
foreach ($quote->getItems() as $item)
$data = [];
$extensionAttributes = $item->getExtensionAttributes();
if ($extensionAttributes === null)
$extensionAttributes = $this->cartItemExtension->create();
$productData = $this->productRepository->create()->get($item->getSku());
$extensionAttributes->setImage($productData->getThumbnail());
$item->setExtensionAttributes($extensionAttributes);
return $quote;
To modify any object in observer not a good approach so I did it in this way. Hope it'll work for you. Let me know if you need further help.
Thanks for the answer + @Ram - I'll try and update you back :)
– Aaditya
Dec 8 '18 at 11:02
sure @AdityaShah :)
– Ramkishan Suthar
Dec 8 '18 at 11:33
1
welcome @AdityaShah Happy Coding :)
– Ramkishan Suthar
Dec 10 '18 at 11:50
Can I call the same plugin for CartTotalRepositoryInterface ?
– Aaditya
Dec 10 '18 at 12:06
1
For this you need to create new plugin and new extension attribute of TotalItemInterface because TotalRepositoryInterface doesn't return CartItemInterface.
– Ramkishan Suthar
Dec 10 '18 at 12:11
|
show 9 more comments
GET /V1/carts/mine
Implementation Notes
Returns information for the cart for a specified customer.
Response:
"id": 0,
"created_at": "string",
"updated_at": "string",
"converted_at": "string",
"is_active": true,
"is_virtual": true,
"items": [
"item_id": 0,
"sku": "string",
"qty": 0,
"name": "string",
"price": 0,
"product_type": "string",
"quote_id": "string",
"product_option":
"extension_attributes":
"custom_options": [
"option_id": "string",
"option_value": "string",
"extension_attributes":
"file_info":
"base64_encoded_data": "string",
"type": "string",
"name": "string"
],
"bundle_options": [
"option_id": 0,
"option_qty": 0,
"option_selections": [
0
],
"extension_attributes":
],
"configurable_item_options": [
"option_id": "string",
"option_value": 0,
"extension_attributes":
],
"downloadable_option":
"downloadable_links": [
0
]
,
"giftcard_item_option":
"giftcard_amount": "string",
"custom_giftcard_amount": 0,
"giftcard_sender_name": "string",
"giftcard_recipient_name": "string",
"giftcard_sender_email": "string",
"giftcard_recipient_email": "string",
"giftcard_message": "string",
"extension_attributes":
,
"extension_attributes":
"negotiable_quote_item":
"item_id": 0,
"original_price": 0,
"original_tax_amount": 0,
"original_discount_amount": 0,
"extension_attributes":
],
"items_count": 0,
"items_qty": 0,
"customer":
"id": 0,
"group_id": 0,
"default_billing": "string",
"default_shipping": "string",
"confirmation": "string",
"created_at": "string",
"updated_at": "string",
"created_in": "string",
"dob": "string",
"email": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"gender": 0,
"store_id": 0,
"taxvat": "string",
"website_id": 0,
"addresses": [
"id": 0,
"customer_id": 0,
"region":
"region_code": "string",
"region": "string",
"region_id": 0,
"extension_attributes":
,
"region_id": 0,
"country_id": "string",
"street": [
"string"
],
"company": "string",
"telephone": "string",
"fax": "string",
"postcode": "string",
"city": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"vat_id": "string",
"default_shipping": true,
"default_billing": true,
"extension_attributes": ,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
],
"disable_auto_group_change": 0,
"extension_attributes":
"company_attributes":
"customer_id": 0,
"company_id": 0,
"job_title": "string",
"status": 0,
"telephone": "string",
"extension_attributes":
,
"is_subscribed": true
,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
,
"billing_address":
"id": 0,
"region": "string",
"region_id": 0,
"region_code": "string",
"country_id": "string",
"street": [
"string"
],
"company": "string",
"telephone": "string",
"fax": "string",
"postcode": "string",
"city": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"vat_id": "string",
"customer_id": 0,
"email": "string",
"same_as_billing": 0,
"customer_address_id": 0,
"save_in_address_book": 0,
"extension_attributes":
"gift_registry_id": 0
,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
,
"reserved_order_id": 0,
"orig_order_id": 0,
"currency":
"global_currency_code": "string",
"base_currency_code": "string",
"store_currency_code": "string",
"quote_currency_code": "string",
"store_to_base_rate": 0,
"store_to_quote_rate": 0,
"base_to_global_rate": 0,
"base_to_quote_rate": 0,
"extension_attributes":
,
"customer_is_guest": true,
"customer_note": "string",
"customer_note_notify": true,
"customer_tax_class_id": 0,
"store_id": 0,
"extension_attributes":
"shipping_assignments": [
"shipping":
"address":
"id": 0,
"region": "string",
"region_id": 0,
"region_code": "string",
"country_id": "string",
"street": [
"string"
],
"company": "string",
"telephone": "string",
"fax": "string",
"postcode": "string",
"city": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"vat_id": "string",
"customer_id": 0,
"email": "string",
"same_as_billing": 0,
"customer_address_id": 0,
"save_in_address_book": 0,
"extension_attributes":
"gift_registry_id": 0
,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
,
"method": "string",
"extension_attributes":
,
"items": [
"item_id": 0,
"sku": "string",
"qty": 0,
"name": "string",
"price": 0,
"product_type": "string",
"quote_id": "string",
"product_option":
"extension_attributes":
"custom_options": [
"option_id": "string",
"option_value": "string",
"extension_attributes":
"file_info":
"base64_encoded_data": "string",
"type": "string",
"name": "string"
],
"bundle_options": [
"option_id": 0,
"option_qty": 0,
"option_selections": [
0
],
"extension_attributes":
],
"configurable_item_options": [
"option_id": "string",
"option_value": 0,
"extension_attributes":
],
"downloadable_option":
"downloadable_links": [
0
]
,
"giftcard_item_option":
"giftcard_amount": "string",
"custom_giftcard_amount": 0,
"giftcard_sender_name": "string",
"giftcard_recipient_name": "string",
"giftcard_sender_email": "string",
"giftcard_recipient_email": "string",
"giftcard_message": "string",
"extension_attributes":
,
"extension_attributes":
"negotiable_quote_item":
"item_id": 0,
"original_price": 0,
"original_tax_amount": 0,
"original_discount_amount": 0,
"extension_attributes":
],
"extension_attributes":
],
"negotiable_quote":
"quote_id": 0,
"is_regular_quote": true,
"status": "string",
"negotiated_price_type": 0,
"negotiated_price_value": 0,
"shipping_price": 0,
"quote_name": "string",
"expiration_period": "string",
"email_notification_status": 0,
"has_unconfirmed_changes": true,
"is_shipping_tax_changed": true,
"is_customer_price_changed": true,
"notifications": 0,
"applied_rule_ids": "string",
"is_address_draft": true,
"deleted_sku": "string",
"creator_id": 0,
"creator_type": 0,
"original_total_price": 0,
"base_original_total_price": 0,
"negotiated_total_price": 0,
"base_negotiated_total_price": 0,
"extension_attributes":
If you see the above response , there is no product image information. So we should call an API for it. You should call GET /V1/products/:sku/media
to retrieve for a product's image.
For for information please see here :
Magento 2 REST APIs
[Edit]
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/Mocule_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;
I want those image data in /V1/carts/mine API itself. I am aware about getbyID and sku APIs :)
– Aaditya
Dec 8 '18 at 8:46
@AdityaShah, Then you should develop a custom module for it.
– Supravat M
Dec 8 '18 at 9:15
@AdityaShah I update my answer.
– Supravat M
Dec 8 '18 at 9:27
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%2f252888%2fhow-to-get-product-image-data-in-cart-api-magento-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try below code for this. Although answer of @Supravat is correct but I am posting this as per my approach.
extension_attributes.xml
<?xml version="1.0"?>
<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" type="string" />
</extension_attributes>
</config>
di.xml //Define it under webapi_rest folder so that it will come in action for API calls only
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoQuoteApiCartRepositoryInterface">
<plugin name="add_more_info" type="VendorModulePluginQuotePlugin" sortOrder="10" />
</type>
</config>
QuotePlugin.php
<?php
/**
* Copyright © 2018 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace VendorModulePlugin;
use MagentoQuoteApiDataCartInterface;
class QuotePlugin
/**
* @var MagentoQuoteApiDataCartItemExtensionFactory
*/
protected $cartItemExtension;
/**
* @var MagentoCatalogApiProductRepositoryInterface
*/
protected $productRepository;
/**
* @param MagentoQuoteApiDataCartItemExtensionFactory $cartItemExtension
* @param MagentoCatalogApiProductRepositoryInterface $productRepository
*/
public function __construct(
MagentoQuoteApiDataCartItemExtensionFactory $cartItemExtension,
MagentoCatalogApiProductRepositoryInterfaceFactory $productRepository )
$this->cartItemExtension = $cartItemExtension;
$this->productRepository = $productRepository;
/**
* Add attribute values
*
* @param MagentoQuoteApiCartRepositoryInterface $subject,
* @param $quote
* @return $quoteData
*/
public function afterGet(
MagentoQuoteApiCartRepositoryInterface $subject, $quote
)
$quoteData = $this->setAttributeValue($quote);
return $quoteData;
/**
* Add attribute values
*
* @param MagentoQuoteApiCartRepositoryInterface $subject,
* @param $quote
* @return $quoteData
*/
public function afterGetActiveForCustomer(
MagentoQuoteApiCartRepositoryInterface $subject, $quote
)
$quoteData = $this->setAttributeValue($quote);
return $quoteData;
/**
* set value of attributes
*
* @param $product,
* @return $extensionAttributes
*/
private function setAttributeValue($quote)
$data = [];
if ($quote->getItemsCount())
foreach ($quote->getItems() as $item)
$data = [];
$extensionAttributes = $item->getExtensionAttributes();
if ($extensionAttributes === null)
$extensionAttributes = $this->cartItemExtension->create();
$productData = $this->productRepository->create()->get($item->getSku());
$extensionAttributes->setImage($productData->getThumbnail());
$item->setExtensionAttributes($extensionAttributes);
return $quote;
To modify any object in observer not a good approach so I did it in this way. Hope it'll work for you. Let me know if you need further help.
Thanks for the answer + @Ram - I'll try and update you back :)
– Aaditya
Dec 8 '18 at 11:02
sure @AdityaShah :)
– Ramkishan Suthar
Dec 8 '18 at 11:33
1
welcome @AdityaShah Happy Coding :)
– Ramkishan Suthar
Dec 10 '18 at 11:50
Can I call the same plugin for CartTotalRepositoryInterface ?
– Aaditya
Dec 10 '18 at 12:06
1
For this you need to create new plugin and new extension attribute of TotalItemInterface because TotalRepositoryInterface doesn't return CartItemInterface.
– Ramkishan Suthar
Dec 10 '18 at 12:11
|
show 9 more comments
You can try below code for this. Although answer of @Supravat is correct but I am posting this as per my approach.
extension_attributes.xml
<?xml version="1.0"?>
<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" type="string" />
</extension_attributes>
</config>
di.xml //Define it under webapi_rest folder so that it will come in action for API calls only
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoQuoteApiCartRepositoryInterface">
<plugin name="add_more_info" type="VendorModulePluginQuotePlugin" sortOrder="10" />
</type>
</config>
QuotePlugin.php
<?php
/**
* Copyright © 2018 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace VendorModulePlugin;
use MagentoQuoteApiDataCartInterface;
class QuotePlugin
/**
* @var MagentoQuoteApiDataCartItemExtensionFactory
*/
protected $cartItemExtension;
/**
* @var MagentoCatalogApiProductRepositoryInterface
*/
protected $productRepository;
/**
* @param MagentoQuoteApiDataCartItemExtensionFactory $cartItemExtension
* @param MagentoCatalogApiProductRepositoryInterface $productRepository
*/
public function __construct(
MagentoQuoteApiDataCartItemExtensionFactory $cartItemExtension,
MagentoCatalogApiProductRepositoryInterfaceFactory $productRepository )
$this->cartItemExtension = $cartItemExtension;
$this->productRepository = $productRepository;
/**
* Add attribute values
*
* @param MagentoQuoteApiCartRepositoryInterface $subject,
* @param $quote
* @return $quoteData
*/
public function afterGet(
MagentoQuoteApiCartRepositoryInterface $subject, $quote
)
$quoteData = $this->setAttributeValue($quote);
return $quoteData;
/**
* Add attribute values
*
* @param MagentoQuoteApiCartRepositoryInterface $subject,
* @param $quote
* @return $quoteData
*/
public function afterGetActiveForCustomer(
MagentoQuoteApiCartRepositoryInterface $subject, $quote
)
$quoteData = $this->setAttributeValue($quote);
return $quoteData;
/**
* set value of attributes
*
* @param $product,
* @return $extensionAttributes
*/
private function setAttributeValue($quote)
$data = [];
if ($quote->getItemsCount())
foreach ($quote->getItems() as $item)
$data = [];
$extensionAttributes = $item->getExtensionAttributes();
if ($extensionAttributes === null)
$extensionAttributes = $this->cartItemExtension->create();
$productData = $this->productRepository->create()->get($item->getSku());
$extensionAttributes->setImage($productData->getThumbnail());
$item->setExtensionAttributes($extensionAttributes);
return $quote;
To modify any object in observer not a good approach so I did it in this way. Hope it'll work for you. Let me know if you need further help.
Thanks for the answer + @Ram - I'll try and update you back :)
– Aaditya
Dec 8 '18 at 11:02
sure @AdityaShah :)
– Ramkishan Suthar
Dec 8 '18 at 11:33
1
welcome @AdityaShah Happy Coding :)
– Ramkishan Suthar
Dec 10 '18 at 11:50
Can I call the same plugin for CartTotalRepositoryInterface ?
– Aaditya
Dec 10 '18 at 12:06
1
For this you need to create new plugin and new extension attribute of TotalItemInterface because TotalRepositoryInterface doesn't return CartItemInterface.
– Ramkishan Suthar
Dec 10 '18 at 12:11
|
show 9 more comments
You can try below code for this. Although answer of @Supravat is correct but I am posting this as per my approach.
extension_attributes.xml
<?xml version="1.0"?>
<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" type="string" />
</extension_attributes>
</config>
di.xml //Define it under webapi_rest folder so that it will come in action for API calls only
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoQuoteApiCartRepositoryInterface">
<plugin name="add_more_info" type="VendorModulePluginQuotePlugin" sortOrder="10" />
</type>
</config>
QuotePlugin.php
<?php
/**
* Copyright © 2018 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace VendorModulePlugin;
use MagentoQuoteApiDataCartInterface;
class QuotePlugin
/**
* @var MagentoQuoteApiDataCartItemExtensionFactory
*/
protected $cartItemExtension;
/**
* @var MagentoCatalogApiProductRepositoryInterface
*/
protected $productRepository;
/**
* @param MagentoQuoteApiDataCartItemExtensionFactory $cartItemExtension
* @param MagentoCatalogApiProductRepositoryInterface $productRepository
*/
public function __construct(
MagentoQuoteApiDataCartItemExtensionFactory $cartItemExtension,
MagentoCatalogApiProductRepositoryInterfaceFactory $productRepository )
$this->cartItemExtension = $cartItemExtension;
$this->productRepository = $productRepository;
/**
* Add attribute values
*
* @param MagentoQuoteApiCartRepositoryInterface $subject,
* @param $quote
* @return $quoteData
*/
public function afterGet(
MagentoQuoteApiCartRepositoryInterface $subject, $quote
)
$quoteData = $this->setAttributeValue($quote);
return $quoteData;
/**
* Add attribute values
*
* @param MagentoQuoteApiCartRepositoryInterface $subject,
* @param $quote
* @return $quoteData
*/
public function afterGetActiveForCustomer(
MagentoQuoteApiCartRepositoryInterface $subject, $quote
)
$quoteData = $this->setAttributeValue($quote);
return $quoteData;
/**
* set value of attributes
*
* @param $product,
* @return $extensionAttributes
*/
private function setAttributeValue($quote)
$data = [];
if ($quote->getItemsCount())
foreach ($quote->getItems() as $item)
$data = [];
$extensionAttributes = $item->getExtensionAttributes();
if ($extensionAttributes === null)
$extensionAttributes = $this->cartItemExtension->create();
$productData = $this->productRepository->create()->get($item->getSku());
$extensionAttributes->setImage($productData->getThumbnail());
$item->setExtensionAttributes($extensionAttributes);
return $quote;
To modify any object in observer not a good approach so I did it in this way. Hope it'll work for you. Let me know if you need further help.
You can try below code for this. Although answer of @Supravat is correct but I am posting this as per my approach.
extension_attributes.xml
<?xml version="1.0"?>
<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" type="string" />
</extension_attributes>
</config>
di.xml //Define it under webapi_rest folder so that it will come in action for API calls only
<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="MagentoQuoteApiCartRepositoryInterface">
<plugin name="add_more_info" type="VendorModulePluginQuotePlugin" sortOrder="10" />
</type>
</config>
QuotePlugin.php
<?php
/**
* Copyright © 2018 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace VendorModulePlugin;
use MagentoQuoteApiDataCartInterface;
class QuotePlugin
/**
* @var MagentoQuoteApiDataCartItemExtensionFactory
*/
protected $cartItemExtension;
/**
* @var MagentoCatalogApiProductRepositoryInterface
*/
protected $productRepository;
/**
* @param MagentoQuoteApiDataCartItemExtensionFactory $cartItemExtension
* @param MagentoCatalogApiProductRepositoryInterface $productRepository
*/
public function __construct(
MagentoQuoteApiDataCartItemExtensionFactory $cartItemExtension,
MagentoCatalogApiProductRepositoryInterfaceFactory $productRepository )
$this->cartItemExtension = $cartItemExtension;
$this->productRepository = $productRepository;
/**
* Add attribute values
*
* @param MagentoQuoteApiCartRepositoryInterface $subject,
* @param $quote
* @return $quoteData
*/
public function afterGet(
MagentoQuoteApiCartRepositoryInterface $subject, $quote
)
$quoteData = $this->setAttributeValue($quote);
return $quoteData;
/**
* Add attribute values
*
* @param MagentoQuoteApiCartRepositoryInterface $subject,
* @param $quote
* @return $quoteData
*/
public function afterGetActiveForCustomer(
MagentoQuoteApiCartRepositoryInterface $subject, $quote
)
$quoteData = $this->setAttributeValue($quote);
return $quoteData;
/**
* set value of attributes
*
* @param $product,
* @return $extensionAttributes
*/
private function setAttributeValue($quote)
$data = [];
if ($quote->getItemsCount())
foreach ($quote->getItems() as $item)
$data = [];
$extensionAttributes = $item->getExtensionAttributes();
if ($extensionAttributes === null)
$extensionAttributes = $this->cartItemExtension->create();
$productData = $this->productRepository->create()->get($item->getSku());
$extensionAttributes->setImage($productData->getThumbnail());
$item->setExtensionAttributes($extensionAttributes);
return $quote;
To modify any object in observer not a good approach so I did it in this way. Hope it'll work for you. Let me know if you need further help.
edited May 27 at 16:34
Nalin Savaliya
921415
921415
answered Dec 8 '18 at 10:56
Ramkishan SutharRamkishan Suthar
2,34421435
2,34421435
Thanks for the answer + @Ram - I'll try and update you back :)
– Aaditya
Dec 8 '18 at 11:02
sure @AdityaShah :)
– Ramkishan Suthar
Dec 8 '18 at 11:33
1
welcome @AdityaShah Happy Coding :)
– Ramkishan Suthar
Dec 10 '18 at 11:50
Can I call the same plugin for CartTotalRepositoryInterface ?
– Aaditya
Dec 10 '18 at 12:06
1
For this you need to create new plugin and new extension attribute of TotalItemInterface because TotalRepositoryInterface doesn't return CartItemInterface.
– Ramkishan Suthar
Dec 10 '18 at 12:11
|
show 9 more comments
Thanks for the answer + @Ram - I'll try and update you back :)
– Aaditya
Dec 8 '18 at 11:02
sure @AdityaShah :)
– Ramkishan Suthar
Dec 8 '18 at 11:33
1
welcome @AdityaShah Happy Coding :)
– Ramkishan Suthar
Dec 10 '18 at 11:50
Can I call the same plugin for CartTotalRepositoryInterface ?
– Aaditya
Dec 10 '18 at 12:06
1
For this you need to create new plugin and new extension attribute of TotalItemInterface because TotalRepositoryInterface doesn't return CartItemInterface.
– Ramkishan Suthar
Dec 10 '18 at 12:11
Thanks for the answer + @Ram - I'll try and update you back :)
– Aaditya
Dec 8 '18 at 11:02
Thanks for the answer + @Ram - I'll try and update you back :)
– Aaditya
Dec 8 '18 at 11:02
sure @AdityaShah :)
– Ramkishan Suthar
Dec 8 '18 at 11:33
sure @AdityaShah :)
– Ramkishan Suthar
Dec 8 '18 at 11:33
1
1
welcome @AdityaShah Happy Coding :)
– Ramkishan Suthar
Dec 10 '18 at 11:50
welcome @AdityaShah Happy Coding :)
– Ramkishan Suthar
Dec 10 '18 at 11:50
Can I call the same plugin for CartTotalRepositoryInterface ?
– Aaditya
Dec 10 '18 at 12:06
Can I call the same plugin for CartTotalRepositoryInterface ?
– Aaditya
Dec 10 '18 at 12:06
1
1
For this you need to create new plugin and new extension attribute of TotalItemInterface because TotalRepositoryInterface doesn't return CartItemInterface.
– Ramkishan Suthar
Dec 10 '18 at 12:11
For this you need to create new plugin and new extension attribute of TotalItemInterface because TotalRepositoryInterface doesn't return CartItemInterface.
– Ramkishan Suthar
Dec 10 '18 at 12:11
|
show 9 more comments
GET /V1/carts/mine
Implementation Notes
Returns information for the cart for a specified customer.
Response:
"id": 0,
"created_at": "string",
"updated_at": "string",
"converted_at": "string",
"is_active": true,
"is_virtual": true,
"items": [
"item_id": 0,
"sku": "string",
"qty": 0,
"name": "string",
"price": 0,
"product_type": "string",
"quote_id": "string",
"product_option":
"extension_attributes":
"custom_options": [
"option_id": "string",
"option_value": "string",
"extension_attributes":
"file_info":
"base64_encoded_data": "string",
"type": "string",
"name": "string"
],
"bundle_options": [
"option_id": 0,
"option_qty": 0,
"option_selections": [
0
],
"extension_attributes":
],
"configurable_item_options": [
"option_id": "string",
"option_value": 0,
"extension_attributes":
],
"downloadable_option":
"downloadable_links": [
0
]
,
"giftcard_item_option":
"giftcard_amount": "string",
"custom_giftcard_amount": 0,
"giftcard_sender_name": "string",
"giftcard_recipient_name": "string",
"giftcard_sender_email": "string",
"giftcard_recipient_email": "string",
"giftcard_message": "string",
"extension_attributes":
,
"extension_attributes":
"negotiable_quote_item":
"item_id": 0,
"original_price": 0,
"original_tax_amount": 0,
"original_discount_amount": 0,
"extension_attributes":
],
"items_count": 0,
"items_qty": 0,
"customer":
"id": 0,
"group_id": 0,
"default_billing": "string",
"default_shipping": "string",
"confirmation": "string",
"created_at": "string",
"updated_at": "string",
"created_in": "string",
"dob": "string",
"email": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"gender": 0,
"store_id": 0,
"taxvat": "string",
"website_id": 0,
"addresses": [
"id": 0,
"customer_id": 0,
"region":
"region_code": "string",
"region": "string",
"region_id": 0,
"extension_attributes":
,
"region_id": 0,
"country_id": "string",
"street": [
"string"
],
"company": "string",
"telephone": "string",
"fax": "string",
"postcode": "string",
"city": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"vat_id": "string",
"default_shipping": true,
"default_billing": true,
"extension_attributes": ,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
],
"disable_auto_group_change": 0,
"extension_attributes":
"company_attributes":
"customer_id": 0,
"company_id": 0,
"job_title": "string",
"status": 0,
"telephone": "string",
"extension_attributes":
,
"is_subscribed": true
,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
,
"billing_address":
"id": 0,
"region": "string",
"region_id": 0,
"region_code": "string",
"country_id": "string",
"street": [
"string"
],
"company": "string",
"telephone": "string",
"fax": "string",
"postcode": "string",
"city": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"vat_id": "string",
"customer_id": 0,
"email": "string",
"same_as_billing": 0,
"customer_address_id": 0,
"save_in_address_book": 0,
"extension_attributes":
"gift_registry_id": 0
,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
,
"reserved_order_id": 0,
"orig_order_id": 0,
"currency":
"global_currency_code": "string",
"base_currency_code": "string",
"store_currency_code": "string",
"quote_currency_code": "string",
"store_to_base_rate": 0,
"store_to_quote_rate": 0,
"base_to_global_rate": 0,
"base_to_quote_rate": 0,
"extension_attributes":
,
"customer_is_guest": true,
"customer_note": "string",
"customer_note_notify": true,
"customer_tax_class_id": 0,
"store_id": 0,
"extension_attributes":
"shipping_assignments": [
"shipping":
"address":
"id": 0,
"region": "string",
"region_id": 0,
"region_code": "string",
"country_id": "string",
"street": [
"string"
],
"company": "string",
"telephone": "string",
"fax": "string",
"postcode": "string",
"city": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"vat_id": "string",
"customer_id": 0,
"email": "string",
"same_as_billing": 0,
"customer_address_id": 0,
"save_in_address_book": 0,
"extension_attributes":
"gift_registry_id": 0
,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
,
"method": "string",
"extension_attributes":
,
"items": [
"item_id": 0,
"sku": "string",
"qty": 0,
"name": "string",
"price": 0,
"product_type": "string",
"quote_id": "string",
"product_option":
"extension_attributes":
"custom_options": [
"option_id": "string",
"option_value": "string",
"extension_attributes":
"file_info":
"base64_encoded_data": "string",
"type": "string",
"name": "string"
],
"bundle_options": [
"option_id": 0,
"option_qty": 0,
"option_selections": [
0
],
"extension_attributes":
],
"configurable_item_options": [
"option_id": "string",
"option_value": 0,
"extension_attributes":
],
"downloadable_option":
"downloadable_links": [
0
]
,
"giftcard_item_option":
"giftcard_amount": "string",
"custom_giftcard_amount": 0,
"giftcard_sender_name": "string",
"giftcard_recipient_name": "string",
"giftcard_sender_email": "string",
"giftcard_recipient_email": "string",
"giftcard_message": "string",
"extension_attributes":
,
"extension_attributes":
"negotiable_quote_item":
"item_id": 0,
"original_price": 0,
"original_tax_amount": 0,
"original_discount_amount": 0,
"extension_attributes":
],
"extension_attributes":
],
"negotiable_quote":
"quote_id": 0,
"is_regular_quote": true,
"status": "string",
"negotiated_price_type": 0,
"negotiated_price_value": 0,
"shipping_price": 0,
"quote_name": "string",
"expiration_period": "string",
"email_notification_status": 0,
"has_unconfirmed_changes": true,
"is_shipping_tax_changed": true,
"is_customer_price_changed": true,
"notifications": 0,
"applied_rule_ids": "string",
"is_address_draft": true,
"deleted_sku": "string",
"creator_id": 0,
"creator_type": 0,
"original_total_price": 0,
"base_original_total_price": 0,
"negotiated_total_price": 0,
"base_negotiated_total_price": 0,
"extension_attributes":
If you see the above response , there is no product image information. So we should call an API for it. You should call GET /V1/products/:sku/media
to retrieve for a product's image.
For for information please see here :
Magento 2 REST APIs
[Edit]
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/Mocule_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;
I want those image data in /V1/carts/mine API itself. I am aware about getbyID and sku APIs :)
– Aaditya
Dec 8 '18 at 8:46
@AdityaShah, Then you should develop a custom module for it.
– Supravat M
Dec 8 '18 at 9:15
@AdityaShah I update my answer.
– Supravat M
Dec 8 '18 at 9:27
add a comment |
GET /V1/carts/mine
Implementation Notes
Returns information for the cart for a specified customer.
Response:
"id": 0,
"created_at": "string",
"updated_at": "string",
"converted_at": "string",
"is_active": true,
"is_virtual": true,
"items": [
"item_id": 0,
"sku": "string",
"qty": 0,
"name": "string",
"price": 0,
"product_type": "string",
"quote_id": "string",
"product_option":
"extension_attributes":
"custom_options": [
"option_id": "string",
"option_value": "string",
"extension_attributes":
"file_info":
"base64_encoded_data": "string",
"type": "string",
"name": "string"
],
"bundle_options": [
"option_id": 0,
"option_qty": 0,
"option_selections": [
0
],
"extension_attributes":
],
"configurable_item_options": [
"option_id": "string",
"option_value": 0,
"extension_attributes":
],
"downloadable_option":
"downloadable_links": [
0
]
,
"giftcard_item_option":
"giftcard_amount": "string",
"custom_giftcard_amount": 0,
"giftcard_sender_name": "string",
"giftcard_recipient_name": "string",
"giftcard_sender_email": "string",
"giftcard_recipient_email": "string",
"giftcard_message": "string",
"extension_attributes":
,
"extension_attributes":
"negotiable_quote_item":
"item_id": 0,
"original_price": 0,
"original_tax_amount": 0,
"original_discount_amount": 0,
"extension_attributes":
],
"items_count": 0,
"items_qty": 0,
"customer":
"id": 0,
"group_id": 0,
"default_billing": "string",
"default_shipping": "string",
"confirmation": "string",
"created_at": "string",
"updated_at": "string",
"created_in": "string",
"dob": "string",
"email": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"gender": 0,
"store_id": 0,
"taxvat": "string",
"website_id": 0,
"addresses": [
"id": 0,
"customer_id": 0,
"region":
"region_code": "string",
"region": "string",
"region_id": 0,
"extension_attributes":
,
"region_id": 0,
"country_id": "string",
"street": [
"string"
],
"company": "string",
"telephone": "string",
"fax": "string",
"postcode": "string",
"city": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"vat_id": "string",
"default_shipping": true,
"default_billing": true,
"extension_attributes": ,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
],
"disable_auto_group_change": 0,
"extension_attributes":
"company_attributes":
"customer_id": 0,
"company_id": 0,
"job_title": "string",
"status": 0,
"telephone": "string",
"extension_attributes":
,
"is_subscribed": true
,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
,
"billing_address":
"id": 0,
"region": "string",
"region_id": 0,
"region_code": "string",
"country_id": "string",
"street": [
"string"
],
"company": "string",
"telephone": "string",
"fax": "string",
"postcode": "string",
"city": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"vat_id": "string",
"customer_id": 0,
"email": "string",
"same_as_billing": 0,
"customer_address_id": 0,
"save_in_address_book": 0,
"extension_attributes":
"gift_registry_id": 0
,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
,
"reserved_order_id": 0,
"orig_order_id": 0,
"currency":
"global_currency_code": "string",
"base_currency_code": "string",
"store_currency_code": "string",
"quote_currency_code": "string",
"store_to_base_rate": 0,
"store_to_quote_rate": 0,
"base_to_global_rate": 0,
"base_to_quote_rate": 0,
"extension_attributes":
,
"customer_is_guest": true,
"customer_note": "string",
"customer_note_notify": true,
"customer_tax_class_id": 0,
"store_id": 0,
"extension_attributes":
"shipping_assignments": [
"shipping":
"address":
"id": 0,
"region": "string",
"region_id": 0,
"region_code": "string",
"country_id": "string",
"street": [
"string"
],
"company": "string",
"telephone": "string",
"fax": "string",
"postcode": "string",
"city": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"vat_id": "string",
"customer_id": 0,
"email": "string",
"same_as_billing": 0,
"customer_address_id": 0,
"save_in_address_book": 0,
"extension_attributes":
"gift_registry_id": 0
,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
,
"method": "string",
"extension_attributes":
,
"items": [
"item_id": 0,
"sku": "string",
"qty": 0,
"name": "string",
"price": 0,
"product_type": "string",
"quote_id": "string",
"product_option":
"extension_attributes":
"custom_options": [
"option_id": "string",
"option_value": "string",
"extension_attributes":
"file_info":
"base64_encoded_data": "string",
"type": "string",
"name": "string"
],
"bundle_options": [
"option_id": 0,
"option_qty": 0,
"option_selections": [
0
],
"extension_attributes":
],
"configurable_item_options": [
"option_id": "string",
"option_value": 0,
"extension_attributes":
],
"downloadable_option":
"downloadable_links": [
0
]
,
"giftcard_item_option":
"giftcard_amount": "string",
"custom_giftcard_amount": 0,
"giftcard_sender_name": "string",
"giftcard_recipient_name": "string",
"giftcard_sender_email": "string",
"giftcard_recipient_email": "string",
"giftcard_message": "string",
"extension_attributes":
,
"extension_attributes":
"negotiable_quote_item":
"item_id": 0,
"original_price": 0,
"original_tax_amount": 0,
"original_discount_amount": 0,
"extension_attributes":
],
"extension_attributes":
],
"negotiable_quote":
"quote_id": 0,
"is_regular_quote": true,
"status": "string",
"negotiated_price_type": 0,
"negotiated_price_value": 0,
"shipping_price": 0,
"quote_name": "string",
"expiration_period": "string",
"email_notification_status": 0,
"has_unconfirmed_changes": true,
"is_shipping_tax_changed": true,
"is_customer_price_changed": true,
"notifications": 0,
"applied_rule_ids": "string",
"is_address_draft": true,
"deleted_sku": "string",
"creator_id": 0,
"creator_type": 0,
"original_total_price": 0,
"base_original_total_price": 0,
"negotiated_total_price": 0,
"base_negotiated_total_price": 0,
"extension_attributes":
If you see the above response , there is no product image information. So we should call an API for it. You should call GET /V1/products/:sku/media
to retrieve for a product's image.
For for information please see here :
Magento 2 REST APIs
[Edit]
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/Mocule_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;
I want those image data in /V1/carts/mine API itself. I am aware about getbyID and sku APIs :)
– Aaditya
Dec 8 '18 at 8:46
@AdityaShah, Then you should develop a custom module for it.
– Supravat M
Dec 8 '18 at 9:15
@AdityaShah I update my answer.
– Supravat M
Dec 8 '18 at 9:27
add a comment |
GET /V1/carts/mine
Implementation Notes
Returns information for the cart for a specified customer.
Response:
"id": 0,
"created_at": "string",
"updated_at": "string",
"converted_at": "string",
"is_active": true,
"is_virtual": true,
"items": [
"item_id": 0,
"sku": "string",
"qty": 0,
"name": "string",
"price": 0,
"product_type": "string",
"quote_id": "string",
"product_option":
"extension_attributes":
"custom_options": [
"option_id": "string",
"option_value": "string",
"extension_attributes":
"file_info":
"base64_encoded_data": "string",
"type": "string",
"name": "string"
],
"bundle_options": [
"option_id": 0,
"option_qty": 0,
"option_selections": [
0
],
"extension_attributes":
],
"configurable_item_options": [
"option_id": "string",
"option_value": 0,
"extension_attributes":
],
"downloadable_option":
"downloadable_links": [
0
]
,
"giftcard_item_option":
"giftcard_amount": "string",
"custom_giftcard_amount": 0,
"giftcard_sender_name": "string",
"giftcard_recipient_name": "string",
"giftcard_sender_email": "string",
"giftcard_recipient_email": "string",
"giftcard_message": "string",
"extension_attributes":
,
"extension_attributes":
"negotiable_quote_item":
"item_id": 0,
"original_price": 0,
"original_tax_amount": 0,
"original_discount_amount": 0,
"extension_attributes":
],
"items_count": 0,
"items_qty": 0,
"customer":
"id": 0,
"group_id": 0,
"default_billing": "string",
"default_shipping": "string",
"confirmation": "string",
"created_at": "string",
"updated_at": "string",
"created_in": "string",
"dob": "string",
"email": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"gender": 0,
"store_id": 0,
"taxvat": "string",
"website_id": 0,
"addresses": [
"id": 0,
"customer_id": 0,
"region":
"region_code": "string",
"region": "string",
"region_id": 0,
"extension_attributes":
,
"region_id": 0,
"country_id": "string",
"street": [
"string"
],
"company": "string",
"telephone": "string",
"fax": "string",
"postcode": "string",
"city": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"vat_id": "string",
"default_shipping": true,
"default_billing": true,
"extension_attributes": ,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
],
"disable_auto_group_change": 0,
"extension_attributes":
"company_attributes":
"customer_id": 0,
"company_id": 0,
"job_title": "string",
"status": 0,
"telephone": "string",
"extension_attributes":
,
"is_subscribed": true
,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
,
"billing_address":
"id": 0,
"region": "string",
"region_id": 0,
"region_code": "string",
"country_id": "string",
"street": [
"string"
],
"company": "string",
"telephone": "string",
"fax": "string",
"postcode": "string",
"city": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"vat_id": "string",
"customer_id": 0,
"email": "string",
"same_as_billing": 0,
"customer_address_id": 0,
"save_in_address_book": 0,
"extension_attributes":
"gift_registry_id": 0
,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
,
"reserved_order_id": 0,
"orig_order_id": 0,
"currency":
"global_currency_code": "string",
"base_currency_code": "string",
"store_currency_code": "string",
"quote_currency_code": "string",
"store_to_base_rate": 0,
"store_to_quote_rate": 0,
"base_to_global_rate": 0,
"base_to_quote_rate": 0,
"extension_attributes":
,
"customer_is_guest": true,
"customer_note": "string",
"customer_note_notify": true,
"customer_tax_class_id": 0,
"store_id": 0,
"extension_attributes":
"shipping_assignments": [
"shipping":
"address":
"id": 0,
"region": "string",
"region_id": 0,
"region_code": "string",
"country_id": "string",
"street": [
"string"
],
"company": "string",
"telephone": "string",
"fax": "string",
"postcode": "string",
"city": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"vat_id": "string",
"customer_id": 0,
"email": "string",
"same_as_billing": 0,
"customer_address_id": 0,
"save_in_address_book": 0,
"extension_attributes":
"gift_registry_id": 0
,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
,
"method": "string",
"extension_attributes":
,
"items": [
"item_id": 0,
"sku": "string",
"qty": 0,
"name": "string",
"price": 0,
"product_type": "string",
"quote_id": "string",
"product_option":
"extension_attributes":
"custom_options": [
"option_id": "string",
"option_value": "string",
"extension_attributes":
"file_info":
"base64_encoded_data": "string",
"type": "string",
"name": "string"
],
"bundle_options": [
"option_id": 0,
"option_qty": 0,
"option_selections": [
0
],
"extension_attributes":
],
"configurable_item_options": [
"option_id": "string",
"option_value": 0,
"extension_attributes":
],
"downloadable_option":
"downloadable_links": [
0
]
,
"giftcard_item_option":
"giftcard_amount": "string",
"custom_giftcard_amount": 0,
"giftcard_sender_name": "string",
"giftcard_recipient_name": "string",
"giftcard_sender_email": "string",
"giftcard_recipient_email": "string",
"giftcard_message": "string",
"extension_attributes":
,
"extension_attributes":
"negotiable_quote_item":
"item_id": 0,
"original_price": 0,
"original_tax_amount": 0,
"original_discount_amount": 0,
"extension_attributes":
],
"extension_attributes":
],
"negotiable_quote":
"quote_id": 0,
"is_regular_quote": true,
"status": "string",
"negotiated_price_type": 0,
"negotiated_price_value": 0,
"shipping_price": 0,
"quote_name": "string",
"expiration_period": "string",
"email_notification_status": 0,
"has_unconfirmed_changes": true,
"is_shipping_tax_changed": true,
"is_customer_price_changed": true,
"notifications": 0,
"applied_rule_ids": "string",
"is_address_draft": true,
"deleted_sku": "string",
"creator_id": 0,
"creator_type": 0,
"original_total_price": 0,
"base_original_total_price": 0,
"negotiated_total_price": 0,
"base_negotiated_total_price": 0,
"extension_attributes":
If you see the above response , there is no product image information. So we should call an API for it. You should call GET /V1/products/:sku/media
to retrieve for a product's image.
For for information please see here :
Magento 2 REST APIs
[Edit]
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/Mocule_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;
GET /V1/carts/mine
Implementation Notes
Returns information for the cart for a specified customer.
Response:
"id": 0,
"created_at": "string",
"updated_at": "string",
"converted_at": "string",
"is_active": true,
"is_virtual": true,
"items": [
"item_id": 0,
"sku": "string",
"qty": 0,
"name": "string",
"price": 0,
"product_type": "string",
"quote_id": "string",
"product_option":
"extension_attributes":
"custom_options": [
"option_id": "string",
"option_value": "string",
"extension_attributes":
"file_info":
"base64_encoded_data": "string",
"type": "string",
"name": "string"
],
"bundle_options": [
"option_id": 0,
"option_qty": 0,
"option_selections": [
0
],
"extension_attributes":
],
"configurable_item_options": [
"option_id": "string",
"option_value": 0,
"extension_attributes":
],
"downloadable_option":
"downloadable_links": [
0
]
,
"giftcard_item_option":
"giftcard_amount": "string",
"custom_giftcard_amount": 0,
"giftcard_sender_name": "string",
"giftcard_recipient_name": "string",
"giftcard_sender_email": "string",
"giftcard_recipient_email": "string",
"giftcard_message": "string",
"extension_attributes":
,
"extension_attributes":
"negotiable_quote_item":
"item_id": 0,
"original_price": 0,
"original_tax_amount": 0,
"original_discount_amount": 0,
"extension_attributes":
],
"items_count": 0,
"items_qty": 0,
"customer":
"id": 0,
"group_id": 0,
"default_billing": "string",
"default_shipping": "string",
"confirmation": "string",
"created_at": "string",
"updated_at": "string",
"created_in": "string",
"dob": "string",
"email": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"gender": 0,
"store_id": 0,
"taxvat": "string",
"website_id": 0,
"addresses": [
"id": 0,
"customer_id": 0,
"region":
"region_code": "string",
"region": "string",
"region_id": 0,
"extension_attributes":
,
"region_id": 0,
"country_id": "string",
"street": [
"string"
],
"company": "string",
"telephone": "string",
"fax": "string",
"postcode": "string",
"city": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"vat_id": "string",
"default_shipping": true,
"default_billing": true,
"extension_attributes": ,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
],
"disable_auto_group_change": 0,
"extension_attributes":
"company_attributes":
"customer_id": 0,
"company_id": 0,
"job_title": "string",
"status": 0,
"telephone": "string",
"extension_attributes":
,
"is_subscribed": true
,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
,
"billing_address":
"id": 0,
"region": "string",
"region_id": 0,
"region_code": "string",
"country_id": "string",
"street": [
"string"
],
"company": "string",
"telephone": "string",
"fax": "string",
"postcode": "string",
"city": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"vat_id": "string",
"customer_id": 0,
"email": "string",
"same_as_billing": 0,
"customer_address_id": 0,
"save_in_address_book": 0,
"extension_attributes":
"gift_registry_id": 0
,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
,
"reserved_order_id": 0,
"orig_order_id": 0,
"currency":
"global_currency_code": "string",
"base_currency_code": "string",
"store_currency_code": "string",
"quote_currency_code": "string",
"store_to_base_rate": 0,
"store_to_quote_rate": 0,
"base_to_global_rate": 0,
"base_to_quote_rate": 0,
"extension_attributes":
,
"customer_is_guest": true,
"customer_note": "string",
"customer_note_notify": true,
"customer_tax_class_id": 0,
"store_id": 0,
"extension_attributes":
"shipping_assignments": [
"shipping":
"address":
"id": 0,
"region": "string",
"region_id": 0,
"region_code": "string",
"country_id": "string",
"street": [
"string"
],
"company": "string",
"telephone": "string",
"fax": "string",
"postcode": "string",
"city": "string",
"firstname": "string",
"lastname": "string",
"middlename": "string",
"prefix": "string",
"suffix": "string",
"vat_id": "string",
"customer_id": 0,
"email": "string",
"same_as_billing": 0,
"customer_address_id": 0,
"save_in_address_book": 0,
"extension_attributes":
"gift_registry_id": 0
,
"custom_attributes": [
"attribute_code": "string",
"value": "string"
]
,
"method": "string",
"extension_attributes":
,
"items": [
"item_id": 0,
"sku": "string",
"qty": 0,
"name": "string",
"price": 0,
"product_type": "string",
"quote_id": "string",
"product_option":
"extension_attributes":
"custom_options": [
"option_id": "string",
"option_value": "string",
"extension_attributes":
"file_info":
"base64_encoded_data": "string",
"type": "string",
"name": "string"
],
"bundle_options": [
"option_id": 0,
"option_qty": 0,
"option_selections": [
0
],
"extension_attributes":
],
"configurable_item_options": [
"option_id": "string",
"option_value": 0,
"extension_attributes":
],
"downloadable_option":
"downloadable_links": [
0
]
,
"giftcard_item_option":
"giftcard_amount": "string",
"custom_giftcard_amount": 0,
"giftcard_sender_name": "string",
"giftcard_recipient_name": "string",
"giftcard_sender_email": "string",
"giftcard_recipient_email": "string",
"giftcard_message": "string",
"extension_attributes":
,
"extension_attributes":
"negotiable_quote_item":
"item_id": 0,
"original_price": 0,
"original_tax_amount": 0,
"original_discount_amount": 0,
"extension_attributes":
],
"extension_attributes":
],
"negotiable_quote":
"quote_id": 0,
"is_regular_quote": true,
"status": "string",
"negotiated_price_type": 0,
"negotiated_price_value": 0,
"shipping_price": 0,
"quote_name": "string",
"expiration_period": "string",
"email_notification_status": 0,
"has_unconfirmed_changes": true,
"is_shipping_tax_changed": true,
"is_customer_price_changed": true,
"notifications": 0,
"applied_rule_ids": "string",
"is_address_draft": true,
"deleted_sku": "string",
"creator_id": 0,
"creator_type": 0,
"original_total_price": 0,
"base_original_total_price": 0,
"negotiated_total_price": 0,
"base_negotiated_total_price": 0,
"extension_attributes":
If you see the above response , there is no product image information. So we should call an API for it. You should call GET /V1/products/:sku/media
to retrieve for a product's image.
For for information please see here :
Magento 2 REST APIs
[Edit]
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/Mocule_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;
edited Dec 8 '18 at 9:21
answered Dec 8 '18 at 7:41
Supravat MSupravat M
1,16611627
1,16611627
I want those image data in /V1/carts/mine API itself. I am aware about getbyID and sku APIs :)
– Aaditya
Dec 8 '18 at 8:46
@AdityaShah, Then you should develop a custom module for it.
– Supravat M
Dec 8 '18 at 9:15
@AdityaShah I update my answer.
– Supravat M
Dec 8 '18 at 9:27
add a comment |
I want those image data in /V1/carts/mine API itself. I am aware about getbyID and sku APIs :)
– Aaditya
Dec 8 '18 at 8:46
@AdityaShah, Then you should develop a custom module for it.
– Supravat M
Dec 8 '18 at 9:15
@AdityaShah I update my answer.
– Supravat M
Dec 8 '18 at 9:27
I want those image data in /V1/carts/mine API itself. I am aware about getbyID and sku APIs :)
– Aaditya
Dec 8 '18 at 8:46
I want those image data in /V1/carts/mine API itself. I am aware about getbyID and sku APIs :)
– Aaditya
Dec 8 '18 at 8:46
@AdityaShah, Then you should develop a custom module for it.
– Supravat M
Dec 8 '18 at 9:15
@AdityaShah, Then you should develop a custom module for it.
– Supravat M
Dec 8 '18 at 9:15
@AdityaShah I update my answer.
– Supravat M
Dec 8 '18 at 9:27
@AdityaShah I update my answer.
– Supravat M
Dec 8 '18 at 9:27
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%2f252888%2fhow-to-get-product-image-data-in-cart-api-magento-2%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