Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gukandrew/80e6697d3284f766d314fb48b9f7b85d to your computer and use it in GitHub Desktop.
Save gukandrew/80e6697d3284f766d314fb48b9f7b85d to your computer and use it in GitHub Desktop.
Get selected values of custom options from Magento2 Quote
<?php
$quoteId = 1;
$ObjectManager = \Magento\Framework\App\ObjectManager::getInstance();
$quoteCollectionFactory = $ObjectManager->create('\Magento\Quote\Model\ResourceModel\Quote\CollectionFactory');
$QuoteRepository = $ObjectManager->create('\Magento\Quote\Model\ResourceModel\Quote\CollectionFactory');
$quote = $quoteCollectionFactory->create()
->addFieldToFilter('quote_id', $quoteId)
->getFirstItem();
$rr = [];
foreach ($quote->getItemsCollection() as $item) {
if ($optionIds = $item->getOptionByCode('option_ids')) {
foreach (explode(',', $optionIds->getValue()) as $optionId) {
$optionOriginal = $item->getProduct()->getOptionById($optionId);
$optionSelected = $item->getOptionByCode('option_' . $optionId);
$selectedValueId = $optionSelected->getValue();
$selectedValue = $optionOriginal->getValueById($selectedValueId);
$rr[] = [
$optionOriginal->getData(),
$selectedValue->getData(),
$optionSelected->getData()
];
}
}
}
return $rr;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment