how to check product has custom options?

落爺英雄遲暮 提交于 2019-12-07 01:43:57

问题


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.


回答1:


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
}



回答2:


use the method $product->getHasOptions()



来源:https://stackoverflow.com/questions/29445230/how-to-check-product-has-custom-options

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!