Magento 产品属性获取值

发布于 2024-11-27 19:12:16 字数 38 浏览 1 评论 0原文

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

How to get specific product attribute value if i know product ID without loading whole product?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

感性 2024-12-04 19:12:16
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);
懒的傷心 2024-12-04 19:12:16

我知道的一种方式:

$product->getResource()->getAttribute($attribute_code)
        ->getFrontend()->getValue($product)

A way that I know of:

$product->getResource()->getAttribute($attribute_code)
        ->getFrontend()->getValue($product)
柳若烟 2024-12-04 19:12:16

你可以使用

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

you can use

<?php echo $product->getAttributeText('attr_id')  ?> 
灯角 2024-12-04 19:12:16

请参阅 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;
}

Please see Daniel Kocherga's answer, as it'll work for you in most cases.

In addition to that method to get the attribute's value, you may sometimes want to get the label of a select or multiselect. In that case, I have created this method which I store in a helper class:

/**
 * @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;
}
迷爱 2024-12-04 19:12:16

如果不加载产品模型,似乎不可能获得价值。如果您查看文件 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;
}

如您所见,该方法需要加载对象才能从中获取数据(第三个线)。

It seems impossible to get value without loading product model. If you take a look at file app/code/core/Mage/Eav/Model/Entity/Attribute/Frontend/Abstract.php you'll see the method

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;
}

As you can see this method requires loaded object to get data from it (3rd line).

中性美 2024-12-04 19:12:16

首先我们必须确保所需的属性已加载,然后将其输出。使用这个:

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

First we must ensure that the desired attribute is loaded, and then output it. Use this:

$product = Mage::getModel('catalog/product')->load('<product_id>', array('<attribute_code>'));
$attributeValue = $product->getResource()->getAttribute('<attribute_code>')->getFrontend()->getValue($product);
丢了幸福的猪 2024-12-04 19:12:16

试试这个

 $attribute = $_product->getResource()->getAttribute('custom_attribute_code');
    if ($attribute)
    {
        echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
    }

Try this

 $attribute = $_product->getResource()->getAttribute('custom_attribute_code');
    if ($attribute)
    {
        echo $attribute_value = $attribute ->getFrontend()->getValue($_product);
    }
撩动你心 2024-12-04 19:12:16

您不必加载整个产品。
Magentos 系列非常强大且智能。

$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
    )

You don't have to load the whole product.
Magentos collections are very powerful and smart.

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

At the moment you call getFirstItem() the query will be executed and the result product is very minimal:

[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
    )
北方的韩爷 2024-12-04 19:12:16

这个有效——

echo $_product->getData('ATTRIBUTE_NAME_HERE');

This one works-

echo $_product->getData('ATTRIBUTE_NAME_HERE');
谷夏 2024-12-04 19:12:16

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

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

You can get attribute value by following way

$model = Mage::getResourceModel('catalog/product');
$attribute_value = $model->getAttributeRawValue($productId, 'attribute_code', $storeId);
も星光 2024-12-04 19:12:16

我想你可以编写一个直接通过 sql 执行此操作的方法。

看起来像这样:

变量:

$store_id = 1;
$product_id = 1234;
$attribute_code = 'manufacturer';

查询:

SELECT value FROM eav_attribute_option_value WHERE option_id IN (
    SELECT option_id FROM eav_attribute_option WHERE FIND_IN_SET(
        option_id, 
        (SELECT value FROM catalog_product_entity_varchar WHERE
            entity_id = '$product_id' AND 
            attribute_id = (SELECT attribute_id FROM eav_attribute WHERE
                attribute_code='$attribute_code')
        )
    ) > 0) AND
    store_id='$store_id';

您必须根据属性的 backend_type(eav_attribute 中的字段)从正确的表中获取值,因此至少需要 1 个额外的查询。

You could write a method that would do it directly via sql I suppose.

Would look something like this:

Variables:

$store_id = 1;
$product_id = 1234;
$attribute_code = 'manufacturer';

Query:

SELECT value FROM eav_attribute_option_value WHERE option_id IN (
    SELECT option_id FROM eav_attribute_option WHERE FIND_IN_SET(
        option_id, 
        (SELECT value FROM catalog_product_entity_varchar WHERE
            entity_id = '$product_id' AND 
            attribute_id = (SELECT attribute_id FROM eav_attribute WHERE
                attribute_code='$attribute_code')
        )
    ) > 0) AND
    store_id='$store_id';

You would have to get the value from the correct table based on the attribute's backend_type (field in eav_attribute) though so it takes at least 1 additional query.

青朷 2024-12-04 19:12:16

如果您有一个名为 my_attr 的文本/文本区域属性,您可以通过以下方式获取它:
产品->getMyAttr();

If you have an text/textarea attribute named my_attr you can get it by:
product->getMyAttr();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文