0

If I run code to load a product and its options in a product view template file app/design/frontend/mymodule/default/template/catalog/product/view.phtml

$product = Mage::getModel("catalog/product")->load($productId);
$options = $product->getOptions();

it works as expected. However, if I run the same code for the same product in a controller of my custom module $options is an empty array.

Is there some initialization code I'm missing?

2 Answers 2

1

Try this :

 <?php

    $productID = 45

    $product = Mage::getModel('catalog/product')->load($productID);
    $options = Mage::getModel('catalog/product_option')->getProductOptionCollection($product);

    foreach ($options as $option) {
        Mage::log('Name: ' . $option->getDefaultTitle());
        Mage::log('    Type: ' . $option->getType());
        Mage::log('    Class: ' . get_class($option));
        Mage::log('    Price/Type: ' . ($option->getPrice() ? $option->getPrice() : '0.00') . ' / ' . $option->getType());

        if ($option->getType() === 'drop_down') {
            $values = Mage::getSingleton('catalog/product_option_value')->getValuesCollection($option);
            Mage::log('    Values: (name/price/type)');

            foreach ($values as $value) {
                Mage::log('        ' . $value->getTitle() . ' / ' . $value->getPrice() . ' / ' . $value->getPriceType());;
            }
        }
    }
    ?>
1

for configurable products, you can get product option like below:

$product = Mage::getModel("catalog/product")->load($productId);
$productAttributeOptions =$product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
$attributeOptions = array();
foreach ($productAttributeOptions as $productAttribute) {
    foreach ($productAttribute['values'] as $attribute) {
        $attributeOptions[$productAttribute['label']] [$attribute['value_index']] = $attribute['store_label'];
    }
}

I hope this will helpful to you.

3
  • No dice - PHP Fatal error: Call to undefined method Mage_Catalog_Model_Product_Type_Simple::getConfigurableAttributesAsArray() Commented Aug 26, 2016 at 15:03
  • above code will load the options of configurable product and as i can see you are passing simple product object..that why its giving error. Commented Aug 26, 2016 at 15:13
  • Ah, maybe revise the answer to state it works for configurable products. in my case I have simple products. Commented Aug 26, 2016 at 15:17

Your Answer

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

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