Magento获得一个类别的制造商

时间:2014-08-22 16:12:51

标签: php magento

我目前可以通过以下代码获取制造商及其首批产品的清单:

    $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,如何获取制造商及其首批产品清单?

1 个答案:

答案 0 :(得分:0)

我相信这可以实现你想要的。

$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');

然后你可以遍历整个系列,它将为每个制造商提供第一个产品。