4

I have some Vitrual product with options, I need to get his options but I can just get the info_buyRequest instead of additional_options

Here is my code:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$cart = $objectManager->get('\Magento\Checkout\Model\Cart');

//This doesn't work
$itemsCollection = $cart->getQuote()->getItemsCollection();

//This doesn't work too
$itemsVisible = $cart->getQuote()->getAllVisibleItems();

//This return something
$items = $cart->getQuote()->getAllItems();

foreach($items as $item) {
    echo 'ID: '.$item->getProductId().'<br />';
    echo 'Name: '.$item->getName().'<br />';
    echo 'Sku: '.$item->getSku().'<br />';
    echo 'Quantity: '.$item->getQty().'<br />';
    echo 'Price: '.$item->getPrice().'<br />';
    echo "<br />";
}

//Here I try to get the product options but no way

$items = $cart->getQuote()->getAllItems();

foreach ($items as $item) {
    $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
    $customOptions = $options['options']; // error here, line 44
    if (!empty($customOptions)) {
        foreach ($customOptions as $option) {
            $optionTitle = $option['label'];
            $optionId = $option['option_id'];
            $optionType = $option['type'];
            $optionValue = $option['value'];
        }
    }
}

Error:

Exception #0 (Exception): Notice: Undefined index: options in /app/code/Vendor/Module/view/frontend/templates/file.phtml on line 44

enter image description here

quote_item_option table > additional_options code

  • try with $item->getProduct()->getCustomOption('info_buyRequest'); – Keyur Shah Apr 10 '18 at 9:00
  • I replaced $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct()); with $item->getProduct()->getCustomOption('info_buyRequest'); but it doesn't work, he breaks the page – PЯINCƏ Apr 10 '18 at 9:07
1

You can use

$options = $item->getBuyRequest();

Instead of

$options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());

  • It doesn't work, I get the product id, sku, qty the info_buyRequest data, but not the options ! – PЯINCƏ Apr 10 '18 at 9:37
1

Please use below to get additional_options

$options = $item->getOptionByCode('additional_options');
  • It doesn't work and this code breaks the page (white page) – PЯINCƏ Apr 10 '18 at 20:56
  • If it's showing white page then check log and see what is causing – Nalin Savaliya Apr 11 '18 at 3:34
  • Yes, I know but I have nothing. As you see the error in my question I can display the Exceptions but with your solution no ! – PЯINCƏ Apr 11 '18 at 7:46
0

Because this wasn't answered the above solutions are missing the call to get the options.

$item->getBuyRequest()->getAdditionalOptions()
0

You can use it is work for me,

$additionalOptions = $item->getOptionByCode('additional_options')
echo $additionalOptions->getValue();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.