I want to update my custom column name order_item_note
in a quote_item table.
I have the below data.
Product Id, and Text to insert.
How to update the column order_item_note
without raw sql
I have overrided magento cart UpdatePost controller by di.xml like below.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MagentoCheckoutControllerCartUpdatePost" type="METMEERCustomerCommentControllerCartUpdatePost" />
</config>
My controller is below.
<?php
namespace METMEERCustomerCommentControllerCart;
use MagentoCheckoutModelCartRequestQuantityProcessor;
use MagentoFrameworkAppActionHttpPostActionInterface;
use MagentoFrameworkAppActionHttpGetActionInterface;
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/**
* Post update shopping cart.
*/
class UpdatePost extends MagentoCheckoutControllerCart implements HttpGetActionInterface, HttpPostActionInterface
{
/**
* @var RequestQuantityProcessor
*/
private $quantityProcessor;
private $quoteItemFactory;
private $itemResourceModel;
/**
* @param MagentoFrameworkAppActionContext $context
* @param MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig
* @param MagentoCheckoutModelSession $checkoutSession
* @param MagentoStoreModelStoreManagerInterface $storeManager
* @param MagentoFrameworkDataFormFormKeyValidator $formKeyValidator
* @param MagentoCheckoutModelCart $cart
* @param RequestQuantityProcessor $quantityProcessor
*/
public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
MagentoCheckoutModelSession $checkoutSession,
MagentoStoreModelStoreManagerInterface $storeManager,
MagentoFrameworkDataFormFormKeyValidator $formKeyValidator,
MagentoCheckoutModelCart $cart,
RequestQuantityProcessor $quantityProcessor = null,
MagentoQuoteModelQuoteItemFactory $quoteItemFactory,
MagentoQuoteModelResourceModelQuoteItem $itemResourceModel
) {
parent::__construct(
$context,
$scopeConfig,
$checkoutSession,
$storeManager,
$formKeyValidator,
$cart,
$this->quoteItemFactory = $quoteItemFactory,
$this->itemResourceModel = $itemResourceModel
);
$this->quantityProcessor = $quantityProcessor ?: $this->_objectManager->get(RequestQuantityProcessor::class);
}
/**
* Empty customer's shopping cart
*
* @return void
*/
protected function _emptyShoppingCart()
{
try {
$this->cart->truncate()->save();
} catch (MagentoFrameworkExceptionLocalizedException $exception) {
$this->messageManager->addErrorMessage($exception->getMessage());
} catch (Exception $exception) {
$this->messageManager->addExceptionMessage($exception, __('We can't update the shopping cart.'));
}
}
/**
* Update customer's shopping cart
*
* @return void
*/
protected function _updateShoppingCart()
{
try {
$cartData = $this->getRequest()->getParam('cart');
$order_item_note = $this->getRequest()->getParam('order_item_note');
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
if (is_array($cartData)) {
if (!$this->cart->getCustomerSession()->getCustomerId() && $this->cart->getQuote()->getCustomerId()) {
$this->cart->getQuote()->setCustomerId(null);
}
$cartData = $this->quantityProcessor->process($cartData);
$cartData = $this->cart->suggestItemsQty($cartData);
$this->cart->updateItems($cartData)->save();
foreach($order_item_note as $key => $notes){
$itemId = $key;
$quoteItem = $this->quoteItemFactory->create();
$items = $this->itemResourceModel->load($quoteItem, $itemId);
// $quote_items = $this->cart->getQuote()->load($key);
// var_dump($items);
// exit;
$items->setData('order_item_note', $notes('notes'));
$items->save();
}
}
} catch (MagentoFrameworkExceptionLocalizedException $e) {
$this->messageManager->addErrorMessage(
$this->_objectManager->get(MagentoFrameworkEscaper::class)->escapeHtml($e->getMessage())
);
} catch (Exception $e) {
$this->messageManager->addExceptionMessage($e, __('We can't update the shopping cart.'));
$this->_objectManager->get(PsrLogLoggerInterface::class)->critical($e);
}
}
/**
* Update shopping cart data action
*
* @return MagentoFrameworkControllerResultRedirect
*/
public function execute()
{
if (!$this->_formKeyValidator->validate($this->getRequest())) {
return $this->resultRedirectFactory->create()->setPath('*/*/');
}
$updateAction = (string)$this->getRequest()->getParam('update_cart_action');
switch ($updateAction) {
case 'empty_cart':
$this->_emptyShoppingCart();
break;
case 'update_qty':
$this->_updateShoppingCart();
break;
default:
$this->_updateShoppingCart();
}
return $this->_goBack();
}
}
_updateShoppingCart is the function that I overrided.
this code returns a fatal error like below.
Fatal error: Uncaught Error: Call to undefined method MagentoQuoteModelResourceModelQuoteItem::setOrderItemNote()