Magento产品属性获取值

82

如果我知道产品ID,而不想加载整个产品,如何获取特定的产品属性值?


1
不是很好的答案,但你可以创建一个直接查询适当表格的模型 :) - Joe Mastey
13个回答

142
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);

1
如何获取选项文本?有人知道吗? - Dreaded semicolon
2
对于那些关心的人:此方法仅在1.6+版本中可用。 - JMTyler
要从中获取选项文本,请参见下面的答案:https://dev59.com/u2w05IYBdhLWcg3w9mZW#30519730 - Mukesh Chapagain

41
我知道的一种方式:
$product->getResource()->getAttribute($attribute_code)
        ->getFrontend()->getValue($product)

4
我对Magento还不熟悉,为什么要使用“$product”两次? - Mr_Green
29
因为这是Magento,没有任何“为什么”能得到合理的答案。 - nakajuice
@Mr_Green,这并不是说要使用两次:第二行是第一行的延续。您正在对getAttribute()返回的对象调用getFrontend()。 - Scott Buchanan

27

你可以使用

<?php echo $product->getAttributeText('attr_id')  ?> 

2
这段代码运行得很好。我搜索了许多博客,但除了这个之外,没有一个代码能够为我工作。真的很棒。 - Dixit Patel
3
此代码仅在产品模型包含属性值时有效,当您仅知道产品ID时,它将无法工作。 - Jiří Chmiel
1
这适用于下拉列表等属性,但如果属性是简单的文本字段,则需要不同的函数。假设属性是my_name,那么代码变为$product->getMyName()。 - Ken
1
@Ken,如果你需要从产品中获取一个动态属性,那么你可以使用 $product->getData('my_name') - pavlindrom
我们如何获取选项ID呢?$product->getAttributeId('attr_id')??? - snh_nl

10

请查看Daniel Kocherga的答案,因为它在大多数情况下都可以对您有用。

除了使用该方法获取属性值之外,有时您可能还想获取 selectmultiselect 的标签。在这种情况下,我创建了这个方法,并将其存储在一个帮助类中:

/**
 * @param int $entityId
 * @param int|string|array $attribute atrribute's ids or codes
 * @param null|int|Mage_Core_Model_Store $store
 *
 * @return bool|null|string
 * @throws Mage_Core_Exception
 */
public function getAttributeRawLabel($entityId, $attribute, $store=null) {
    if (!$store) {
        $store = Mage::app()->getStore();
    }

    $value = (string)Mage::getResourceModel('catalog/product')->getAttributeRawValue($entityId, $attribute, $store);
    if (!empty($value)) {
        return Mage::getModel('catalog/product')->getResource()->getAttribute($attribute)->getSource()->getOptionText($value);
    }

    return null;
}

8
似乎在不加载产品模型的情况下无法获取值。如果您查看文件app/code/core/Mage/Eav/Model/Entity/Attribute/Frontend/Abstract.php,您将看到该方法。
public function getValue(Varien_Object $object)
{
    $value = $object->getData($this->getAttribute()->getAttributeCode());
    if (in_array($this->getConfigField('input'), array('select','boolean'))) {
        $valueOption = $this->getOption($value);
        if (!$valueOption) {
            $opt = new Mage_Eav_Model_Entity_Attribute_Source_Boolean();
            if ($options = $opt->getAllOptions()) {
                foreach ($options as $option) {
                    if ($option['value'] == $value) {
                        $valueOption = $option['label'];
                    }
                }
            }
        }
        $value = $valueOption;
    }
    elseif ($this->getConfigField('input')=='multiselect') {
        $value = $this->getOption($value);
        if (is_array($value)) {
            $value = implode(', ', $value);
        }
    }
    return $value;
}

正如您所看到的,该方法需要已加载的对象以从中获取数据(第三行)。


5

首先,我们必须确保所需的属性已被加载,然后输出它。使用以下代码:

$product = Mage::getModel('catalog/product')->load('<product_id>', array('<attribute_code>'));
$attributeValue = $product->getResource()->getAttribute('<attribute_code>')->getFrontend()->getValue($product);

4
尝试这个。
 $attribute = $_product->getResource()->getAttribute('custom_attribute_code');
    if ($attribute)
    {
        echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
    }

2

您不必加载整个产品。 Magento的集合非常强大且智能。

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('entity_id', $product->getId());
$collection->addAttributeToSelect('manufacturer');
$product = $collection->getFirstItem();
$manufacturer = $product->getAttributeText('manufacturer');

当您调用getFirstItem()时,查询将被执行,并且结果产品非常简单:

[status] => 1
[entity_id] => 38901
[type_id] => configurable
[attribute_set_id] => 9
[manufacturer] => 492
[manufacturer_value] => JETTE
[is_salable] => 1
[stock_item (Varien_Object)] => Array
    (
        [is_in_stock] => 1
    )

1
这个可以工作 -
echo $_product->getData('ATTRIBUTE_NAME_HERE');

2
问题是“不加载整个产品”。 - Denis Óbukhov

0

您可以通过以下方式获取属性值

$model = Mage::getResourceModel('catalog/product');
$attribute_value = $model->getAttributeRawValue($productId, 'attribute_code', $storeId);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接