How to Add Custom Product Option Data on quote_item_option in magento - Magento Source24

 

How to Add Custom Product Option Data on quote_item_option in magento - Magento Source24

In this Blog Post i'm Share with to How to Add Custom Product Option Data on quote_item_option in magento

👉 this Custom Product Option show on mini cart , cart, checkout, sales order view, invoice new, invoice view and invoice pdf 


👉 Step 1: Create a events.xml file in VendorName/ModuleName/etc/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="mageget_checkout_cart_product_add_after"

                  instance="Dww\Quote\Observer\CheckoutCartAdd"/>

    </event>

    <event name="sales_model_service_quote_submit_before">

        <observer name="mageget_add" instance="Dww\Quote\Observer\AddOptionToOrder"/>

    </event>

</config>


👉 Step2 : Create a CheckoutCartAdd.php file in VendorName/ModuleName/Observer/CheckoutCartAdd.php


<?php


namespace Dww\Quote\Observer;


use Magento\Framework\Event\Observer as EventObserver;

use Magento\Framework\Event\ObserverInterface;

use Magento\Store\Model\StoreManagerInterface;

use Magento\Framework\View\LayoutInterface;

use Magento\Framework\App\RequestInterface;

use Magento\Framework\Serialize\SerializerInterface;


class CheckoutCartAdd implements ObserverInterface

{

    protected $layout;

    protected $storeManager;

    protected $request;

    private $serializer;


    public function __construct(

        StoreManagerInterface $storeManager,

        LayoutInterface       $layout,

        RequestInterface      $request,

        \Magento\Framework\Serialize\Serializer\Json $json,

        SerializerInterface   $serializer

    )

    {

        $this->layout = $layout;

        $this->storeManager = $storeManager;

        $this->request = $request;

        $this->json = $json;

        $this->serializer = $serializer;

    }


    public function execute(EventObserver $observer)

    {

        $item = $observer->getQuoteItem();

        $post = $this->request->getPost();

        $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');

        $logger = new \Zend_Log();

        $logger->addWriter($writer);

      


        $customedata = array();

        if ($additionalOption = $item->getOptionByCode('additional_options')) {

            $customedata = $this->serializer->unserialize($additionalOption->getValue());

        }

        $part= '{"face": "#FFF333", "Accent": "#f00"}';

        $color = "";

        $jsonDecode = $this->json->unserialize($part);


        $customedata[] = [

            'label' => 'Customized Parts',

            'value' => $part,

        ];


        if (count($customedata) > 0) {

            $item->addOption(array(

                'product_id' => $item->getProductId(),

                'code' => 'additional_options',

                'value' => $this->serializer->serialize($customedata)

            ));

        }

    }

}


👉 Step3: Create a AddOptionToOrder.php file in VendorName/ModuleName/Observer/AddOptionToOrder.php


<?php


namespace Dww\Quote\Observer;


use Magento\Framework\Event\ObserverInterface;

use Magento\Framework\Serialize\SerializerInterface;


class AddOptionToOrder implements ObserverInterface

{

    private $serializer;


    public function __construct(

        SerializerInterface $serializer

    ){

        $this->serializer = $serializer;

    }


    public function execute(\Magento\Framework\Event\Observer $observer)

    {

       

        $writer = new \Zend_Log_Writer_Stream(BP . '/var/log/custom.log');

        $logger = new \Zend_Log();

        $logger->addWriter($writer);


            $quote = $observer->getQuote();

            $order = $observer->getOrder();

            foreach ($quote->getAllVisibleItems() as $quoteItem) {

                $quoteItems[$quoteItem->getId()] = $quoteItem;

            }


            foreach ($order->getAllVisibleItems() as $orderItem) {

                $quoteItemId = $orderItem->getQuoteItemId();

                $quoteItem = $quoteItems[$quoteItemId];

                $additionalOptions = $quoteItem->getOptionByCode('additional_options');

                $logger->info(print_r($additionalOptions->getValue(), true)); //To print array log

                if (count(array($additionalOptions)) > 0) {

                    $options = $orderItem->getProductOptions();

                    $options['additional_options'] = $this->serializer->unserialize($additionalOptions->getValue());

                    $orderItem->setProductOptions($options);

                }

            }

            return $this;

    }

}






Deepak Kumar Bind

Success is peace of mind, which is a direct result of self-satisfaction in knowing you made the effort to become the best of which you are capable.

Post a Comment

If you liked this post please do not forget to leave a comment. Thanks

Previous Post Next Post