如何检查产品是否具有自定义选项

如何检查产品是否具有自定义选项

本文介绍了如何检查产品是否具有自定义选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查产品是否在代码中具有自定义选项(我的代码运行sales_order_place_after事件).我有下面的代码尝试,但它不返回任何东西.$product->hasCustomOptions()$product->hasOptions()

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.

推荐答案

我遇到此错误的次数比我计算的次数还多. $_product->hasOptions()$_product->hasCustomOptions()总是返回false.我仍然不知道为什么会发生此错误.

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!

编辑

在Magento中启用 flat category 数据选项时,上述解决方案无法循环使用,并且我们不想在foreach循环内重新加载产品!

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
}

这篇关于如何检查产品是否具有自定义选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 23:09