how to check product has custom options?

androidjunky picture androidjunky · Apr 4, 2015 · Viewed 7.6k times · Source

I'm trying to check whether product has custom options or not in code (my code runs sales_order_place_after event). I have try below code but it does not returning anything. $product->hasCustomOptions() and $product->hasOptions()

Please let me know what I'm missing.

Answer

Jongosi picture Jongosi · Jun 14, 2015

I've encountered this error more times than I care to count. Either $_product->hasOptions() or $_product->hasCustomOptions() always returns false. I still don't know why this error occurs.

Anyway, you can get the same result by doing the following. For configurable products:

<?php if ( $_product->getData('has_options') ): ?>
    <!-- do something -->
<?php endif; ?>

And to get the same result for simple products with custom options:

<?php if ( $_product->getData('has_options') && ($_product->getTypeID() == 'simple') ): ?>
    <!-- do something -->
<?php endif; ?>

I hope that helps a future adventurer!


EDIT


The solution above does not work in loops when the flat category data option is enabled in Magento, and we don't want to reload the product inside the foreach loop!!

Instead, we can check for custom options using the following singleton inside the loop:

$opts = Mage::getSingleton('catalog/product_option')->getProductOptionCollection($_product);
$optsSize = $opts->getSize();

if ( $optsSize ) {
    ... // go go go
}