cancel
Showing results for 
Search instead for 
Did you mean: 

cart item with images base on sku

cart item with images base on sku

I have hit "/V1/carts/mine" this api to get cart details ...whole json  get but i need items with all images base on sku in same api

3 REPLIES 3

Re: cart item with images base on sku


@rakesh_sain 

 

As default magento doesnt provide cart product with images in API so you can get in extension attribute or you can call 2 apis as well:

I have suggested below 2 links to follow, please check if that helps you:

https://magento.stackexchange.com/questions/252888/how-to-get-product-image-data-in-cart-api-magento...

https://magento.stackexchange.com/questions/144197/magento-2-rest-api-get-cart-items-with-images?rq=...

Manish Mittal
https://www.manishmittal.com/

Re: cart item with images base on sku

can i create module with same flow as current api only added images in that module?

Re: cart item with images base on sku

Hi @rakesh_sain 

Create extention attribute within custom module and follow below steps:
1. Create etc/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="Magento\Quote\Api\Data\CartItemInterface">
      <attribute code="image" type="string" />
    </extension_attributes>
</config>

2. Add below code With di.xml

<?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_image" type="VendorName\ModuleName\Plugin\CustomPlugin" sortOrder="10" />
    </type>
  </config>

3. create file under Plugin folder CustomPlugin.php

<?php

/**
 * Copyright © 2018 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace VendorName\ModuleName\Plugin;

use Magento\Quote\Api\Data\CartInterface;

class CustomPlugin {

    /**
     * @var \Magento\Quote\Api\Data\CartItemExtensionFactory
     */
    protected $cartItemExtension;

    /**
     * @var \Magento\Catalog\Api\ProductRepositoryInterface
     */
    protected $productRepository;

    /**
     * @param \Magento\Quote\Api\Data\CartItemExtensionFactory $cartItemExtension
     * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
     */
    public function __construct(
        \Magento\Quote\Api\Data\CartItemExtensionFactory $cartItemExtension, 
        \Magento\Catalog\Api\ProductRepositoryInterfaceFactory $productRepository        ) {
        $this->cartItemExtension = $cartItemExtension;
        $this->productRepository = $productRepository;
    }

    /**
     * Add attribute values
     *
     * @param   \Magento\Quote\Api\CartRepositoryInterface $subject,
     * @param   $quote
     * @return  $quoteData
     */
    public function afterGet(
    \Magento\Quote\Api\CartRepositoryInterface $subject, $quote
    ) {
        $quoteData = $this->setAttributeValue($quote);
        return $quoteData;
    }

    /**
     * Add attribute values
     *
     * @param   \Magento\Quote\Api\CartRepositoryInterface $subject,
     * @param   $quote
     * @return  $quoteData
     */
    public function afterGetActiveForCustomer(
    \Magento\Quote\Api\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();
                }
                $productData = $this->productRepository->create()->get($item->getSku());                
                $extensionAttributes->setImage($productData->getThumbnail());

                $item->setExtensionAttributes($extensionAttributes);
            }
        }

        return $quote;
    }

}

It will help you to add custom field with cart api.

If issue resolve, Please click on 'Kudos' & Accept as Solution!

Problem solved? Click Accept as Solution!