Magento – How to get all items with custom options in magento current cart

cartcustom-optionsproduct-attributeshopping-cart

Get cart visible products with custom options.
I have done with out custom options.

    $cart = Mage::getModel('checkout/cart')->getQuote();
           //$cart->getAllItems() //for all cart items
                foreach ($cart->getAllVisibleItems() as $item) {            
                    $productName = $item->getProduct()->getName();
                    $productPrice = $item->getProduct()->getPrice();                  
                    /*
                    //OR
                    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 />';
                    */
                   //get custom options        
                }

How can get custom options from this

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

Best Answer

Try this below code

<?php
$cart = Mage::getModel('checkout/cart')->getQuote();
foreach ($cart->getAllVisibleItems() as $item) { 
  $options = Mage::helper('catalog/product_configuration')->getCustomOptions($item);
   print_r($options);

}
?>

OR

<?php
$product = $item->getProduct();
$options = array();
$optionIds = $item->getOptionByCode('option_ids');
if ($optionIds) {
    $options = array();
    foreach (explode(',', $optionIds->getValue()) as $optionId) {
        $option = $product->getOptionById($optionId);
        if ($option) {
            $itemOption = $item->getOptionByCode('option_' . $option->getId());
            $group = $option->groupFactory($option->getType())
                ->setOption($option)
                ->setConfigurationItem($item)
                ->setConfigurationItemOption($itemOption);

            if ('file' == $option->getType()) {
                $downloadParams = $item->getFileDownloadParams();
                if ($downloadParams) {
                    $url = $downloadParams->getUrl();
                    if ($url) {
                        $group->setCustomOptionDownloadUrl($url);
                    }
                    $urlParams = $downloadParams->getUrlParams();
                    if ($urlParams) {
                        $group->setCustomOptionUrlParams($urlParams);
                    }
                }
            }

            $options[] = array(
                'label' => $option->getTitle(),
                'value' => $group->getFormattedOptionValue($itemOption->getValue()),
                'print_value' => $group->getPrintableOptionValue($itemOption->getValue()),
                'option_id' => $option->getId(),
                'option_type' => $option->getType(),
                'custom_view' => $group->isCustomizedView()
            );
        }
    }
}

$addOptions = $item->getOptionByCode('additional_options');
if ($addOptions) {
    $options = array_merge($options, unserialize($addOptions->getValue()));
}
print_r($options);
?>