1

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

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

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

非常感谢

4

3 回答 3

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>";
    }
?>
于 2013-12-22T22:37:06.173 回答
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();
}  

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

于 2013-03-01T15:53:15.073 回答
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);
于 2016-09-19T08:50:53.977 回答