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>';
获取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
相关推荐
magento通过产品名称获取产品id提示代码
我们公司在用的商城产品属性过滤插件,希望对大家有用,同时,看还有什么好的建议和意见也可以提出,我们再次修改
magento通过mysql语句批量导出产品及其自定义属性
magento价格过滤器 可以拖动的区间,自动填写
Magento导入导出产品插件 编辑csv表格导入产品 Import Products categories multiple images and custom options 1 1 2 tgz 与官方插件一样
如何给产品描述里的关键字、字词批量添加链接?我想大家一直在的这样的插件。说实话,我也找了很久,也没有找到。可是由于需要,不得不自己开发一个这样的插件。并支持到Magento1.6.2 说明请查看:...
magento 批量上传产品,多图,自定义属性
Chapter 1, Installing Magento 2 on Apache and NGINX, is a totally different ballgame compared to Magento 1. Where Magento 1 could be installed through FTP or SSH, Magento 2 is installable only via the...
这个文件用于magento 批量上传产品后,前端只显示产品主图不能显示详细图片的bug. 也可用于批量上传商品的尺码或属性。注意: media_img.csv和 attrib.csv这两个文件只能放在tool目录里面。这个工具测试了magento ...
magento颜色属性图片展示插件,经过测试适用于magento1.7
You’ll start by getting a general understanding of what Magento is, why and how you should use it, and whether it is possible and feasible to migrate from an old web store to Magento 2. As you work ...
magento站点首页显示新产品及产品数量,可在block块中设置产品数量
为什么magento产品不改变“缺货”尽管数量已经低于最低
magento产品名称及描述批量修改sql
Magento1.7测试可用 批量产品多图上传 包含上传样本
无需进入产品产品详情页就可以预览产品图片,商店访问者也可以通过单击项目图片进入产品页面而没有任何问题(禁止用作商业用途)
magento数据结构分析 magento数据字典
兼容Magento 1.9.x, 1.8.x, 1.7.x ,特点:完全自适应(响应式布局),自定义子主题,无限颜色,完全自定义,一键安装演示,小工具化横幅系统,颜色选择,MEGA菜单,多商店支持,自定义产品尺寸,黑白2个版本,AJAX...
Magento列表页面属性筛选,实现URL静态化展示,eg:http://***.com/shopby/color/1/design/2/...