2015-04-01 195 views
0

我一直在尋找好幾個小時,我似乎無法想出一個很好的答案。Magento - 按屬性獲取產品數量

我有一堆不同的產品,和一堆不同的屬性和屬性集。

我在計算整個屬性列表中具有有效屬性值的產品數量。因此,我想遍歷每個屬性,然後統計具有該屬性的產品數量,併爲該屬性設置好值。

我們所有的屬性都來自第三方。所以,他們經常要麼把這個值留空,要麼把它放在「N/A」上。

我現在面臨的問題是,我甚至無法獲得具有該特定屬性的產品。 'notnull'過濾器不適用於我。我已經嘗試了這麼多種不同的方式。這是我目前無法使用的代碼,但看起來最有前途。我會給你這個錯誤,但如果有人有解決方案,我願意與你分享。

$productAttrs = Mage::getResourceModel('catalog/product_attribute_collection'); 
    $x = 0; 
    foreach ($productAttrs as $productAttr) { 
     $collection_size = Mage::getModel('catalog/product')->getCollection() 
      ->addAttributeToSelect($productAttr->getAttributeCode()) 
      ->addAttributeToFilter(
       array 
       (
        array 
        (
         'attribute' => $productAttr->getAttributeCode(), 
         'notnull' => true 
        ), 
        array 
        (
         'attribute' => $productAttr->getAttributeCode(), 
         'nin' => array('N/A', '', ' ') 
        ) 
       ) 
      ); 
     echo count($collection_size->getData()); 
     echo "<br>"; 
     $x++; 
     if($x>50) { 
      break; 
     } 
    } 

因此,對於上述情況,我只顯示了前50個屬性。這個特定的代碼實際上給出了錯誤。它僅在顯示第一個屬性的計數之後纔會出現錯誤,所以我認爲這只是因爲category_id屬性需要被忽略(我現在要這樣做),但它不會計算產品的正確數量 - - 當我知道並非所有產品實際上都具有特定的屬性時,它似乎給出了商店中所有產品的價值。

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'at_category_ids.category_ids' in 'field list'' in /var/www/html/store/lib/Zend/Db/Statement/Pdo.php:228 

我不確定該從哪裏出發。

此外,我這樣做的原因.....我有分層導航,只想打開屬性在200多個產品中正確設置。我不希望所有4000個屬性都可用於分層導航。所以,如果任何人有更好的解決分層導航問題的方法,那麼我也是。

在此先感謝!

回答

0

這就是我所做的。我仍然必須弄清楚給定的屬性使用什麼數值,並設置屬性可用於分層導航,但是這將通過並找出對於給定屬性具有看似良好值的產品的數量。

如果你願意,你可以通過包括像打印出的每個屬性的產品數量.....

   echo $productAttr->getAttributeCode(); 
       echo " : "; 
       echo count($collection->getData());    
       echo "<br>"; 

把「收藏」的代碼後這個地方。

//get list of attributes 
    $productAttrs = Mage::getResourceModel('catalog/product_attribute_collection'); 
    $x = 0; 
    $y = array(); 
    //go through each attribute and get the number of products that exist for that attribute 
    foreach ($productAttrs as $productAttr) { 
     //exclude attributes that don't match criteria 
     if($productAttr->getAttributeCode() == 'category_ids' || substr($productAttr->getAttributeCode(), 0, 2) != 'i_') { 
       continue; 
     } 
     //get attributes that match the following 
     $collection = Mage::getModel('catalog/product')->getCollection() 
      ->addAttributeToSelect($productAttr->getAttributeCode()) 
      ->addAttributeToFilter(
       array 
       (
        array 
        (
         'attribute' => $productAttr->getAttributeCode(), 
         'notnull' => true 
        ), 
        array 
        (
         'attribute' => $productAttr->getAttributeCode(), 
         'nin' => array('N/A', '', ' ') 
        ) 
       ) 
      ); 
     //use y array to count how many attributes have more than certain amount of products 
     //this loop will give 100, 200, 300, 400, 500, 600, 700, 800, 900, and 1000 
     //loops backwards and breaks if count is over the current number 
     for($z=0; $z<=10; $z++) { 
      if(count($collection->getData()) > $z*100) { 
       $y[$z]++; 
       // break; 
      } 
     } 
     $x++; 
    } 
    //after finding out numbers, print the results 
    $number_over = 0; 
    foreach($y as $num) { 
     $number_under = $number_over+100; 
     echo "<br>Total number of attributes that are set in over " . $number_over; 
     echo " products total is: " .$num; 
     $number_over = $number_over + 100; 
    } 
} 

編輯: 正如我以前說過,我使用產品的聯合式Icecat。所以,我只想看看帶有「i_」前綴的icecat屬性,而沒有引號。所以,我排除了所有沒有「i_」前綴的屬性。