How to get Order Item Selected Options in Magento 2?

We can get Product selected options in Magento 2 by just simple code snippet, We can get selected options of Bundle, Configurable and Grouped product by below code snippet.

Customer purchased the configurable product with different variant and if you need to get selected variant of Product using Programmatically, Below code is useful to get Configurable and Bundle product selected options.

First, you need to get Item Object from a specific order.
Call below function in PHP file,

<?php
namespace Rbj\Sales\Block;

class Items extends \Magento\Framework\View\Element\Template
{
	/*
     * get Configurable/Bundle selected options from Item object
     */
    public function getSelectedOptions($item){
    	$result = [];
        $options = $item->getProductOptions();
        if ($options) {
            if (isset($options['options'])) {
                $result = array_merge($result, $options['options']);
            }
            if (isset($options['additional_options'])) {
                $result = array_merge($result, $options['additional_options']);
            }
            if (isset($options['attributes_info'])) {
                $result = array_merge($result, $options['attributes_info']);
            }
        }
        return $result;
    }

In template or PHP file, You have already Order or Quote object for getting all the items of Quote or Order. You can get Order Object by Get Order data by order id

<?php
$order = LOAD_ORDER_OBJECT; // load order or quote object
foreach ($order->getAllVisibleItems() as $_item) {
	if($_options = $this->getSelectedOptions($_item)) {
        $options .= '<dt class="options">';
            foreach ($_options as $_option) : 
                $options .= '<dd>'.$_option['label'].'</dd><dd>'.$_option['value'].'</dd>';
            endforeach; 
        $options .= '</dt>';
    }
}

The output will look like,

configurable product selected options
configurable product selected options

A Complete List of all events in Magento 2

Magento 2 Complete guide to the list of available events that will be used in daily routine as a Magento developer to trigger event-specific tasks at a time of Development.

You can use any required events from the Core module to accomplish your requirements. Define events in the events.xml file, Create an observer.php file to do specific actions for an event.

You can also create your own custom event to accomplish your task by link, Create Custom Events. Continue reading “A Complete List of all events in Magento 2”

Different types of Cancel events for Orders in Magento 2

There are many events are available in Native Magento 2 but some of the difficult events of cancel action of payment, invoice or order are as below,

1. order_cancel_after

This event is used after order canceled. If you want to do something after an order is canceled you can use this event.
Check file, vendor/magento/module-sales/Model/Order.php at cancel() function.

$this->_eventManager->dispatch('order_cancel_after', ['order' => $this]);

2. sales_order_invoice_cancel

Allows you to cancel the order invoice when you canceled the order.
Check event called at, file, vendor/magento/module-sales/Model/Order/Invoice.php at cancel() function.

$this->_eventManager->dispatch('sales_order_invoice_cancel', [$this->_eventObject => $this]);

3. sales_order_payment_cancel_invoice

Allows you to cancel the order invoice when you canceled the order.
Check event called at, file, vendor/magento/module-sales/Model/Order/Invoice.php at cancel() function.

$this->_eventManager->dispatch('sales_order_invoice_cancel', [$this->_eventObject => $this]);

4. sales_order_payment_cancel_creditmemo

Event defined under vendor/magento/module-sales/Model/Order/Payment.php using cancelCreditmemo() function but this function is not called at any file. Might be used in future developments. This event contains payment and specified creditmemo object.

$this->_eventManager->dispatch(
            'sales_order_payment_cancel_creditmemo',
            ['payment' => $this, 'creditmemo' => $creditmemo]
        );

5.sales_order_payment_cancel_invoice

Cancel specified invoice from order and update self-total from order invoice.
Event defined under vendor/magento/module-sales/Model/Order/Payment.php

$this->_eventManager->dispatch(
            'sales_order_payment_cancel_invoice',
            ['payment' => $this, 'invoice' => $invoice]
        );

This event contains payment and invoice object.

6. sales_order_creditmemo_cancel

When you want to refund from a specific order or specific item from order,
Your best suitable event is, sales_order_creditmemo_cancel

$this->eventManager->dispatch('sales_order_creditmemo_cancel', ['creditmemo' => $creditmemo]);