2

在 Magento 中检索属性值有不同的方法,

$options=$_product->getAttributeText('some_attribute')

$options=$_product->getResource()->getAttribute('some_attribute')->getFrontend()->getValue($_product)

$options=$_product->getSomeAttribute()

上述方法有什么不同,哪一种是检索属性值的正确方法?

4

2 回答 2

4
echo $_product->getSomeAttribute()

将获取带有属性的值作为文本值或文本区域值等。

echo $_product->getAttributeText('some_attribute')

将获得一个or属性类型的所有选项的数组。Drop DownMultiple Select

$attributes = $_product->getAttributes();
$someAttr = $attributes['some_attribute']->getFrontend()->getValue($_product);

将为您提供任何类型的属性的,甚至是Drop Down or Multiple Select属性类型的值。

于 2012-08-21T16:25:30.350 回答
0

使用 getAttributetext 您将直接在属性数组上获取属性。当您使用 get 方法时,您将通过“正确”的方式获得产品,因为 get 和 set 可以根据 magento 规则来衡量日期。并使用 getFrontend 您将直接从数据库中获取它。

没有正确的方法可以做到这一点,但有低成本的解决方案,我认为使用 get 方法和 getAttributeText 是最好的方法。

于 2012-08-21T11:18:44.573 回答