Magento - 按属性获取产品数量

Magento - get product count by attribute

我已经搜索了好几个小时了,但似乎找不到合适的答案。

我有一堆不同的产品,还有一堆不同的属性和属性集。

我想计算在整个属性列表中具有有效属性值的产品数量。所以,我想遍历每个属性,然后计算具有该属性的产品数量,并且该属性具有良好的价值。

我们所有的属性都来自第三方。因此,他们通常要么将值留空,要么输入 "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 个属性都可用于分层导航。所以,如果有人有更好的方法来解决分层导航问题,我也洗耳恭听。

提前致谢!

这是我所做的。我仍然需要弄清楚我将对给定属性使用多少计数并将属性设置为可用于分层导航,但这将通过并找出对给定属性具有看似好的值的产品数量。

如果需要,您可以打印出每个属性的产品数量,方法是包含类似...

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

将其放在 "collection" 代码段之后的某处。

    //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 进行产品联合。所以,我只想看看 icecat 属性,它们以 "i_" 为前缀,没有引号。因此,我排除了所有没有 "i_" 前缀的属性。