0

I have a stock product with some custom options. I would like to fetch the custom options for this product in the view.phtml of my rwd-based theme.

Now I thought this would be possible through

$myoptions = $_product->getOptions();
print_r($myoptions);

But instead of giving me back the options, the page loads for a long time and then outputs lots of unrelated stuff. At least it looks like this.

Is this the right way to fetch the custom options of a product? Is there another (better) way to do it?

1 Answer 1

1

The code will be somewhat like:-

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

/**
 * In Magento Models or database schema level, the product's Custom Options are
 * executed & maintained as only "options". So, when checking whether any product has
 * Custom Options or not, we should check by using this method "hasOptions()" only.
 */
if($product->hasOptions()) {
    echo '<pre>';

    foreach ($product->getOptions() as $o) {
        $optionType = $o->getType();
        echo 'Type = '.$optionType;

        if ($optionType == 'drop_down') {
            $values = $o->getValues();

            foreach ($values as $k => $v) {
                print_r($v);
            }
        }
        else {
            print_r($o);
        }
    }

    echo '</pre>';
}
1
  • For some reason this does not work. The product page is showing the custom options just fine with its standard behaviour, but has_options() does not return a result, just like there are none.
    – flomei
    Commented Jul 16, 2018 at 14:38

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.