Magento 2 How to add custom attribute to Quote Item Rest API The 2019 Stack Overflow Developer Survey Results Are InMagento 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
Have you ever entered Singapore using a different passport or name?
How to support a colleague who finds meetings extremely tiring?
What does Linus Torvalds mean when he says that Git "never ever" tracks a file?
Return to UK after being refused entry years previously
Why did Acorn's A3000 have red function keys?
Is this app Icon Browser Safe/Legit?
Is there a symbol for a right arrow with a square in the middle?
Loose spokes after only a few rides
Which Sci-Fi work first showed weapon of galactic-scale mass destruction?
Time travel alters history but people keep saying nothing's changed
For what reasons would an animal species NOT cross a *horizontal* land bridge?
Identify boardgame from Big movie
Why is the maximum length of OpenWrt’s root password 8 characters?
Did 3000BC Egyptians use meteoric iron weapons?
What is the meaning of the verb "bear" in this context?
During Temple times, who can butcher a kosher animal?
How technical should a Scrum Master be to effectively remove impediments?
Did Scotland spend $250,000 for the slogan "Welcome to Scotland"?
Can a rogue use sneak attack with weapons that have the thrown property even if they are not thrown?
If a Druid sees an animal’s corpse, can they wild shape into that animal?
Pokemon Turn Based battle (Python)
Are there any other methods to apply to solving simultaneous equations?
Protecting Dualbooting Windows from dangerous code (like rm -rf)
When should I buy a clipper card after flying to OAK?
Magento 2 How to add custom attribute to Quote Item Rest API
The 2019 Stack Overflow Developer Survey Results Are InMagento 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
bumped to the homepage by Community♦ 8 mins ago
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♦ 8 mins ago
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
1739
1739
bumped to the homepage by Community♦ 8 mins ago
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♦ 8 mins ago
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