doumao1519 2014-08-22 16:12
浏览 14
已采纳

Magento获得一个类别的制造商

I currently can get a list of manufacturers and each of their first product with the code below:

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

If I have a category ID how do a get a list of manufacturers and their first product?

  • 写回答

1条回答 默认 最新

  • dongzi4030 2014-08-22 19:07
    关注

    I believe this accomplishes what you want.

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

    Then you can loop through the collection and it'll have the first product for each manufacturer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题