[Magento] Get Product Attributes

来源:互联网 发布:软件行业增值税税负 编辑:程序博客网 时间:2024/05/12 02:13

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

This is how to get a drop down lists options. I don’t think it willwork for a mulit-select attribute. I stick the value/label pairsinto 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'];}$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'];}}}$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();//Referenced from /app/code/core/Mage/Eav/Model/Config/php @ line 443$_product->getResource()->getAttribute('club_type')->getFrontend()->getValue($_product)$attributesInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setEntityTypeFilter({entityType})->setCodeFilter($attributes)->addSetInfo()->getData();//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();
原创粉丝点击