0

I want to add value for custom quote attribute using rest api. This attribute is not a product attribute. I have created a database column in quote_item table. Also created the plugin on class Magento\Quote\Model\Quote\Item\Repository on method save().

I am using the extension_attributes to get the value for custom attribute. Below is my request body.

{ "cartItem": 
    { 
        "quote_id": "70", 
        "sku": "VNLIC", 
        "qty": 1,  
        "extension_attributes": {
            "custom_message" : "Quote Item Custom Message Text"
        }   
    } 
}

The plugin code is below:

<?php

namespace Vendor\ModuleName\Plugin;

class QuoteItemRepository
{
    public function beforeSave(
        \Magento\Quote\Model\Quote\Item\Repository\Interceptor $subject,
        $cartItem
    )
    {
        $extensionAttr = $cartItem->getExtensionAttributes();
        if($extensionAttr !== null) {

            $customMessage = $extensionAttr->getCustomMessage();
            if($customMessage !== null) {
                $cartItem->setData('custom_message', $customMessage);
            }

            //$cartItem->save();
        }
        return [$cartItem];
    }
}

If I use this there is no value get populated in the quote_item table for the custom_message column.

If I uncomment $cartItem->save() then I am getting the below error.

Fatal error: Uncaught Error: Call to a member function setFinalPrice() on null in /var/www/html/bakeway/vendor/magento/module-quote/Model/Quote/Item/AbstractItem.php on line 152

Please help.

9
  • let me know whether u got the solution for this ? Aug 16, 2017 at 8:41
  • @nagaraju-kasa : In quote item repository save method you will observe the code $quoteItems[] = $cartItem. This means it push the quote item object you passed in REST to items array. This item is not the one which is saved in the database. So you have to write plugin on before quote save. Aug 16, 2017 at 11:38
  • ok thanks for your reply. ok what about the attribute whether i need to create any where? (or)where i need to configure ? Aug 16, 2017 at 11:42
  • Yes of course. If you want to save it in a database you have to add column in quote_item table using UpgradeSchema. Aug 16, 2017 at 11:44
  • 1
    Added my answer to your question. Hope that will help you. Please accept it if it helps you. Aug 16, 2017 at 12:01

1 Answer 1

0

1.Try to use the around save method in the plugin file.

public function aroundSave(
       CartItemRepositoryInterface $subject, \Closure $proceed, CartItemInterface $entity
 ): mixed
{ }

2.After setting the custom attribute and trying to use the resource model file for saving the attribute.

3.Class Resource File for Quote Item Magento\Quote\Model\ResourceModel\Quote\Item

4.$this->resourceModel->save($cartItem);

Give thump up if it's work.

Your Answer

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

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