2015-04-01 191 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_”前缀的属性。