I want to add an image to the Items in Abandoned cart API response, I can able to add images to the rest/default/V1/carts/mine API, But unable to get in abandoned cart Items, How should I achieve this?
1 Answer
If you want to Add image to products in the Abandoned cart API response use the following approach
Abandoned cart API
http://<rest>/rest/V1/carts/search?searchCriteria[filter_groups][0][filters][0][field]=is_active&searchCriteria[filter_groups][0][filters][0][value]=1&searchCriteria[filter_groups][0][filters][0][condition_type]=eq&searchCriteria[filter_groups][3][filters][0][field]=customer_id&searchCriteria[filter_groups][3][filters][0][value]=null&searchCriteria[filter_groups][3][filters][0][condition_type]=neq
Create extension_attributes.xml inside the etc directory and add the following code
<?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="Magento\Quote\Api\Data\CartItemInterface">
<attribute code="image" type="string" />
</extension_attributes>
</config>
Create di.XML inside etc/webapi_rest directory and add the following code
<?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="Magento\Quote\Api\CartRepositoryInterface">
<plugin name="add_more_info" type="Adobe\CustomizeCart\Plugin\QuotePlugin" sortOrder="10" />
</type>
</config>
Create a plugin inside the Plugin directory
<?php
/**
* Copyright © 2018 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Adobe\CustomizeCart\Plugin;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartSearchResultsInterface;
use Magento\Quote\Api\Data\CartItemExtensionFactory;
use Magento\Catalog\Api\ProductRepositoryInterfaceFactory as ProductRepository;
use Magento\Catalog\Helper\ImageFactory as ProductImageHelper;
class QuotePlugin
{
/**
* @var CartItemExtensionFactory
*/
protected $cartItemExtension;
/**
* @var ProductRepositoryInterface
*/
protected $productRepository;
/**
* @param CartItemExtensionFactory $cartItemExtension
* @param ProductRepositoryInterface $productRepository
*/
public function __construct(CartItemExtensionFactory $cartItemExtension, ProductRepository $productRepository, ProductImageHelper $productImageHelper)
{
$this->productImageHelper = $productImageHelper;
$this->cartItemExtension = $cartItemExtension;
$this->productRepository = $productRepository;
}
/**
* Add attribute values
*
* @param CartRepositoryInterface $subject ,
* @param $quote
* @return $quoteData
*/
public function afterGet(CartRepositoryInterface $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();
}
$product = $this->productRepository->create()->get($item->getSku());
$imageurl = $this->productImageHelper->create()->init($product, 'product_thumbnail_image')->setImageFile($product->getThumbnail())->getUrl();
$extensionAttributes->setImage($imageurl);
$item->setExtensionAttributes($extensionAttributes);
}
}
return $quote;
}
public function afterGetList(CartRepositoryInterface $subject, CartSearchResultsInterface $searchResults)
{
foreach ($searchResults->getItems() as $entity) {
foreach ($entity->getItems() as $singleItem) {
$extensionAttributes = $singleItem->getExtensionAttributes();
if ($extensionAttributes === null) {
$extensionAttributes = $this->cartItemExtension->create();
}
$product = $this->productRepository->create()->get($singleItem->getSku());
$imageurl = $this->productImageHelper->create()->init($product, 'product_thumbnail_image')->setImageFile($product->getThumbnail())->getUrl();
$extensionAttributes->setImage($imageurl);
$singleItem->setExtensionAttributes($extensionAttributes);
}
}
return $searchResults;
}
/**
* Add attribute values
*
* @param CartRepositoryInterface $subject ,
* @param $quote
* @return $quoteData
*/
public function afterGetActiveForCustomer(CartRepositoryInterface $subject, $quote)
{
$quoteData = $this->setAttributeValue($quote);
return $quoteData;
}
}
After this, you will get a response like this
{
"items": [
{
"id": 10,
"created_at": "2022-08-09 07:51:39",
"updated_at": "2022-08-19 13:40:35",
"is_active": true,
"is_virtual": false,
"items": [
{
"item_id": 36,
"sku": "24-MB01",
"qty": 1,
"name": "Joust Duffle Bag",
"price": 34,
"product_type": "simple",
"quote_id": "10",
"extension_attributes": {
"image": "http://commerce.test/media/catalog/product\\cache\\1b586fea9684f361adf9ddaf06adedaf\\/m/b/mb01-blue-0.jpg"
}
},
{
"item_id": 37,
"sku": "24-MB04",
"qty": 1,
"name": "Strive Shoulder Pack",
"price": 32,
"product_type": "simple",
"quote_id": "10",
"extension_attributes": {
"image": "http://commerce.test/media/catalog/product\\cache\\1b586fea9684f361adf9ddaf06adedaf\\/m/b/mb04-black-0.jpg"
}
}
],
"items_count": 2,
"items_qty": 2,
"customer": {
"id": 2,
"group_id": 1,
"dob": "1973-12-15",
"email": "[email protected]",
"firstname": "Veronica",
"lastname": "Costello",
"gender": 2,
"store_id": 1,
"website_id": 1,
}
]
"total_count": 1
}
I hope this will help.
-
thanks, but kindly can you take a look here magento.stackexchange.com/questions/359058/… Commented Aug 21, 2022 at 10:07