Question

Magento 2 Rest API of cart totals doesn't provide SKU, Which is necessary for any operation like extension_attributes

/rest/V1/carts/mine/totals

The return array of Items is

array (size=21)
          'item_id' => string '139' (length=3)
          'name' => string '40Cm Width Protective Packaging Pe Air Bubble Roll Film Wrap' (length=60)
          'qty' => int 2
          'price' => string '0.0000' (length=6)
          'base_price' => string '0.0000' (length=6)
          'discount_percent' => string '0.0000' (length=6)
          'discount_amount' => string '0.0000' (length=6)
          'base_discount_amount' => string '0.0000' (length=6)
          'tax_percent' => string '0.0000' (length=6)
          'tax_amount' => string '0.0000' (length=6)
          'base_tax_amount' => string '0.0000' (length=6)
          'row_total' => string '0.0000' (length=6)
          'base_row_total' => string '0.0000' (length=6)
          'row_total_with_discount' => string '0.0000' (length=6)
          'price_incl_tax' => null
          'base_price_incl_tax' => null
          'row_total_incl_tax' => null
          'base_row_total_incl_tax' => null
          'weee_tax_applied' => null
          'weee_tax_applied_amount' => null
          'options' => string '[]' (length=2)

I want to add extension attribute of Image but I need SKU for that, But in this return data Magento doesn't provide SKU.

File is used for API

/vendor/magento/module-quote/Model/Cart/CartTotalRepository.php

Function is used for API

public function get($cartId)

Any one have Fixed the above issue, Or Reported the issue in git ?

How to get SKU in /rest/V1/carts/mine/totals response API

Was it helpful?

Solution

Extension attribute is the simplest way to achieve this.

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\TotalsItemInterface">
      <attribute code="sku" type="string" />
    </extension_attributes>
</config>

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\CartTotalRepositoryInterface">
        <plugin name="total_cart_repository_plugin" type="Vendor\Module\Plugin\TotalRepositoryPlugin"/>
    </type>
</config>

TotalRepositoryPlugin.php

<?php

namespace Vendor\Module\Plugin;

class TotalRepositoryPlugin
{

    /**
     * @param \Magento\Authorization\Model\UserContextInterface $userContext
     * @param \Hexcrypto\WishlistAPI\Helper\Data $wishlistHelper
     */
    public function __construct(
        \Magento\Quote\Model\Quote\ItemFactory $itemFactory,
        \Magento\Quote\Api\Data\TotalsItemExtensionFactory $totalItemExtensionFactory    
    ) {
        $this->itemFactory = $itemFactory;
        $this->totalItemExtension = $totalItemExtensionFactory;

    }

    /**
     * add sku in total cart items
     *
     * @param  \Magento\Quote\Api\CartTotalRepositoryInterface $subject
     * @param  \Magento\Quote\Api\Data\TotalsInterface $totals
     * @return \Magento\Quote\Api\Data\TotalsInterface $totals
     */
    public function afterGet(
        \Magento\Quote\Api\CartTotalRepositoryInterface $subject,
        \Magento\Quote\Api\Data\TotalsInterface $totals
    ) {
        foreach($totals->getItems() as $item)
        {
            $quoteItem = $this->itemFactory->create()->load($item->getItemId());
            $extensionAttributes = $item->getExtensionAttributes();
            if ($extensionAttributes === null) {
                $extensionAttributes = $this->totalItemExtension->create();
            }
            $extensionAttributes->setSku($quoteItem->getSku());
            $item->setExtensionAttributes($extensionAttributes);
        }

        return $totals;
    }

}

I hope it will work. Let me know if I can help you further :)

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top