【问题标题】:how to check product has custom options?如何检查产品是否有自定义选项?
【发布时间】:2015-06-09 07:31:21
【问题描述】:

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

请让我知道我缺少什么。

【问题讨论】:

标签: magento-1.8


【解决方案1】:

我遇到这个错误的次数比我想的要多。 $_product->hasOptions()$_product->hasCustomOptions() 总是返回 false。我还是不知道为什么会出现这个错误。

无论如何,您可以通过执行以下操作获得相同的结果。对于可配置产品:

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

对于具有自定义选项的简单产品,要获得相同的结果:

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

希望对未来的冒险者有所帮助!


编辑


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

相反,我们可以在循环中使用以下单例检查自定义选项:

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

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

【讨论】:

  • 但为什么 $_product->hasOptions() 不起作用?,这似乎是自然的呼吁
【解决方案2】:

使用方法$product-&gt;getHasOptions()

【讨论】:

    猜你喜欢
    • 2015-06-04
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 2021-12-04
    • 1970-01-01
    • 2014-11-14
    相关资源
    最近更新 更多