I want to add custom attribute to >http://{{magento2}}/rest/V1/carts/mine/totals
API response.
I have done follwing way but it not working.
>extension_attributes.xml
<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="product_option" type="float" />
</extension_attributes>
</config>
>di.xml
<type name="Magento\Quote\Model\Quote\Item">
<plugin name="cs_product_opt" type="Namespace\Product\Plugin\Model\Cart\CartTotalRepositoryPlugin" />
</type>
>Plugin file
<?php
/**
* Copyright 2016 aheadWorks. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace Namespace\Product\Plugin\Model\Cart;
use Magento\Quote\Api\Data\TotalsExtensionInterface;
use Magento\Quote\Api\Data\TotalsExtensionFactory;
use Magento\Quote\Api\Data\CartItemExtensionFactory;
use Magento\Quote\Api\Data\TotalsInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Cart\CartTotalRepository as TotalRepository;
use Magento\Quote\Model\Quote;
/**
* Class Aheadworks\StoreCredit\Plugin\Model\Cart\CartTotalRepositoryPlugin
*/
class CartTotalRepositoryPlugin
{
/**
* @var CartRepositoryInterface
*/
private $quoteRepository;
/**
* @var CartItemExtensionFactory
*/
private $totalsExtensionFactory;
/**
* @param CartRepositoryInterface $quoteRepository
* @param CartItemExtensionFactory $totalsExtensionFactory
*/
public function __construct(
CartRepositoryInterface $quoteRepository,
CartItemExtensionFactory $totalsExtensionFactory
) {
$this->quoteRepository = $quoteRepository;
$this->totalsExtensionFactory = $totalsExtensionFactory;
}
/**
* Apply extension attributes to totals
*
* @param TotalRepository $subject
* @param \Closure $proceed
* @param int $cartId
* @return TotalsInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundGet(TotalRepository $subject, \Closure $proceed, $cartId)
{
/** @var TotalsInterface $totals */
$totals = $proceed($cartId);
/** @var \Magento\Quote\Api\Data\TotalsExtensionInterface $extensionAttributes */
$extensionAttributes = $totals->getExtensionAttributes()
? $totals->getExtensionAttributes()
: $this->totalsExtensionFactory->create();
/** @var Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
$extensionAttributes->setProductOption(1.11);
$totals->setExtensionAttributes($extensionAttributes);
return $totals;
}
}
anyone have idea how to add custom attribute to Quote Item Rest API.