Magento - 获取所有产品数量的制造商列表

时间:2012-09-05 06:05:52

标签: magento-1.7

我使用以下代码列出所有制造商,它就像一个魅力:

$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 . " - (" . $optionId . ")</li>";
    }
    echo "</ul>";
}

我正在寻找的是一种显示与每个制造商相关的产品数量的方法。有人可以告诉我这样做的方法吗?

非常感谢

3 个答案:

答案 0 :(得分:1)

不是我的全部,但在1.6 +中为我工作?

<?php
    include_once 'app/Mage.php';
    Mage::app();

    $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) {
            $collection = Mage::getModel('catalog/product')->getCollection();
            $collection->addFieldToFilter(array(array('attribute' => 'manufacturer', 'eq' => $optionId)));
            $mumberOfProducrt = count($collection);
            echo "<li>" . $value . " - (" . $mumberOfProducrt . ")</li>";
        }
        echo "</ul>";
    }
?>

答案 1 :(得分:0)

这样可行,但效率不高:

foreach($valuesCollection as $value) {
    $preparedManufacturers[$value->getOptionId()] = $value->getValue();
    $collection = Mage::getModel('catalog/product')->getCollection();
    $collection
        ->addAttributeToSelect('*') // '*' not efficient though
        ->addAttributeToFilter('manufacturer', array('eq' => $value->getOptionId()))
        //->addAttributeToFilter('manufacturer', $value->getOptionId())
     ;
    $count = $collection->->getSize();
}  

这是对每个制造商的额外查询,所以它不是很好,如果你有一些缓存等,它不会太糟糕。

答案 2 :(得分:0)

工作代码应该这样做

$collection = Mage::getModel('catalog/product')->getCollection()->groupByAttribute('manufacturer')
                ->addFieldToFilter('status',Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
                ->addExpressionAttributeToSelect("count",'COUNT({{entity_id}})', 'entity_id');
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);