2

I'm using following code to set additional options for configurable product in magento 2

<?php
namespace Company\ModuleName\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\Observer as EventObserver;

class AdditionalOptions implements ObserverInterface
{

    /**
     * @var \Magento\Framework\View\LayoutInterface
     */
    protected $_layout;
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $_storeManager;
    protected $_request;
    /**
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\View\LayoutInterface $layout
     */
    public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\View\LayoutInterface $layout,
        \Magento\Framework\App\RequestInterface $request
    )
    {
        $this->_layout = $layout;
        $this->_storeManager = $storeManager;
        $this->_request = $request;
    }
    /**
     *
     * @param EventObserver $observer
     * @return void
     */
    public function execute(EventObserver $observer)
    {
        /* @var \Magento\Quote\Model\Quote\Item $item */
        $item = $observer->getQuoteItem();

        $additionalOptions = array();
        if ($additionalOption = $item->getOptionByCode('additional_options')){
            $additionalOptions = (array) unserialize($additionalOption->getValue());
        }
        $post = $this->_request->getParam('custom_options');
        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(
                'product_id' => $item->getProductId(),
                'code' => 'additional_options',
                'value' => serialize($additionalOptions)
            ));
        }
    }
}    

Above code works when customer is not logged in and I get following result.

enter image description here

Additional Option labelled Testing Option is shown in the image above.

But, when the customer is logged in, and when I land on checkout/cart page, the additional option is rendered on the screen just like above image, looking into the tables, I see a record with additional_options in quote_item_option table as shown below

enter image description here

but one function is called after rendering the checkout/cart page named loadCustomerQuote() in vendor/magento/module-checkout/Model/Session.php This function deletes all the entries of that quote id from quote_item and quote_item_option table and clone them with new ids.

New records are inserted after the function is called as shown below.

enter image description here

The record with code additional_option is not cloned or added after deletion just like rest of the records. Can anyone help me out why this happens for only configurable product while customer is logged in? Above code works for simple product with and without customer logged in.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.