In this Blog Post I have share How to Add Custom addOption Data on quote_item_option (Sales Flat Quote Item Option) in magento 2
Hi friends in this blog post im share with you to Add Custom addOption Data on quote_item_option (Sales Flat Quote Item Option) in magento 2
👉 Step 1 : Create a events.xml file in VendorName\ModuleName\etc\frontend\events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="checkout_cart_product_add_after">
<observer name="customPriceInCart" instance="Dww\Quote\Observer\QuoteUpdate" />
</event>
</config>
👉 Step 2 : Create a QuoteUpdate.php file in VendorName\ModuleName\Observer\QuoteUpdate.php
<?php
namespace Dww\Quote\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
class QuoteUpdate implements ObserverInterface
{
protected $uri;
protected $responseFactory;
protected $_urlinterface;
protected $logger;
protected $_checkoutSession;
public function __construct(
\Zend\Validator\Uri $uri,
\Magento\Framework\UrlInterface $urlinterface,
\Magento\Framework\App\ResponseFactory $responseFactory,
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\Session\SessionManagerInterface $session,
\Magento\Checkout\Model\Session $checkoutSession,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Serialize\SerializerInterface $serializer,
array $data = []
) {
$this->uri = $uri;
$this->_urlinterface = $urlinterface;
$this->responseFactory = $responseFactory;
$this->_request = $request;
$this->session = $session;
$this->logger = $logger;
$this->checkoutSession = $checkoutSession;
$this->serializer = $serializer;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
//$quoteItem = $observer->getEvent()->getData('quote_item');
$product = $observer->getEvent()->getData('product');
// $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');
// $logger = new \Zend_Log();
// $logger->addWriter($writer);
$customPrice = $this->_request->getParam('customPrice');
$customizedParts = $this->_request->getParam('customized_parts');
$customizedImage = $this->_request->getParam('customized_image');
$item = $observer->getEvent()->getData('quote_item');
$item = ( $item->getParentItem() ? $item->getParentItem() : $item );
$item->setCustomPrice($customPrice);
$item->setOriginalCustomPrice($customPrice);
$item->getProduct()->setIsSuperMode(true);
$item->setQty(4);
$customOption = [];
$customOption['type'] = [
'label' => 'Is Customized',
'value' => $product->getIsCustomized()
];
$customOption['data'] = [
'label' => 'Customized Part',
'value' => $customizedParts
];
$customOption['image'] = [
'label' => 'Customized Image',
'value' => $customizedImage,
];
$customOptions[] = $customOption;
$logger->info('text message');
$logger->info(print_r($customOptions, true));
$item->addOption([
'product_id' => $item->getProductId(),
'code' => 'additional_options',
'value' => $this->serializer->serialize($customOptions),
]);
$item->save();
}
}