Reputation: 567
In Magento is there any way i can filter product attributes based on product category? Actually i am working to build Make -> Model -> Year type of search functionality.
Here is the code which is working fine to build dropdown of product attributes but i want to filter attributes based on categories
$makeattribute_code = $attr_name;
$resource = Mage::getSingleton('core/resource');
$readonce = $resource->getConnection('core_read');
$table1 = $resource->getTableName('eav_attribute');
$table2 = $resource->getTableName('catalog_product_entity_varchar');
$makequery = $readonce->query("select attribute_id from " . $table1 . " where attribute_code='" . $makeattribute_code . "'");
$row = $makequery->fetch();
$make_attributeid = $row['attribute_id'];
$genquery1 = $readonce->query("select attribute_id from " . $table1 . " where attribute_code='" . $modelattribute_code . "'");
$row = $genquery1->fetch();
$model_attributeid = $row['attribute_id'];
$queryString = "SELECT distinct value from " . $table2 . " where attribute_id=" . $model_attributeid . " and entity_id in (select entity_id from " . $table2 . " where attribute_id=" . $make_attributeid . " and value like '%" . $value . "%') order by value";
Upvotes: 3
Views: 1178
Reputation: 13822
You can steal the logic from the layered nav blocks
// Id of category we want to get attributes for
$categoryId = 355;
// Products in that category
$products = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection();
// Get all attributes
$attributes = Mage::getResourceModel('catalog/product_attribute_collection')
->setItemObjectClass('catalog/resource_eav_attribute')
// Filter by attribute sets of products in desired category
->setAttributeSetFilter($products->getSetIds())
->addStoreLabel(Mage::app()->getStore()->getId());
foreach ($attributes as $attribute) {
// Do things with the attributes
var_dump($attribute->getAttributeCode());
}
MySQL, bad!
Upvotes: 2