Magento 2 How to add custom attribute to Quote Item Rest API
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
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="MagentoQuoteApiDataCartItemInterface">
<attribute code="product_option" type="float" />
</extension_attributes>
</config>
di.xml
<type name="MagentoQuoteModelQuoteItem">
<plugin name="cs_product_opt" type="NamespaceProductPluginModelCartCartTotalRepositoryPlugin" />
</type>
Plugin file
<?php
/**
* Copyright 2016 aheadWorks. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace NamespaceProductPluginModelCart;
use MagentoQuoteApiDataTotalsExtensionInterface;
use MagentoQuoteApiDataTotalsExtensionFactory;
use MagentoQuoteApiDataCartItemExtensionFactory;
use MagentoQuoteApiDataTotalsInterface;
use MagentoQuoteApiCartRepositoryInterface;
use MagentoQuoteModelCartCartTotalRepository as TotalRepository;
use MagentoQuoteModelQuote;
/**
* Class AheadworksStoreCreditPluginModelCartCartTotalRepositoryPlugin
*/
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 MagentoQuoteApiDataTotalsExtensionInterface $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.
magento2 api rest-api
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
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="MagentoQuoteApiDataCartItemInterface">
<attribute code="product_option" type="float" />
</extension_attributes>
</config>
di.xml
<type name="MagentoQuoteModelQuoteItem">
<plugin name="cs_product_opt" type="NamespaceProductPluginModelCartCartTotalRepositoryPlugin" />
</type>
Plugin file
<?php
/**
* Copyright 2016 aheadWorks. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace NamespaceProductPluginModelCart;
use MagentoQuoteApiDataTotalsExtensionInterface;
use MagentoQuoteApiDataTotalsExtensionFactory;
use MagentoQuoteApiDataCartItemExtensionFactory;
use MagentoQuoteApiDataTotalsInterface;
use MagentoQuoteApiCartRepositoryInterface;
use MagentoQuoteModelCartCartTotalRepository as TotalRepository;
use MagentoQuoteModelQuote;
/**
* Class AheadworksStoreCreditPluginModelCartCartTotalRepositoryPlugin
*/
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 MagentoQuoteApiDataTotalsExtensionInterface $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.
magento2 api rest-api
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
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="MagentoQuoteApiDataCartItemInterface">
<attribute code="product_option" type="float" />
</extension_attributes>
</config>
di.xml
<type name="MagentoQuoteModelQuoteItem">
<plugin name="cs_product_opt" type="NamespaceProductPluginModelCartCartTotalRepositoryPlugin" />
</type>
Plugin file
<?php
/**
* Copyright 2016 aheadWorks. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace NamespaceProductPluginModelCart;
use MagentoQuoteApiDataTotalsExtensionInterface;
use MagentoQuoteApiDataTotalsExtensionFactory;
use MagentoQuoteApiDataCartItemExtensionFactory;
use MagentoQuoteApiDataTotalsInterface;
use MagentoQuoteApiCartRepositoryInterface;
use MagentoQuoteModelCartCartTotalRepository as TotalRepository;
use MagentoQuoteModelQuote;
/**
* Class AheadworksStoreCreditPluginModelCartCartTotalRepositoryPlugin
*/
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 MagentoQuoteApiDataTotalsExtensionInterface $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.
magento2 api rest-api
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="MagentoQuoteApiDataCartItemInterface">
<attribute code="product_option" type="float" />
</extension_attributes>
</config>
di.xml
<type name="MagentoQuoteModelQuoteItem">
<plugin name="cs_product_opt" type="NamespaceProductPluginModelCartCartTotalRepositoryPlugin" />
</type>
Plugin file
<?php
/**
* Copyright 2016 aheadWorks. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace NamespaceProductPluginModelCart;
use MagentoQuoteApiDataTotalsExtensionInterface;
use MagentoQuoteApiDataTotalsExtensionFactory;
use MagentoQuoteApiDataCartItemExtensionFactory;
use MagentoQuoteApiDataTotalsInterface;
use MagentoQuoteApiCartRepositoryInterface;
use MagentoQuoteModelCartCartTotalRepository as TotalRepository;
use MagentoQuoteModelQuote;
/**
* Class AheadworksStoreCreditPluginModelCartCartTotalRepositoryPlugin
*/
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 MagentoQuoteApiDataTotalsExtensionInterface $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.
magento2 api rest-api
magento2 api rest-api
asked Nov 21 '18 at 6:01
Niranjan GondaliyaNiranjan Gondaliya
1789
1789
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ yesterday
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
To add new attribute on cart item. you need to consume V1/carts/mine service.
CartRepositoryPlugin.php
<?php
/**
* Copyright 2016 aheadWorks. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace NamespaceProductPluginModelCart;
use MagentoQuoteApi;
use MagentoQuoteApiCartRepositoryInterface;
use MagentoQuoteApiCartTotalRepositoryInterface;
use MagentoCatalogHelperProductConfigurationPool;
use MagentoFrameworkApiDataObjectHelper;
use MagentoFrameworkApiExtensibleDataInterface;
use MagentoQuoteModelCartTotalsItemConverter;
use MagentoQuoteApiCouponManagementInterface;
use MagentoQuoteModelCartTotalsConverter;
use MagentoQuoteApiDataCartItemExtensionFactory;
use MagentoQuoteModelCartCartTotalRepository as TotalRepository;
/**
* Class AheadworksStoreCreditPluginModelCartCartTotalRepositoryPlugin
*/
class CartRepositoryPlugin
{
/**
* Cart totals factory.
*
* @var ApiDataTotalsInterfaceFactory
*/
private $totalsFactory;
/**
* Quote repository.
*
* @var MagentoQuoteApiCartRepositoryInterface
*/
private $quoteRepository;
/**
* @var MagentoFrameworkApiDataObjectHelper
*/
private $dataObjectHelper;
/**
* @var ConfigurationPool
*/
private $itemConverter;
/**
* @var CouponManagementInterface
*/
protected $couponService;
/**
* @var TotalsConverter
*/
protected $totalsConverter;
/**
* @param ApiDataTotalsInterfaceFactory $totalsFactory
* @param CartRepositoryInterface $quoteRepository
* @param DataObjectHelper $dataObjectHelper
* @param CouponManagementInterface $couponService
* @param TotalsConverter $totalsConverter
* @param ItemConverter $converter
*/
public function __construct(
ApiDataTotalsInterfaceFactory $totalsFactory,
CartRepositoryInterface $quoteRepository,
DataObjectHelper $dataObjectHelper,
CouponManagementInterface $couponService,
TotalsConverter $totalsConverter,
CartItemExtensionFactory $cartItemInterfaceFactory,
ItemConverter $converter
) {
$this->totalsFactory = $totalsFactory;
$this->quoteRepository = $quoteRepository;
$this->dataObjectHelper = $dataObjectHelper;
$this->couponService = $couponService;
$this->totalsConverter = $totalsConverter;
$this->cartItemInterfaceFactory = $cartItemInterfaceFactory;
$this->itemConverter = $converter;
}
/**
* 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 MagentoQuoteModelQuote $quote */
$quote = $this->quoteRepository->getActive($cartId);
if ($quote->isVirtual()) {
$addressTotalsData = $quote->getBillingAddress()->getData();
$addressTotals = $quote->getBillingAddress()->getTotals();
} else {
$addressTotalsData = $quote->getShippingAddress()->getData();
$addressTotals = $quote->getShippingAddress()->getTotals();
}
unset($addressTotalsData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
/** @var MagentoQuoteApiDataTotalsInterface $quoteTotals */
$quoteTotals = $this->totalsFactory->create();
$this->dataObjectHelper->populateWithArray(
$quoteTotals,
$addressTotalsData,
MagentoQuoteApiDataTotalsInterface::class
);
$items = ;
foreach ($quote->getAllVisibleItems() as $index => $item) {
$cartItemExtensionAttributes = $item->getExtensionAttributes();
if($cartItemExtensionAttributes == null){
$cartItemExtensionAttributes = $this->cartItemInterfaceFactory->create();
$cartItemExtensionAttributes->setProductOption('Product Option Check');
$item->setExtensionAttributes($cartItemExtensionAttributes);
$item->save();
}
$items[$index] = $this->itemConverter->modelToDataObject($item);
}
$calculatedTotals = $this->totalsConverter->process($addressTotals);
$quoteTotals->setTotalSegments($calculatedTotals);
$amount = $quoteTotals->getGrandTotal() - $quoteTotals->getTaxAmount();
$amount = $amount > 0 ? $amount : 0;
$quoteTotals->setCouponCode($this->couponService->get($cartId));
$quoteTotals->setGrandTotal($amount);
$quoteTotals->setItems($items);
$quoteTotals->setItemsQty($quote->getItemsQty());
$quoteTotals->setBaseCurrencyCode($quote->getBaseCurrencyCode());
$quoteTotals->setQuoteCurrencyCode($quote->getQuoteCurrencyCode());
return $quoteTotals;
}
}
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="MagentoQuoteApiDataCartItemInterface">
<attribute code="product_option" type="string" />
</extension_attributes>
di.xml
<type name="MagentoQuoteModelCartCartTotalRepository">
<plugin name="cp_product_opt" type="CommercepunditProductPluginModelCartCartRepositoryPlugin" />
</type>
I hope it'll work for you. Let me know if you need further help.
no i no needed in total i want this in Quote Item.
– Niranjan Gondaliya
Nov 21 '18 at 6:32
if you need this in cart item then you need to do create your plugin on cart repository as total api does not return item data
– Ramkishan Suthar
Nov 21 '18 at 6:57
Please check updated answer.
– Ramkishan Suthar
Nov 21 '18 at 7:05
ok Let me implement
– Niranjan Gondaliya
Nov 21 '18 at 7:13
Call to undefined method Magento\Quote\Model\QuoteRepository\Interceptor::getItems()
– Niranjan Gondaliya
Nov 21 '18 at 7:32
|
show 5 more comments
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%2f250744%2fmagento-2-how-to-add-custom-attribute-to-quote-item-rest-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
To add new attribute on cart item. you need to consume V1/carts/mine service.
CartRepositoryPlugin.php
<?php
/**
* Copyright 2016 aheadWorks. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace NamespaceProductPluginModelCart;
use MagentoQuoteApi;
use MagentoQuoteApiCartRepositoryInterface;
use MagentoQuoteApiCartTotalRepositoryInterface;
use MagentoCatalogHelperProductConfigurationPool;
use MagentoFrameworkApiDataObjectHelper;
use MagentoFrameworkApiExtensibleDataInterface;
use MagentoQuoteModelCartTotalsItemConverter;
use MagentoQuoteApiCouponManagementInterface;
use MagentoQuoteModelCartTotalsConverter;
use MagentoQuoteApiDataCartItemExtensionFactory;
use MagentoQuoteModelCartCartTotalRepository as TotalRepository;
/**
* Class AheadworksStoreCreditPluginModelCartCartTotalRepositoryPlugin
*/
class CartRepositoryPlugin
{
/**
* Cart totals factory.
*
* @var ApiDataTotalsInterfaceFactory
*/
private $totalsFactory;
/**
* Quote repository.
*
* @var MagentoQuoteApiCartRepositoryInterface
*/
private $quoteRepository;
/**
* @var MagentoFrameworkApiDataObjectHelper
*/
private $dataObjectHelper;
/**
* @var ConfigurationPool
*/
private $itemConverter;
/**
* @var CouponManagementInterface
*/
protected $couponService;
/**
* @var TotalsConverter
*/
protected $totalsConverter;
/**
* @param ApiDataTotalsInterfaceFactory $totalsFactory
* @param CartRepositoryInterface $quoteRepository
* @param DataObjectHelper $dataObjectHelper
* @param CouponManagementInterface $couponService
* @param TotalsConverter $totalsConverter
* @param ItemConverter $converter
*/
public function __construct(
ApiDataTotalsInterfaceFactory $totalsFactory,
CartRepositoryInterface $quoteRepository,
DataObjectHelper $dataObjectHelper,
CouponManagementInterface $couponService,
TotalsConverter $totalsConverter,
CartItemExtensionFactory $cartItemInterfaceFactory,
ItemConverter $converter
) {
$this->totalsFactory = $totalsFactory;
$this->quoteRepository = $quoteRepository;
$this->dataObjectHelper = $dataObjectHelper;
$this->couponService = $couponService;
$this->totalsConverter = $totalsConverter;
$this->cartItemInterfaceFactory = $cartItemInterfaceFactory;
$this->itemConverter = $converter;
}
/**
* 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 MagentoQuoteModelQuote $quote */
$quote = $this->quoteRepository->getActive($cartId);
if ($quote->isVirtual()) {
$addressTotalsData = $quote->getBillingAddress()->getData();
$addressTotals = $quote->getBillingAddress()->getTotals();
} else {
$addressTotalsData = $quote->getShippingAddress()->getData();
$addressTotals = $quote->getShippingAddress()->getTotals();
}
unset($addressTotalsData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
/** @var MagentoQuoteApiDataTotalsInterface $quoteTotals */
$quoteTotals = $this->totalsFactory->create();
$this->dataObjectHelper->populateWithArray(
$quoteTotals,
$addressTotalsData,
MagentoQuoteApiDataTotalsInterface::class
);
$items = ;
foreach ($quote->getAllVisibleItems() as $index => $item) {
$cartItemExtensionAttributes = $item->getExtensionAttributes();
if($cartItemExtensionAttributes == null){
$cartItemExtensionAttributes = $this->cartItemInterfaceFactory->create();
$cartItemExtensionAttributes->setProductOption('Product Option Check');
$item->setExtensionAttributes($cartItemExtensionAttributes);
$item->save();
}
$items[$index] = $this->itemConverter->modelToDataObject($item);
}
$calculatedTotals = $this->totalsConverter->process($addressTotals);
$quoteTotals->setTotalSegments($calculatedTotals);
$amount = $quoteTotals->getGrandTotal() - $quoteTotals->getTaxAmount();
$amount = $amount > 0 ? $amount : 0;
$quoteTotals->setCouponCode($this->couponService->get($cartId));
$quoteTotals->setGrandTotal($amount);
$quoteTotals->setItems($items);
$quoteTotals->setItemsQty($quote->getItemsQty());
$quoteTotals->setBaseCurrencyCode($quote->getBaseCurrencyCode());
$quoteTotals->setQuoteCurrencyCode($quote->getQuoteCurrencyCode());
return $quoteTotals;
}
}
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="MagentoQuoteApiDataCartItemInterface">
<attribute code="product_option" type="string" />
</extension_attributes>
di.xml
<type name="MagentoQuoteModelCartCartTotalRepository">
<plugin name="cp_product_opt" type="CommercepunditProductPluginModelCartCartRepositoryPlugin" />
</type>
I hope it'll work for you. Let me know if you need further help.
no i no needed in total i want this in Quote Item.
– Niranjan Gondaliya
Nov 21 '18 at 6:32
if you need this in cart item then you need to do create your plugin on cart repository as total api does not return item data
– Ramkishan Suthar
Nov 21 '18 at 6:57
Please check updated answer.
– Ramkishan Suthar
Nov 21 '18 at 7:05
ok Let me implement
– Niranjan Gondaliya
Nov 21 '18 at 7:13
Call to undefined method Magento\Quote\Model\QuoteRepository\Interceptor::getItems()
– Niranjan Gondaliya
Nov 21 '18 at 7:32
|
show 5 more comments
To add new attribute on cart item. you need to consume V1/carts/mine service.
CartRepositoryPlugin.php
<?php
/**
* Copyright 2016 aheadWorks. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace NamespaceProductPluginModelCart;
use MagentoQuoteApi;
use MagentoQuoteApiCartRepositoryInterface;
use MagentoQuoteApiCartTotalRepositoryInterface;
use MagentoCatalogHelperProductConfigurationPool;
use MagentoFrameworkApiDataObjectHelper;
use MagentoFrameworkApiExtensibleDataInterface;
use MagentoQuoteModelCartTotalsItemConverter;
use MagentoQuoteApiCouponManagementInterface;
use MagentoQuoteModelCartTotalsConverter;
use MagentoQuoteApiDataCartItemExtensionFactory;
use MagentoQuoteModelCartCartTotalRepository as TotalRepository;
/**
* Class AheadworksStoreCreditPluginModelCartCartTotalRepositoryPlugin
*/
class CartRepositoryPlugin
{
/**
* Cart totals factory.
*
* @var ApiDataTotalsInterfaceFactory
*/
private $totalsFactory;
/**
* Quote repository.
*
* @var MagentoQuoteApiCartRepositoryInterface
*/
private $quoteRepository;
/**
* @var MagentoFrameworkApiDataObjectHelper
*/
private $dataObjectHelper;
/**
* @var ConfigurationPool
*/
private $itemConverter;
/**
* @var CouponManagementInterface
*/
protected $couponService;
/**
* @var TotalsConverter
*/
protected $totalsConverter;
/**
* @param ApiDataTotalsInterfaceFactory $totalsFactory
* @param CartRepositoryInterface $quoteRepository
* @param DataObjectHelper $dataObjectHelper
* @param CouponManagementInterface $couponService
* @param TotalsConverter $totalsConverter
* @param ItemConverter $converter
*/
public function __construct(
ApiDataTotalsInterfaceFactory $totalsFactory,
CartRepositoryInterface $quoteRepository,
DataObjectHelper $dataObjectHelper,
CouponManagementInterface $couponService,
TotalsConverter $totalsConverter,
CartItemExtensionFactory $cartItemInterfaceFactory,
ItemConverter $converter
) {
$this->totalsFactory = $totalsFactory;
$this->quoteRepository = $quoteRepository;
$this->dataObjectHelper = $dataObjectHelper;
$this->couponService = $couponService;
$this->totalsConverter = $totalsConverter;
$this->cartItemInterfaceFactory = $cartItemInterfaceFactory;
$this->itemConverter = $converter;
}
/**
* 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 MagentoQuoteModelQuote $quote */
$quote = $this->quoteRepository->getActive($cartId);
if ($quote->isVirtual()) {
$addressTotalsData = $quote->getBillingAddress()->getData();
$addressTotals = $quote->getBillingAddress()->getTotals();
} else {
$addressTotalsData = $quote->getShippingAddress()->getData();
$addressTotals = $quote->getShippingAddress()->getTotals();
}
unset($addressTotalsData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
/** @var MagentoQuoteApiDataTotalsInterface $quoteTotals */
$quoteTotals = $this->totalsFactory->create();
$this->dataObjectHelper->populateWithArray(
$quoteTotals,
$addressTotalsData,
MagentoQuoteApiDataTotalsInterface::class
);
$items = ;
foreach ($quote->getAllVisibleItems() as $index => $item) {
$cartItemExtensionAttributes = $item->getExtensionAttributes();
if($cartItemExtensionAttributes == null){
$cartItemExtensionAttributes = $this->cartItemInterfaceFactory->create();
$cartItemExtensionAttributes->setProductOption('Product Option Check');
$item->setExtensionAttributes($cartItemExtensionAttributes);
$item->save();
}
$items[$index] = $this->itemConverter->modelToDataObject($item);
}
$calculatedTotals = $this->totalsConverter->process($addressTotals);
$quoteTotals->setTotalSegments($calculatedTotals);
$amount = $quoteTotals->getGrandTotal() - $quoteTotals->getTaxAmount();
$amount = $amount > 0 ? $amount : 0;
$quoteTotals->setCouponCode($this->couponService->get($cartId));
$quoteTotals->setGrandTotal($amount);
$quoteTotals->setItems($items);
$quoteTotals->setItemsQty($quote->getItemsQty());
$quoteTotals->setBaseCurrencyCode($quote->getBaseCurrencyCode());
$quoteTotals->setQuoteCurrencyCode($quote->getQuoteCurrencyCode());
return $quoteTotals;
}
}
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="MagentoQuoteApiDataCartItemInterface">
<attribute code="product_option" type="string" />
</extension_attributes>
di.xml
<type name="MagentoQuoteModelCartCartTotalRepository">
<plugin name="cp_product_opt" type="CommercepunditProductPluginModelCartCartRepositoryPlugin" />
</type>
I hope it'll work for you. Let me know if you need further help.
no i no needed in total i want this in Quote Item.
– Niranjan Gondaliya
Nov 21 '18 at 6:32
if you need this in cart item then you need to do create your plugin on cart repository as total api does not return item data
– Ramkishan Suthar
Nov 21 '18 at 6:57
Please check updated answer.
– Ramkishan Suthar
Nov 21 '18 at 7:05
ok Let me implement
– Niranjan Gondaliya
Nov 21 '18 at 7:13
Call to undefined method Magento\Quote\Model\QuoteRepository\Interceptor::getItems()
– Niranjan Gondaliya
Nov 21 '18 at 7:32
|
show 5 more comments
To add new attribute on cart item. you need to consume V1/carts/mine service.
CartRepositoryPlugin.php
<?php
/**
* Copyright 2016 aheadWorks. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace NamespaceProductPluginModelCart;
use MagentoQuoteApi;
use MagentoQuoteApiCartRepositoryInterface;
use MagentoQuoteApiCartTotalRepositoryInterface;
use MagentoCatalogHelperProductConfigurationPool;
use MagentoFrameworkApiDataObjectHelper;
use MagentoFrameworkApiExtensibleDataInterface;
use MagentoQuoteModelCartTotalsItemConverter;
use MagentoQuoteApiCouponManagementInterface;
use MagentoQuoteModelCartTotalsConverter;
use MagentoQuoteApiDataCartItemExtensionFactory;
use MagentoQuoteModelCartCartTotalRepository as TotalRepository;
/**
* Class AheadworksStoreCreditPluginModelCartCartTotalRepositoryPlugin
*/
class CartRepositoryPlugin
{
/**
* Cart totals factory.
*
* @var ApiDataTotalsInterfaceFactory
*/
private $totalsFactory;
/**
* Quote repository.
*
* @var MagentoQuoteApiCartRepositoryInterface
*/
private $quoteRepository;
/**
* @var MagentoFrameworkApiDataObjectHelper
*/
private $dataObjectHelper;
/**
* @var ConfigurationPool
*/
private $itemConverter;
/**
* @var CouponManagementInterface
*/
protected $couponService;
/**
* @var TotalsConverter
*/
protected $totalsConverter;
/**
* @param ApiDataTotalsInterfaceFactory $totalsFactory
* @param CartRepositoryInterface $quoteRepository
* @param DataObjectHelper $dataObjectHelper
* @param CouponManagementInterface $couponService
* @param TotalsConverter $totalsConverter
* @param ItemConverter $converter
*/
public function __construct(
ApiDataTotalsInterfaceFactory $totalsFactory,
CartRepositoryInterface $quoteRepository,
DataObjectHelper $dataObjectHelper,
CouponManagementInterface $couponService,
TotalsConverter $totalsConverter,
CartItemExtensionFactory $cartItemInterfaceFactory,
ItemConverter $converter
) {
$this->totalsFactory = $totalsFactory;
$this->quoteRepository = $quoteRepository;
$this->dataObjectHelper = $dataObjectHelper;
$this->couponService = $couponService;
$this->totalsConverter = $totalsConverter;
$this->cartItemInterfaceFactory = $cartItemInterfaceFactory;
$this->itemConverter = $converter;
}
/**
* 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 MagentoQuoteModelQuote $quote */
$quote = $this->quoteRepository->getActive($cartId);
if ($quote->isVirtual()) {
$addressTotalsData = $quote->getBillingAddress()->getData();
$addressTotals = $quote->getBillingAddress()->getTotals();
} else {
$addressTotalsData = $quote->getShippingAddress()->getData();
$addressTotals = $quote->getShippingAddress()->getTotals();
}
unset($addressTotalsData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
/** @var MagentoQuoteApiDataTotalsInterface $quoteTotals */
$quoteTotals = $this->totalsFactory->create();
$this->dataObjectHelper->populateWithArray(
$quoteTotals,
$addressTotalsData,
MagentoQuoteApiDataTotalsInterface::class
);
$items = ;
foreach ($quote->getAllVisibleItems() as $index => $item) {
$cartItemExtensionAttributes = $item->getExtensionAttributes();
if($cartItemExtensionAttributes == null){
$cartItemExtensionAttributes = $this->cartItemInterfaceFactory->create();
$cartItemExtensionAttributes->setProductOption('Product Option Check');
$item->setExtensionAttributes($cartItemExtensionAttributes);
$item->save();
}
$items[$index] = $this->itemConverter->modelToDataObject($item);
}
$calculatedTotals = $this->totalsConverter->process($addressTotals);
$quoteTotals->setTotalSegments($calculatedTotals);
$amount = $quoteTotals->getGrandTotal() - $quoteTotals->getTaxAmount();
$amount = $amount > 0 ? $amount : 0;
$quoteTotals->setCouponCode($this->couponService->get($cartId));
$quoteTotals->setGrandTotal($amount);
$quoteTotals->setItems($items);
$quoteTotals->setItemsQty($quote->getItemsQty());
$quoteTotals->setBaseCurrencyCode($quote->getBaseCurrencyCode());
$quoteTotals->setQuoteCurrencyCode($quote->getQuoteCurrencyCode());
return $quoteTotals;
}
}
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="MagentoQuoteApiDataCartItemInterface">
<attribute code="product_option" type="string" />
</extension_attributes>
di.xml
<type name="MagentoQuoteModelCartCartTotalRepository">
<plugin name="cp_product_opt" type="CommercepunditProductPluginModelCartCartRepositoryPlugin" />
</type>
I hope it'll work for you. Let me know if you need further help.
To add new attribute on cart item. you need to consume V1/carts/mine service.
CartRepositoryPlugin.php
<?php
/**
* Copyright 2016 aheadWorks. All rights reserved.
* See LICENSE.txt for license details.
*/
namespace NamespaceProductPluginModelCart;
use MagentoQuoteApi;
use MagentoQuoteApiCartRepositoryInterface;
use MagentoQuoteApiCartTotalRepositoryInterface;
use MagentoCatalogHelperProductConfigurationPool;
use MagentoFrameworkApiDataObjectHelper;
use MagentoFrameworkApiExtensibleDataInterface;
use MagentoQuoteModelCartTotalsItemConverter;
use MagentoQuoteApiCouponManagementInterface;
use MagentoQuoteModelCartTotalsConverter;
use MagentoQuoteApiDataCartItemExtensionFactory;
use MagentoQuoteModelCartCartTotalRepository as TotalRepository;
/**
* Class AheadworksStoreCreditPluginModelCartCartTotalRepositoryPlugin
*/
class CartRepositoryPlugin
{
/**
* Cart totals factory.
*
* @var ApiDataTotalsInterfaceFactory
*/
private $totalsFactory;
/**
* Quote repository.
*
* @var MagentoQuoteApiCartRepositoryInterface
*/
private $quoteRepository;
/**
* @var MagentoFrameworkApiDataObjectHelper
*/
private $dataObjectHelper;
/**
* @var ConfigurationPool
*/
private $itemConverter;
/**
* @var CouponManagementInterface
*/
protected $couponService;
/**
* @var TotalsConverter
*/
protected $totalsConverter;
/**
* @param ApiDataTotalsInterfaceFactory $totalsFactory
* @param CartRepositoryInterface $quoteRepository
* @param DataObjectHelper $dataObjectHelper
* @param CouponManagementInterface $couponService
* @param TotalsConverter $totalsConverter
* @param ItemConverter $converter
*/
public function __construct(
ApiDataTotalsInterfaceFactory $totalsFactory,
CartRepositoryInterface $quoteRepository,
DataObjectHelper $dataObjectHelper,
CouponManagementInterface $couponService,
TotalsConverter $totalsConverter,
CartItemExtensionFactory $cartItemInterfaceFactory,
ItemConverter $converter
) {
$this->totalsFactory = $totalsFactory;
$this->quoteRepository = $quoteRepository;
$this->dataObjectHelper = $dataObjectHelper;
$this->couponService = $couponService;
$this->totalsConverter = $totalsConverter;
$this->cartItemInterfaceFactory = $cartItemInterfaceFactory;
$this->itemConverter = $converter;
}
/**
* 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 MagentoQuoteModelQuote $quote */
$quote = $this->quoteRepository->getActive($cartId);
if ($quote->isVirtual()) {
$addressTotalsData = $quote->getBillingAddress()->getData();
$addressTotals = $quote->getBillingAddress()->getTotals();
} else {
$addressTotalsData = $quote->getShippingAddress()->getData();
$addressTotals = $quote->getShippingAddress()->getTotals();
}
unset($addressTotalsData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]);
/** @var MagentoQuoteApiDataTotalsInterface $quoteTotals */
$quoteTotals = $this->totalsFactory->create();
$this->dataObjectHelper->populateWithArray(
$quoteTotals,
$addressTotalsData,
MagentoQuoteApiDataTotalsInterface::class
);
$items = ;
foreach ($quote->getAllVisibleItems() as $index => $item) {
$cartItemExtensionAttributes = $item->getExtensionAttributes();
if($cartItemExtensionAttributes == null){
$cartItemExtensionAttributes = $this->cartItemInterfaceFactory->create();
$cartItemExtensionAttributes->setProductOption('Product Option Check');
$item->setExtensionAttributes($cartItemExtensionAttributes);
$item->save();
}
$items[$index] = $this->itemConverter->modelToDataObject($item);
}
$calculatedTotals = $this->totalsConverter->process($addressTotals);
$quoteTotals->setTotalSegments($calculatedTotals);
$amount = $quoteTotals->getGrandTotal() - $quoteTotals->getTaxAmount();
$amount = $amount > 0 ? $amount : 0;
$quoteTotals->setCouponCode($this->couponService->get($cartId));
$quoteTotals->setGrandTotal($amount);
$quoteTotals->setItems($items);
$quoteTotals->setItemsQty($quote->getItemsQty());
$quoteTotals->setBaseCurrencyCode($quote->getBaseCurrencyCode());
$quoteTotals->setQuoteCurrencyCode($quote->getQuoteCurrencyCode());
return $quoteTotals;
}
}
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="MagentoQuoteApiDataCartItemInterface">
<attribute code="product_option" type="string" />
</extension_attributes>
di.xml
<type name="MagentoQuoteModelCartCartTotalRepository">
<plugin name="cp_product_opt" type="CommercepunditProductPluginModelCartCartRepositoryPlugin" />
</type>
I hope it'll work for you. Let me know if you need further help.
edited Nov 22 '18 at 6:07
answered Nov 21 '18 at 6:19
Ramkishan SutharRamkishan Suthar
2,29221435
2,29221435
no i no needed in total i want this in Quote Item.
– Niranjan Gondaliya
Nov 21 '18 at 6:32
if you need this in cart item then you need to do create your plugin on cart repository as total api does not return item data
– Ramkishan Suthar
Nov 21 '18 at 6:57
Please check updated answer.
– Ramkishan Suthar
Nov 21 '18 at 7:05
ok Let me implement
– Niranjan Gondaliya
Nov 21 '18 at 7:13
Call to undefined method Magento\Quote\Model\QuoteRepository\Interceptor::getItems()
– Niranjan Gondaliya
Nov 21 '18 at 7:32
|
show 5 more comments
no i no needed in total i want this in Quote Item.
– Niranjan Gondaliya
Nov 21 '18 at 6:32
if you need this in cart item then you need to do create your plugin on cart repository as total api does not return item data
– Ramkishan Suthar
Nov 21 '18 at 6:57
Please check updated answer.
– Ramkishan Suthar
Nov 21 '18 at 7:05
ok Let me implement
– Niranjan Gondaliya
Nov 21 '18 at 7:13
Call to undefined method Magento\Quote\Model\QuoteRepository\Interceptor::getItems()
– Niranjan Gondaliya
Nov 21 '18 at 7:32
no i no needed in total i want this in Quote Item.
– Niranjan Gondaliya
Nov 21 '18 at 6:32
no i no needed in total i want this in Quote Item.
– Niranjan Gondaliya
Nov 21 '18 at 6:32
if you need this in cart item then you need to do create your plugin on cart repository as total api does not return item data
– Ramkishan Suthar
Nov 21 '18 at 6:57
if you need this in cart item then you need to do create your plugin on cart repository as total api does not return item data
– Ramkishan Suthar
Nov 21 '18 at 6:57
Please check updated answer.
– Ramkishan Suthar
Nov 21 '18 at 7:05
Please check updated answer.
– Ramkishan Suthar
Nov 21 '18 at 7:05
ok Let me implement
– Niranjan Gondaliya
Nov 21 '18 at 7:13
ok Let me implement
– Niranjan Gondaliya
Nov 21 '18 at 7:13
Call to undefined method Magento\Quote\Model\QuoteRepository\Interceptor::getItems()
– Niranjan Gondaliya
Nov 21 '18 at 7:32
Call to undefined method Magento\Quote\Model\QuoteRepository\Interceptor::getItems()
– Niranjan Gondaliya
Nov 21 '18 at 7:32
|
show 5 more comments
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%2f250744%2fmagento-2-how-to-add-custom-attribute-to-quote-item-rest-api%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