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.