How to create Guest Empty Cart quote programmatically in Magento 2?

Magento 2, You can create a Guest or anonymous customer empty quote programmatically using Magento\Quote\Api\GuestCartManagementInterface class.

You can create quote entry using interface GuestCartManagementInterface Continue reading “How to create Guest Empty Cart quote programmatically in Magento 2?”

How to get all the Items of Order In Magento 2?

In this tutorial, We want to get all the items of the placed order.

The customer placed an order and after place order, if a developer needs to get all the items programmatically this tutorial will help them.

We can get all the items of the order by order id in Magento 2 by just simple code snippets. For,  getting all the items of Order we need to required order id. Continue reading “How to get all the Items of Order In Magento 2?”

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