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

How to get current store information in Magento 2?

You can get Current store related data like store id, store name, group id, website id and available currency code using Magento 2 by just simple code snippets.

<?php
namespace Rbj\StoreInfo\Block;

class StoreInfo extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = []
    ) {
        $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }

    /**
     * @return Store
     */
    public function getStore()
    {
        return $this->_storeManager->getStore();
    }

Call from Template file for getting the data of store,

echo $block->getStore()->getName(); // Default Store view
echo $block->getStore()->getCode(); // default
echo "<pre>";print_r($block->getStore()->getData());

Output for Current Store looks like below array,

Array
(
    [store_id] => 1
    [code] => default
    [website_id] => 1
    [group_id] => 1
    [name] => Default Store View
    [sort_order] => 0
    [is_active] => 1
    [available_currency_codes] => Array
        (
            [0] => USD
        )

)

Want to Know about Next blog, Get All Root Categories Ids in Magento 2.

How to get all Root Categories ids in Magento 2?

If your Website is setup with multiple store and each store has different Root Category assigned, In that case you need to refer below blog for getting all root category ids by programmatically.

Let’s consider your store with Two Root Category available.

First Native Magento Default Category which id is 2.
Second, create custom Root Category based on your requirement which id is 42.

Now How to get above root category id using coding in Magento 2, You can get root category id by below way,

<?php
namespace Rbj\Root\Block;
 
class RootCategoryIds extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = []
    ) {
        $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }

    /**
     * Return ids of root categories as array
     *
     * @return array
     */
    public function getRootIds()
    {
        $ids = [\Magento\Catalog\Model\Category::TREE_ROOT_ID];
        foreach ($this->_storeManager->getGroups() as $store) {
            $ids[] = $store->getRootCategoryId();
        }
        return $ids;
    }

Call from template file,

echo "<pre>";print_r($this->getRootIds());

Output as array,

Array
(
    [0] => 1
    [1] => 2
    [2] => 42
)

In above case 1 is for Root Catalog id,
2 is your Default Category id.
42 is your custom created root category id.

Let’s explore get category collection per store by,
Get Category Collection of specific store