【问题标题】:Magento - get product count by attributeMagento - 按属性获取产品数量
【发布时间】:2015-04-01 16:35:17
【问题描述】:

我已经搜索了好几个小时,但似乎无法找到一个好的答案。

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

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

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

提前致谢!

【问题讨论】:

    标签: magento


    【解决方案1】:

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

    如果你愿意,你可以打印出每个属性的产品数量,包括.....

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多