10

I am trying to get product details including product custom options value in a custom block i created.This is the code i used on product detail page and it worked there, but when trying to use it on custom block page get Options returns empty value.

if ($_product->getId()) {  
    foreach ($_product->getOptions() as $opt) {  
    echo  $optionType = $opt->getType();  
      if ($optionType == 'drop_down') {  
        $values = $opt->getValues();  
        foreach($values as $v)
        {
     $mydata = $v->getTitle();      
print_r($mydata);    
        }
      }  
    }   
}

I am new to magento so not sure what i am missing out here.
Can any one give me a hint please
Thanks

1

2 Answers 2

2

Try this:

if (Mage::registry('current_product')) {
    $product = Mage::registry('current_product');
} else {
    $product = Mage::getSingleton('catalog/product');
}


if ($product->getId()) {
    if ($product->hasCustomOptions()) {
        foreach ($product->getOptions() as $option) {
            $optionType = $option->getType();
            if ($optionType == 'drop_down') {
                $values = $option->getValues();
                foreach ($values as $k => $value) {
                    print_r($value);
                }
            }

        }
    }
}
1
  • 1
    not working... When i print the array $prodct all the information is being shown except the custom options while the same code is working fine in product view page Feb 6, 2016 at 9:43
2

The problem is that the product is not fully loaded in your block. The load() method makes sure that all information of an object is loaded. If you only use loadByAttribute() or a collection, this is not the case.

An example would be:

$product = Mage::getModel('catalog/product')->load($productId);

If you want to display multiple products, you should avoid using load() though as it has a strong negative performance impact. Sorry that I can't help you with adding options to a collection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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