Magento获取一个类别的制造商


Magento Get Manufacturers of a category

我目前可以获得制造商及其每个第一个产品的列表,代码如下:

    $attribute = Mage::getModel('eav/entity_attribute')
                    ->loadByCode('catalog_product', 'manufacturer');
    $valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
                ->setAttributeFilter($attribute->getData('attribute_id'))
                ->setStoreFilter(0, false);
    $preparedManufacturers = array();            
    foreach($valuesCollection as $value) {
        $preparedManufacturers[$value->getOptionId()] = $value->getValue();
    }   
    $prefs = array();        
    if (count($preparedManufacturers)) {
        foreach ($preparedManufacturers as $optionId => $value) {
            $manufacturerItem = array();
            $manufacturerItem['manufacturerID'] = $optionId;
            $manufacturerItem['manufacturerName'] = $value; 
            $firstProductId=Mage::getModel('catalog/product')->getCollection()
                ->addStoreFilter(0)
                ->addAttributeToFilter('manufacturer',$optionId)->getFirstItem()->getId();              
            $product = Mage::getModel('catalog/product')->load($firstProductId);
            $manufacturerItem['productImageURL'] = (string)Mage::helper('catalog/image')->init($product, 'thumbnail')->resize(100);;
            $prefs['manufacturer'][] = $manufacturerItem;
        }
        $mainBlock = $this->getLayout()->createBlock(
                                       'Mage_Core_Block_Template',
                                       'mainContentProducts',
                     array(
                         'template' => 'page/manufacturer.phtml'
                     )
            ) // NOTE - Custom Variables Below
            ->setData('prefs', $prefs);
        $this->getLayout()->getBlock('content')->append($mainBlock);            
    } 

如果我有一个类别ID,我如何获得制造商和他们的第一个产品的列表?

我相信这达到了你想要的效果。

$category = Mage::getModel('catalog/category')->load(**Id Goes Here**);
$productCollection = Mage::getModel('catalog/product')->getCollection()
    ->addCategoryFilter($category)
    ->addAttributeToSelect('manufacturer')
    ->addAttributeToSort('entity_id','ASC')
    ->groupByAttribute('manufacturer');

然后循环遍历集合,它将包含每个制造商的第一个产品。