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.

asked Aug 9, 2017 at 16:26
9
  • let me know whether u got the solution for this ? Commented 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. Commented 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 ? Commented 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. Commented 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. Commented 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.

Mohit Patel
4,0484 gold badges27 silver badges57 bronze badges
answered Jun 7, 2021 at 17:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.