Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check product has custom options?

Tags:

magento-1.8

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.

like image 884
androidjunky Avatar asked Apr 04 '15 10:04

androidjunky


People also ask

What is the use of custom option?

Adding customizable options to a product is an easy way to offer a selection of options with a variety of text, selection, and date input types. Customizable options are a good solution if your inventory needs are simple.

What is a custom option?

a a practice which by long-established usage has come to have the force of law. b such practices collectively (esp. in the phrase custom and practice) 4 habitual patronage, esp. of a shop or business.


2 Answers

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
}
like image 184
Jongosi Avatar answered Jan 22 '23 03:01

Jongosi


use the method $product->getHasOptions()

like image 37
Alex Moo Avatar answered Jan 22 '23 04:01

Alex Moo