2012-09-05 61 views
1

我使用下面的代碼列出所有的製造商和它的工作原理就像一個魅力: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 . " - (" . $optionId . ")</li>"; 
    } 
    echo "</ul>"; 
} 

我所尋找的是顯示數字的方式與每個製造商相關的產品。有人能告訴我這樣做的方式嗎?

非常感謝

回答

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

這是每個廠家一個額外的查詢,所以它不是很大,如果你有一些緩存等它贏得」不過,太糟糕了。

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>"; 
    } 
?> 
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); 
相關問題