0

I have a controller with following code

$data = $this->getRequest()->getPostValue();
 $item_id = $data['item_id'];
 $additionalOptions = $data['additional_options'];
 $itemFactory = $this->itemFactory->create()->load($item_id);
 $itemFactory->setAdditionalData($additionalOptions);
 $itemFactory->save();

but it throws following error while saving the quote item data as follows.

Fatal error: Uncaught Error: Call to a member function getStoreId() on null in vendor/magento/module-quote/Model/Quote/Item/AbstractItem.php:144 Stack trace: #0

asked Nov 1, 2018 at 7:38

1 Answer 1

2

I think you are trying to loading an the model \Magento\Quote\Model\Quote\Item with quote_id parameter, which I think won't work.

What you need to do here is, use the quote_id parameter to load \Magento\Quote\Model\Quote model instance and then pick the right \Magento\Quote\Model\Quote\Item through it and set your data.

So this is what you need to do here.

$quote_id = $data['quote_id'];
$additionalOptions = $data['additional_options'];
$quote = $this->quoteRepository->getById($quote_id);
foreach ($quote->getItems() as $quoteItem) {
 $quoteItem->setAdditionalData($additionalOptions);
}
$quote->save();

Here $this->quoteRepository dependency should be \Magento\Quote\Api\CartRepositoryInterface. Also in the foreach loop, if you want to add further filtering for the quote item, then you should add it too.

answered Nov 1, 2018 at 9:00
2
  • $quote_id variable contains item_id so it that case it should work isn't it.? Commented Nov 1, 2018 at 9:50
  • I've updated the code, just have a look at it. Commented Nov 1, 2018 at 10:14

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.