TransWikia.com

Error while saving custom fields on place order

Magento Asked by Pramod on March 6, 2021

Hi am trying to save custom fields but am getting this error something went wrong

below is my custom fields

<div class="field sender name required">
    <label class="label" for="sender">
      <span><?= $block->escapeHtml(__('From')) ?></span>
    </label>
    <div class="control">
       <input type="text" name="sender" id="sender" value="" title="<?= $block->escapeHtmlAttr(__('From')) ?>" class="input-text" data-validate="{required:true}">
    </div>
</div>

<div class="field to required">
    <label class="label" for="to">
      <span><?= $block->escapeHtml(__('To')) ?></span>
    </label>
    <div class="control">
       <input type="text" name="to" id="to" value="" title="<?= $block->escapeHtmlAttr(__('To')) ?>" class="input-text" data-validate="{required:true}">
    </div>
</div>


<div class="field message required">
    <label class="label" for="message">
      <span><?= $block->escapeHtml(__('Your Message')) ?></span>
    </label>
    <div class="control">
       <input type="text" name="message" id="message" value="" title="<?= $block->escapeHtmlAttr(__('Your Message')) ?>" class="input-text" data-validate="{required:true}">
    </div>
</div>

My observer/event file

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="sales_model_service_quote_submit_before">
        <observer name="mageplaza_helloworld_sales_model_service_quote_submit_before" instance="MageplazaHelloWorldObserverSalesModelServiceQuoteSubmitBeforeObserver" />
    </event>

    <event name="checkout_cart_product_add_after">
        <observer name="mageplaza_helloworld_checkout_cart_product_add_after" instance="MageplazaHelloWorldObserverCheckoutCartProductAddAfterObserver" />
    </event>
   
</config>

CheckoutCartProductAddAfterObserver.php

<?php
namespace MageplazaHelloWorldObserver;
use MagentoFrameworkEventObserver as EventObserver;
use MagentoFrameworkEventObserverInterface;
class CheckoutCartProductAddAfterObserver implements ObserverInterface
{
   
    protected $_layout;
    protected $_storeManager;
    protected $_request;
    
    public function __construct(
        MagentoStoreModelStoreManagerInterface $storeManager,
        MagentoFrameworkViewLayoutInterface $layout,
        MagentoFrameworkAppRequestInterface $request
    )
    {
        $this->_layout = $layout;
        $this->_storeManager = $storeManager;
        $this->_request = $request;
    }
 
    public function execute(EventObserver $observer)
    {
     
        $item = $observer->getQuoteItem();
        $additionalOptions = array();
        if ($additionalOption = $item->getOptionByCode('additional_options')){
            $additionalOptions = (array) unserialize($additionalOption->getValue());
        }
        $post = $this->_request->getFullActionName() == 'checkout_cart_add';
        if(is_array($post))
        {
            foreach($post as $key => $value)
            {
                if($key == '' || $value == '')
                {
                    continue;
                }
                $additionalOptions[] = [
                    'label' => $key,
                    'value' => $value
                ];
            }
        }
        if(count($additionalOptions) > 0)
        {
            $item->addOption(array(
                'code' => 'additional_options',
                'value' => serialize($additionalOptions)
            ));

             $observer->getProduct()->addCustomOption('additional_options', serialize($additionalOptions));
        }

    }
}

SalesModelServiceQuoteSubmitBeforeObserver.php

<?php
namespace MageplazaHelloWorldObserver;
use MagentoFrameworkEventObserver as EventObserver;
use MagentoFrameworkEventObserverInterface;
class SalesModelServiceQuoteSubmitBeforeObserver implements ObserverInterface
{
    private $quoteItems = [];
    private $quote = null;
    private $order = null;
   
    public function execute(EventObserver $observer)
    {
        $this->quote = $observer->getQuote();
        $this->order = $observer->getOrder();
        foreach($this->order->getItems() as $orderItem)
        {
            if(!$orderItem->getParentItemId() && $orderItem->getProductType() == MagentoCatalogModelProductType::TYPE_SIMPLE)
            {
                if($quoteItem = $this->getQuoteItemById($orderItem->getQuoteItemId())){
                    if ($additionalOptionsQuote = $quoteItem->getOptionByCode('additional_options'))
                    {
                       
                        if($additionalOptionsOrder = $orderItem->getProductOptionByCode('additional_options'))
                        {
                            $additionalOptions = array_merge($additionalOptionsQuote, $additionalOptionsOrder);
                        }
                        else
                        {
                            $additionalOptions = $additionalOptionsQuote;
                        }
                        if(count($additionalOptions) > 0)
                        {
                            $options = $orderItem->getProductOptions();
                            $options['additional_options'] = unserialize($additionalOptions->getValue());
                            $orderItem->setProductOptions($options);
                        }
                    }
                }
            }
        }
    }
    private function getQuoteItemById($id)
    {
        if(empty($this->quoteItems))
        {
          
            foreach($this->quote->getItems() as $item)
            {
                
                if(!$item->getParentItemId() && $item->getProductType() == MagentoCatalogModelProductType::TYPE_SIMPLE)
                {
                    $this->quoteItems[$item->getId()] = $item;
                }
            }
        }
        if(array_key_exists($id, $this->quoteItems))
        {
            return $this->quoteItems[$id];
        }
        return null;
    }
}

Any solution for this .. thanks

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP