If you want to know how to do something, it's always best to look at how Magento does it. So if you take a look at the cart templates, you'll notice that the template responsible for rendering a default product (module-checkout/view/frontend/templates/cart/item/default.phtml
) uses a method getOptionList()
to get the options:
<?php if ($_options = $block->getOptionList()):?>
... etc.
Now take a look at this method (Magento\Checkout\Block\Cart\Item\Renderer::getOptionList()
):
public function getOptionList()
{
return $this->getProductOptions();
}
And Magento\Checkout\Block\Cart\Item\Renderer::getProductOptions()
will do the following:
public function getProductOptions()
{
/* @var $helper \Magento\Catalog\Helper\Product\Configuration */
$helper = $this->_productConfig;
return $helper->getCustomOptions($this->getItem());
}
Now this is interesting, because here we see the use of a helper that does exactly what your question is: get the custom options of a quote item. And it's a helper, so you can use dependency injection to use it in your own code.
A simple example:
class Example
{
/**
* @var \Magento\Catalog\Helper\Product\Configuration
*/
protected $configurationHelper;
/**
* Example constructor.
* @param \Magento\Catalog\Helper\Product\Configuration $configurationHelper
*/
public function __construct(
\Magento\Catalog\Helper\Product\Configuration $configurationHelper
)
{
$this->configurationHelper = $configurationHelper;
}
/**
* @param \Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item
* @return array
*/
public function getSelectedOptionsOfQuoteItem(\Magento\Catalog\Model\Product\Configuration\Item\ItemInterface $item)
{
return $this->configurationHelper->getCustomOptions($item);
}
}
This should solve your problem.
Using Magento 2.1.6
Magento\Checkout\Model\Session
u will get itmagento\vendor\magento\module-checkout\view\frontend\templates\cart\form.phtml