Magento 2 How to add custom attribute to Quote Item Rest API Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Magento 2 CMS Page getList() repository methods does not return expected objectmain.CRITICAL: Plugin class doesn't existMagento2 add custom address attributeAdd value for custom quote item attribute using REST apiHow to add new field in the Default API responseMagento 2 - Save Extension Attribute afterSave() returns voidCart page error in Magento 2.2.3Add configure product in Cart using Magento 2 API facing an issueMagento2 REST API get all customers detailsMagento 2 REST - SKU to items on cart totals API response is not available, How to get SKU in response API
Do working physicists consider Newtonian mechanics to be "falsified"?
Was credit for the black hole image misattributed?
Why is there no army of Iron-Mans in the MCU?
Statistical model of ligand substitution
Writing Thesis: Copying from published papers
What to do with post with dry rot?
How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time
Need a suitable toxic chemical for a murder plot in my novel
How does modal jazz use chord progressions?
Working around an AWS network ACL rule limit
How is simplicity better than precision and clarity in prose?
New Order #5: where Fibonacci and Beatty meet at Wythoff
I'm having difficulty getting my players to do stuff in a sandbox campaign
Antler Helmet: Can it work?
3 doors, three guards, one stone
Is it possible to ask for a hotel room without minibar/extra services?
Blender game recording at the wrong time
Is there a documented rationale why the House Ways and Means chairman can demand tax info?
Is there a service that would inform me whenever a new direct route is scheduled from a given airport?
Who can trigger ship-wide alerts in Star Trek?
Mortgage adviser recommends a longer term than necessary combined with overpayments
Area of a 2D convex hull
Can a non-EU citizen traveling with me come with me through the EU passport line?
Estimate capacitor parameters
Magento 2 How to add custom attribute to Quote Item Rest API
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Magento 2 CMS Page getList() repository methods does not return expected objectmain.CRITICAL: Plugin class doesn't existMagento2 add custom address attributeAdd value for custom quote item attribute using REST apiHow to add new field in the Default API responseMagento 2 - Save Extension Attribute afterSave() returns voidCart page error in Magento 2.2.3Add configure product in Cart using Magento 2 API facing an issueMagento2 REST API get all customers detailsMagento 2 REST - SKU to items on cart totals API response is not available, How to get SKU in response 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
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
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
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