Created
March 3, 2020 16:20
-
-
Save gukandrew/80e6697d3284f766d314fb48b9f7b85d to your computer and use it in GitHub Desktop.
Get selected values of custom options from Magento2 Quote
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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