1

I want to add custom attribute in quote items Magento\Quote\Api\Data\CartItemExtensionInterface

<extension_attributes for="Magento\Quote\Api\Data\CartItemExtensionInterface">
    <attribute code="cust_price" type="float" />
</extension_attributes>

I did this doesn't know how to proceed further.
Dont know which plugin i have to use to accept and set attribute data
I have run API for add item

POST : rest/V1/carts/mine/items?cart_id=27333

{
"cartItem": 
    {
        "sku": "SCC010010000",
        "qty": 1,
        "price": 15,
        "quote_id": "27333",
        "extension_attributes": {
            "cust_price" : 25
        }
    }
}

And error is Property \"CustPrice\" does not have corresponding setter in class \"Magento\\Quote\\Api\\Data\\CartItemExtensionInterface\".

8
  • use plugin at api level when product adding to cart. Aug 9, 2019 at 10:01
  • which method and which type name i have to override? Aug 9, 2019 at 10:06
  • Plugin is not meant to override the file, it meant for update object while some action. Please read about magento plugin from devdocs.magento.com/guides/v2.3/extension-dev-guide/… Aug 9, 2019 at 10:19
  • yes i know that but dont know which <type name="{ObservedType}"> and methods i have to change Aug 9, 2019 at 10:24
  • will you be specific by answer> Aug 9, 2019 at 10:24

1 Answer 1

-1

The following approach works fine for me to add the custom attribute or field to the quote item using the extension attribute in Magento 2.

Step 1: Create module.xml file.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Company_CustomModule" setup_version="1.0.2">
    </module>
</config>

Step 2: Create extension_attributes.xml file.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Quote\Api\Data\CartItemInterface">
    <attribute code="bambuseritem_flag" type="string" />
</extension_attributes>
</config>

Step 3: Create di.xml file.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Quote\Model\Quote\Item\Repository" type="Company\CustomModule\Model\Quote\Item\Repository" />
</config>

Step 4: Create UpgradeSchema.php file.

<?php

namespace Company\CustomModule\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;

class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        $setup->startSetup();
        if (version_compare($context->getVersion(), '1.0.2', '<')) {
            $tableName = 'quote_item';
            if ($installer->getConnection()->isTableExists($tableName) ) {
                $installer->getConnection()->addColumn(
                    $installer->getTable($tableName),
                    'custom_flag',
                    [
                        'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        'length' => 10,
                        'nullable' => true,
                        'comment' => 'Custom Flag'
                    ]
                );
            }
        }
        $setup->endSetup();
    }
} 

Step 5: Send custom flag filed in add to cart REST API payload Or Request for Guest & Register user are as below.

{
    "cart_item": {
        "quote_id": "75d1a5424a279581a34b9a1b4f4df020",
        "product_type": "configurable",
        "sku": "214480140",
        "qty": 1,
        "product_option": {
            "extension_attributes": {
                "configurable_item_options": [
                    {
                        "option_id": "93",
                        "option_value": "676"
                    },
                    {
                        "option_id": "188",
                        "option_value": "888"
                    },
                    {
                        "option_id": "189",
                        "option_value": "912"
                    }
                ]
            }
        },
        "extension_attributes": {
            "custom_flag": "Your Custom Value"
        }
    }
}

Step 6: Finally save custom flag filed in quote item using extension attribute.

<?php
/**
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Company\CustomModule\Model\Quote\Item;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;

class Repository extends \Magento\Quote\Model\Quote\Item\Repository
{
    /**
     * Quote repository.
     *
     * @var \Magento\Quote\Api\CartRepositoryInterface
     */
    protected $quoteRepository;

    /**
     * Product repository.
     *
     * @var \Magento\Catalog\Api\ProductRepositoryInterface
     */
    protected $productRepository;

    /**
     * @var \Magento\Quote\Api\Data\CartItemInterfaceFactory
     */
    protected $itemDataFactory;

    /**
     * @var CartItemProcessorInterface[]
     */
    protected $cartItemProcessors;

    /**
     * @var CartItemOptionsProcessor
     */
    private $cartItemOptionsProcessor;

    /**
     * @param \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
     * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
     * @param \Magento\Quote\Api\Data\CartItemInterfaceFactory $itemDataFactory
     * @param CartItemProcessorInterface[] $cartItemProcessors
     */
    public function __construct(
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Quote\Api\Data\CartItemInterfaceFactory $itemDataFactory,
        array $cartItemProcessors = []
    ) {
        $this->quoteRepository = $quoteRepository;
        $this->productRepository = $productRepository;
        $this->itemDataFactory = $itemDataFactory;
        $this->cartItemProcessors = $cartItemProcessors;
    }

    /**
     * {@inheritdoc}
     */
    public function save(\Magento\Quote\Api\Data\CartItemInterface $cartItem)
    {
        /** @var \Magento\Quote\Model\Quote $quote */
        $cartId = $cartItem->getQuoteId();
        $quote = $this->quoteRepository->getActive($cartId);
        $quoteItems = $quote->getItems();
        $quoteItems[] = $cartItem;
        $quote->setItems($quoteItems);
        $this->quoteRepository->save($quote);
        $quote->collectTotals();
        $extensionAttributes = $cartItem->getExtensionAttributes();
        if(null !== $extensionAttributes && null !== $extensionAttributes->getCustomFlag())             
        {
            $itemq = $quote->getItemById($quote->getLastAddedItem()->getItemId());
            $itemq->setCustomFlag($cartItem->getExtensionAttributes()->getCustomFlag());
            $itemq->save();
        }             
        return $quote->getLastAddedItem(); 
    }    
}
7
  • I want to save during go to checkout button. How can i proceed for it ? Aug 3, 2022 at 8:30
  • Hey, I have provided detailed answers with the API Request body as well. Can you explain whether you are using Rest API? Aug 3, 2022 at 12:48
  • I am trying to use during go to checkoout from shopping cart page to checkout Aug 4, 2022 at 12:30
  • You need to use the Plugin or Event. Event example for your case is controller_action_predispatch_checkout_index_index for more details refer to this link. magento.stackexchange.com/questions/138355/… Aug 4, 2022 at 13:25
  • I have added custom value to each and every item by select option. now i want to save the save in quote_item table during go to checkout. Aug 4, 2022 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.