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.
Categories:
Magento2
Tags:
custom option from order in magento 2 custom option from quote in magento 2 How To Get Customoptions From Quote and Order In Magento 2 magento 2 custom option from order magento 2 order option magento 2 quote custom option magento 2 quote option
View Comments
Comment or Ask a Question
Quick Links