Plugin- set additonal options

pabodah

Paboda Hettiarachchi

Posted on June 12, 2023

Plugin- set additonal options
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Quote\Model\Quote\Item\ToOrderItem">
        <plugin name="unique_name" type="Vendor\ModuleName\Plugin\ToOrderItem" sortOrder="1" />
    </type>
</config>
Enter fullscreen mode Exit fullscreen mode
<?php
namespace Vendor\ModuleName\Plugin;
use Magento\Quote\Model\Quote\Item\ToOrderItem as QuoteToOrderItem;
class ToOrderItem
{
    /**
     * aroundConvert
     *
     * @param QuoteToOrderItem $subject
     * @param \Closure $proceed
     * @param \Magento\Quote\Model\Quote\Item $item
     * @param array $data
     *
     * @return \Magento\Sales\Model\Order\Item
     */
    public function aroundConvert(
        QuoteToOrderItem $subject,
        \Closure $proceed,
        $item,
        $data = []
    ) {
        // Get Order Item
        $orderItem = $proceed($item, $data);
        // Get Quote Item's additional Options
        $additionalOptions = $item->getOptionByCode('additional_options');
        // Check if there is any additional options in Quote Item
        if ($additionalOptions->getValue()) {
            // Get Order Item's other options
            $options = $orderItem->getProductOptions();
            // Set additional options to Order Item
            $options['additional_options'] = json_decode($additionalOptions->getValue());
            $orderItem->setProductOptions($options);
        }
        return $orderItem;
    }
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
pabodah
Paboda Hettiarachchi

Posted on June 12, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related