Webkul
Services
Technologies Bagisto QloApps
iOS Android SAP BigCommerce Wix
Trusted by
Samsung Intel Toyota Walmart Disney Nokia Nestlé Decathlon
See all customers
Back to Top
DOC

How To Get Customoptions From Quote and Order In Magento 2

How To Get Customoptions From Quote and Order In Magento 2 : Here i am going to let you know. how you can get custom option from quote and order.
i will describe both separately.
1 => get custom option from quote :

/**
 * @var \Magento\Checkout\Model\Cart
 */
protected $_cartModel;
 
/**
 * @param Context                             $context
 * @param \Magento\Checkout\Model\Cart        $cartModel
 */
public function __construct(
    Context $context,
    \Magento\Checkout\Model\Cart $cartModel,
) {
    $this->_cartModel = $cartModel;
    parent::__construct($context);
}

public function execute()
{
    // _cartModel is an instance of \Magento\Checkout\Model\Cart
    $quote = $this->_cartModel->getQuote();
    $cartAllItems = $quote->getAllItems();
    foreach ($cartAllItems as $item) {
        $options = $item->getProduct()->getTypeInstance(true)
        ->getOrderOptions($item->getProduct());
        foreach ($options as $option) {
            $itemOptions = $option;
            //itemOptions contain all the custom option of an item
        }
    }
}

here _cartModel is an instance of \Magento\Checkout\Model\Cart .
itemOptions contains custom options of item.

2 => get custom option from order :

<?php
/**
 * @var \Magento\Checkout\Model\Cart
 */
protected $_cartModel;
 
/**
 * @param Context                             $context
 * @param \Magento\Sales\Model\Order          $order
 */
public function __construct(
    Context $context,
    \Magento\Sales\Model\Order $order,
) {
    $this->_order = $order;
    parent::__construct($context);
}

public function execute()
{
    // _order is an instance of \Magento\Sales\Model\Order
    $order = $this->_order->load('order_id');
    $orderAllItems = $order->getAllItems();
    foreach ($orderAllItems as $item) {  
        $options = $item->getProductOptions();
        foreach ($options as $option) {
            // $option contains custom option of an item
        }
    }
}

here _order is an instance of \Magento\Sales\Model\Order
Hope so it will help you.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home