Magento 获取产品(filter过滤器)属性值 Magento Product Attributes Values and Labels

I have found that it is very useful to be able to get attributes from the system and use them in places other than a products category page. I always forget the exact syntax to use so, this is going to be my unofficial cheat sheet.

This is how to get a drop down lists options. I don’t think it will work for a mulit-select attribute. I stick the value/label pairs into an array to use how I please.

$attribute = Mage::getModel('eav/config')->getAttribute('catalog_product', 'attribute_id');
foreach ( $attribute->getSource()->getAllOptions(true, true) as $option){
    $attributeArray[$option['value']] = $option['label'];
}

I had a trickier time getting values for a multi-select attribute. I don’t think that this is the best method, but it is one that worked for me. First the multi-select attribute must be set to be used in the advanced search. You can set this in the manage attributes area.

$attributes = Mage::getModel('catalogsearch/advanced')->getAttributes();
$attributeArray=array();
foreach($attributes as $a){
    if($a->getAttributeCode() == 'desired_attribute_code'){
        foreach($a->getSource()->getAllOptions(false) as $option){
            $attributeArray[$option['value']] = $option['label'];
        }
    }
}

Here is a better way to get the multi-select attribute values.

$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','attribute_code_here');
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions();

If you only need to retrieve a value for a product you can try this way

//Referenced from /app/code/core/Mage/Eav/Model/Config/php @ line 443
$_product->getResource()->getAttribute('club_type')->getFrontend()->getValue($_product)

The code below will get an attribute collection. Set {entityType} to 4 to get attributes for products. You can remove the “setCodeFilters” line if you want to get all the attributes. To get anything really useful you will probably need to get the resulting attribute ids and do someting with them, like use them in a filter for the products collection or alike.

//
$attributesInfo = Mage::getResourceModel('eav/entity_attribute_collection')
->setEntityTypeFilter({entityType})
->setCodeFilter($attributes)
->addSetInfo()
->getData();

 

Entity Type Id’s
$entityType is an integer id for what type of entity the attribute is associated to. If you look at the “eav_attribute” table you will see that each attribute has an entity_type_id.

1 = Customer Entity
2 = Shipping Entity (I believe)
3 = Category Entity
4 = Product Entity

To make a product attribute avaliable when getting a product collection (such as on a category page or search results page) you can add some code to config.xml file that instructs Magento to always load the attribute when a product collection is loaded.

How to load an attribute whenever a product collection is loaded:
This can go in any config.xml file. I would recommend putting it in a custom module rather than one from the core code. Just replace “attribute_name” with the attribute code of the attribute you are trying to add.

How to get only the Attribute Values that have been used on products

The Code below will get the attribute values that have been used on the products in the product collection.

//First get the attribute id
$audienceAttributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','session_audience');
//Now Load the attribute
$audienceAttribute = Mage::getModel('catalog/resource_eav_attribute')->load($audienceAttributeId);

//Now get the product collection that you want to use to fine the attributes values for.
//I wanted the attribute values for only grouped products. You could add category filters and such
$productCollection = Mage::getModel('catalog/product')->getCollection()
->addStoreFilter(Mage::app()->getStore())
->addAttributeToSelect('session_audience')
->addAttributeToSort('session_audience', 'asc')
->addAttributeToFilter('type_id', array('eq' => 'grouped'));

//Now get the product ids for the collection
$productCollectionIds = $productCollection ->getAllIds();

//Now we query the data base to get the attribute values for the given product ids.
//NOTE that I am selecting from the catalog_product_entity_varchar table because this is the type of attribute I was using.
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$select = $read->select();
$select->from('catalog_product_entity_varchar')
->where('attribute_id = ?', $audienceAttributeId)
->where('entity_id IN (?)', $productCollectionIds );

//print_r($select->__toString());die();
 
//Now get the ids for the attribute values.
$result = $read->query($select);
$attributeOptionIds = array();
while($row = $result->fetch()){
    $attributeOptionIds = array_merge($attributeOptionIds, explode(',', $row['value']));
}
array_unique($attributeOptionIds);
//print_r($audienceOptions);die();
 
//Now get the actual values for the Ids as a Collection and do something with the values.
$filteredAudienceCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
->setPositionOrder('asc')
->setAttributeFilter($audienceAttributeId)
->setStoreFilter($audienceAttribute->getStoreId())
->addFieldToFilter('main_table.option_id', array('in' => $attributeOptionIds))
->load();

 

个人实例:

//app/design/frontend/default/{theme}/template/catalog/layer/view.phtml
$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','shoes_styles'); //shoes_styles 是Attribute Code 见下图
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions();

//echo '<pre>'; print_r($attributeOptions); echo '</pre>';

Selection_004

获取Attribute Code的方法是:

//app/design/frontend/default/{theme}/template/catalog/layer/view.phtml
$_filters = $this->getFilters();
foreach ($_filters as $_filter): 
    $_attributeModel = $_filter->getAttributeModel();
    $_attribute_code = $_attributeModel->getAttributeCode();
endforeach;

 

原文:http://www.emagen.co.uk/magento-tips-advice/getting-attributes/

本文: Magento 获取产品(filter过滤器)属性值 Magento Product Attributes Values and Labels

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.