<?php


namespace Magetop\Advancepreview\Plugin;


class ToOrderItemPlugin
{
    /**
     * @var \Magento\Framework\Serialize\Serializer\Json
     */
    protected $_json;
    /**
     * ToOrderItemPlugin constructor.
     * @param \Magento\Framework\Serialize\Serializer\Json $json
     */
    function __construct(
        \Magento\Framework\Serialize\Serializer\Json $json
    )
    {
        $this->_json = $json;
    }
    public function aroundConvert(\Magento\Quote\Model\Quote\Item\ToOrderItem $subject, \Closure $procede, $item, $data = []) {
        $orderItem = $procede($item, $data);
        if ($additionalOptions = $item->getOptionByCode('additional_options')) {
            $options = $orderItem->getProductOptions();
            $options['additional_options'] = $this->_json->unserialize($additionalOptions->getValue());
            $orderItem->setProductOptions($options);
        }
        return $orderItem;
    }
}
