且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Magento:如何获取产品中使用的属性值

更新时间:2023-11-30 14:34:10

我相信您不是在尝试读取产品模型的属性值,而是获取特定属性的所有已使用值的列表.

I believe you are not trying to read an attribute value of a product model, but get a list of all used values for a specific attribute.

纯属属性是所有不使用selectmultiselect进行输入,而是使用text或textarea字段或类似内容的属性.

Plain attributes are all attributes that don't use a select or multiselect for input, but a text or textarea field or something similar.

对于这些属性,请使用:

For these attributes use this:

// specify the attribute code
$attributeCode = 'name';

// build and filter the product collection
$products = Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToFilter($attributeCode, array('notnull' => true))
        ->addAttributeToFilter($attributeCode, array('neq' => ''))
        ->addAttributeToSelect($attributeCode);

// get all distinct attribute values
$usedAttributeValues = array_unique($products->getColumnValues($attributeCode));

选项"属性

如果它是selectmultiselect属性,则仍需要将选项ID映射到选项值.如果您获取的是整数列表而不是人类可读的标签(例如colormanufacturer属性),就是这种情况.

"Option" attributes

If it is a select or multiselect attribute, you still need to map the option ID's to option values. That is the case if you are getting a list of integers instead of human readable labels (e.g. for the color or manufacturer attribute).

// specify the select or multiselect attribute code
$attributeCode = 'color';

// build and filter the product collection
$products = Mage::getResourceModel('catalog/product_collection')
        ->addAttributeToFilter($attributeCode, array('notnull' => true))
        ->addAttributeToFilter($attributeCode, array('neq' => ''))
        ->addAttributeToSelect($attributeCode);

$usedAttributeValues = array_unique($products->getColumnValues($attributeCode));

// ---- this is the different part compared to the previous example ----

// fetch the attribute model
$attributeModel = Mage::getSingleton('eav/config')
        ->getAttribute('catalog_product', $attributeCode);

// map the option id's to option labels
$usedAttributeValues = $attributeModel->getSource()->getOptionText(
    implode(',', $usedAttributeValues)
);

// $usedAttributeValues now contains an array of used values in human readable format

直接DB查询示例

根据您要执行此操作的位置,这里有一个不使用产品集合而获取值的示例.效率更高.
仅在资源模型中使用以下代码,因为那是与数据库相关的代码所属的.
这是一个教育性的示例,展示了如何使用Magento的EAV表.

Direct DB query example

Depending on where you want to do this, here is an example of fetching the values without using a product collection. It is slightly more efficient.
Only use the following code in resource models, as thats where DB related code belongs.
This is meant as an educational example to show how to work with Magento's EAV tables.

// specify the attribute code
$attributeCode = 'color';

// get attribute model by attribute code, e.g. 'color'
$attributeModel = Mage::getSingleton('eav/config')
        ->getAttribute('catalog_product', $attributeCode);

// build select to fetch used attribute value id's
$select = Mage::getSingleton('core/resource')
        ->getConnection('default_read')->select()
        ->from($attributeModel->getBackend()->getTable(), 'value')
        ->where('attribute_id=?', $attributeModel->getId())
        ->distinct();

// read used values from the db
$usedAttributeValues = Mage::getSingleton('core/resource')
        ->getConnection('default_read')
        ->fetchCol($select);

// map used id's to the value labels using the source model
if ($attributeModel->usesSource())
{
    $usedAttributeValues = $attributeModel->getSource()->getOptionText(
        implode(',', $usedAttributeValues)
    );
}

// $usedAttributeValues now contains an array of used option value labels