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.

  • let me know whether u got the solution for this ? – Nagaraju Kasa Aug 16 '17 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. – manish_khot Aug 16 '17 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 ? – Nagaraju Kasa Aug 16 '17 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. – manish_khot Aug 16 '17 at 11:44
  • 1
    Added my answer to your question. Hope that will help you. Please accept it if it helps you. – manish_khot Aug 16 '17 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.