1

I created an observer on checkout_onepage_controller_success_action event, I want to get selected custom options values for this particular order. How can I do this please help? Please check the following code:

public function execute(Observer $observer) {

    $order_id = $observer->getEvent()->getOrderIds()[0];

    // \Magento\Sales\Api\OrderRepositoryInterface
    $order = $this->_orderRepositoryInterface->get($order_id);

    $order_items = $order->getItems();

    foreach ($order_items as $order_item) {
        //Here I want to get selected custom options values for each item
        //With other fields like product name, price and other product related data
    }
}
2

Try below code:

<?php
/**
 * User: amit-pc
 * Date: 28/12/18
 */

namespace Stackexchange\Magento\Observer;


use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\OrderRepository;


class SuccessPage implements ObserverInterface
{

    /**
     * @var OrderInterface
     */
    private $order;
    /**
     * @var OrderRepositoryInterface
     */
    private $orderRepository;
    /**
     * @var \Magento\Catalog\Model\Product\OptionFactory
     */
    private $optionFactory;

    public function __construct(
        OrderRepositoryInterface $orderRepository,
        \Magento\Catalog\Model\Product\OptionFactory $optionFactory
    )
    {

        $this->orderRepository = $orderRepository;
        $this->optionFactory = $optionFactory;
    }

    /**
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {

        $orderIds = $observer->getEvent()->getOrderIds();

        if(isset($orderIds[0]))
        {
            $order = $this->orderRepository->get($orderIds[0]);
            $items = $order->getItems();

            foreach ($items as $item)
            {
                if ($options = $item->getProductOptions()) {
                    if (isset($options['options'])) {

                        foreach ($options['options'] as $_option){
                            // got option as index => option label and  value = option value
                            $optionsAssview = $this->getCustomOption($_option);
                        }
                    }
                }
                //$item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
            }
        }
    }

    /**
     * get Option
     * @param $_option
     * @return array
     */
    private function getCustomOption($_option)
    {
        $result = [];
        $result['label'] = $_option['label'];
        if (isset($_option['custom_view']) && $_option['custom_view'])
        {
            $result['value'] =  $this->getCustomizedOptionValue($_option);
        }else{
            $result['value'] =$_option['value'];
        }
        return $result;
    }
    private function getCustomizedOptionValue($optionInfo)
    {
        // render customized option view
        $_default = $optionInfo['value'];
        if (isset($optionInfo['option_type'])) {
            try {
                $group = $this->optionFactory->create()->groupFactory($optionInfo['option_type']);
                return $group->getCustomizedView($optionInfo);
            } catch (\Exception $e) {
                return $_default;
            }
        }
        return $_default;
    }

}

NOte:$optionsAssview variable has selected custom options values and label

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.