Magento通过制造商获取产品

时间:2014-08-20 16:08:50

标签: 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();
    }   


    if (count($preparedManufacturers)) {
        echo "<h2>Manufacturers</h2><ul>";
        foreach($preparedManufacturers as $optionId => $value) {
            echo "<li>" . $value . " - (ID:" . $optionId . ")</li>";
        }
        echo "</ul>";
    }        

如何获得每个制造商的第一个产品?

由于

1 个答案:

答案 0 :(得分:1)

试试这个

     if (count($preparedManufacturers)) {
    echo "<h2>Manufacturers</h2><ul>";
    foreach($preparedManufacturers as $optionId => $value) {
  <!-- add the this code for get first item id -->
    $firstProductId=Mage::getModel('catalog/product')->getCollection()
        ->addStoreFilter(0)
        ->addAttributeToFilter('manufacturer',$optionId)->getFirstItem()->getId();
        echo "<li>" . $value . " - (ID:" . $optionId . ")</li>";
    }
    echo "</ul>";
}