How to get all manufacturers for category in Magento
I've got such a problem with Magento CMS. I need to retrieve all manufacturers for category. At first glance this is not a problem, because there are a Filter block and Layer navigation from which you can take the necessary methods.
First of all I create a public method in redefined category model /app/code/local/Mage/ Catalog/Model/Category.php
public function getManufacturers()
{
$collection = Mage::getRes开发者_开发问答ourceModel('catalog/product_attribute_collection')
->setItemObjectClass('catalog/resource_eav_attribute');
$setIds = $this->getProductCollection()->getSetIds();
$collection->getSelect()->distinct(true);
$collection
->setAttributeSetFilter($setIds)
->addStoreLabel(Mage::app()->getStore()->getId())
->setOrder('position', 'ASC');
$collection->addIsFilterableFilter();;
$collection->load();
return $collection;
}
I call this method in category template:
$manufscturers = $_category->getManufacturers();
So we get a huge object Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Attribute_Collection
.
Then:
$items = $manufscturers->getItems();
And we get object Mage_Catalog_Model_Resource_Eav_Attribute
.
Then I do not know what to do. That is a dead end. Maybe it's the wrong way?
The version of Magento - 1.4.0.1
Thank's for your help!
Here is how you should get all manufacturers for a category:
$category = Mage::registry('current_category');
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($category);
$attributes = $layer->getFilterableAttributes();
$manufacturers = array();
foreach ($attributes as $attribute) {
if ($attribute->getAttributeCode() == 'manufacturer') {
$filterBlockName = 'catalog/layer_filter_attribute';
$result = Mage::app()->getLayout()->createBlock($filterBlockName)->setLayer($layer)->setAttributeModel($attribute)->init();
foreach($result->getItems() as $option) {
$manufacturers[$option->getValue()] = $option->getLabel();
}
}
}
var_dump($manufacturers);
Hope this was useful.
Cheers!
As far as I can see, you have achieved product attribute collection that doesn't depends from given category or product collection.
My advise to you is to take product product collection for given cateogory like:
$layer = $this->getLayer();
$productCollection = $layer->getProductCollection();
then iterate through it and get all all attribute values for given category. Cache the results. Exactly the same is done in magento (in "magento way" ofcourse)
It took a long time but my answer might help someone.
If you need get category filter on category page you can get wrong result.
My code is based on code @MagePsycho
public function getCategoryAttributeFilter($category, $attributeCode)
{
/** @var Mage_Catalog_Model_Layer $layer */
$layer = Mage::getModel('catalog/layer');
$layer->setCurrentCategory($category);
$attributes = $layer->getFilterableAttributes();
$request = Mage::app()->getRequest();
Mage::app()->setRequest($newRequest = new Mage_Core_Controller_Request_Http());
$newRequest->setParam($attributeCode, false);
$items = array();
foreach ($attributes as $attribute) {
if ($attribute->getAttributeCode() == $attributeCode) {
$filterBlockName = 'catalog/layer_filter_attribute';
/** @var Mage_Catalog_Block_Layer_Filter_Attribute $block */
$block = Mage::app()->getLayout()->createBlock($filterBlockName);
$result = $block->setLayer($layer)->setAttributeModel($attribute)->init();
foreach($result->getItems() as $option) {
$manufacturers[$option->getValue()] = $option->getLabel();
}
//$items = $result->getItems();
break;
}
}
Mage::app()->setRequest($request);
Zend_Debug::dump($manufacturers);
return $items;
}
$category = Mage::getModel('catalog/category')->load(/*category id here*/);
$helper->getCategoryAttributeFilter($category, 'brand');
If you want to add layered navigation in the manufacturer page . Please add manufacturer as the categories and use the following magento scripts to create the categories and assign products programmatically.
http://www.pearlbells.co.uk/how-to-create-new-categories-and-assigned-products-to-category-programmatically-magento/
$attrLabel = 'manufacturer';
$attr = $product->getResource()->getAttribute($attrLabel);
$manufacturer_id = $attr->getSource()->getOptionId($manufacturer);
$newproducts = $product->getCollection()->addAttributeToFilter(array(array('attribute'=>'manufacturer', 'eq'=> $manufacturer_id)));
For assign the products
$newCategory = array( $list[0] , $list[$key]);
foreach($newproducts as $prod)
{
$prod->setCategoryIds(
array_merge($prod->getCategoryIds(), $newCategory)
);
$prod->save();
}
精彩评论