简体   繁体   English

Magento中的可配置产品排名

[英]Configurable products ranking in Magento

I am trying to generate a "best selling products" list on my magento site. 我试图在我的magento网站上生成“最畅销的产品”列表。 I tried following the answer from here . 我尝试按照这里的答案进行操作。 This works okay, however, I realized that this is only ideal for simple products. 可以,但是,我意识到这仅适用于简单产品。 I am trying to create the ranking for configurable products so I believe I need to get the total orders for the simple products first which I'm not sure how I can do programmatically. 我正在尝试为可配置产品创建排名,因此我认为我需要首先获取简单产品的总订单,但我不确定该如何编程。 This is basically the idea I have in mind: 这基本上是我想到的想法:

Get the simple products associated to a configurable product 获取与可配置产品关联的简单产品

Get the total orders for each simple product and sum them up 获取每个简单产品的总订单并汇总

Pass the total sum to configurable products again (i'm thinking of using an array here) then call the necessary data ordered by ranking. 再次将总和传递给可配置产品(我在这里考虑使用数组),然后调用按排名排序的必要数据。

So far I have come up with this code: 到目前为止,我已经提出了以下代码:

$storeId = Mage::app()->getStore()->getId();
$_collection = Mage::getResourceModel('reports/product_collection')
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter('type_id', 'configurable');
foreach($_collection as $c):
$configurable = $c->getTypeInstance()->getUsedProductIds();
foreach($configurable as $_config):
    $_simple = Mage::getModel('catalog/product')->load($_config);
                echo $_simple->getName()."<br/>";
                echo "qty:".(int)$_simple->ordered_qty."<br/>";
            endforeach;
        endforeach;

However, while trying to test it, all of my quantities return 0 so I'm stuck. 但是,在尝试对其进行测试时,我所有的数量都返回0,所以我被卡住了。 How do I implement the ranking for configurable products? 如何实现可配置产品的排名? Please advise. 请指教。 Also, if possible, I'd like to make as little changes to the core files as possible. 另外,如果可能的话,我想对核心文件进行尽可能少的更改。

Also, I have this ranking list limited to 10 and if the other products have 0 quantity sold, then they will automatically be sorted alphabetically. 另外,我的排名列表限制为10,如果其他产品的销量为0,则它​​们将自动按字母顺序排序。 Same goes if for example 2 products have the same amount sold. 例如,如果2种产品的销售额相同,则结果相同。

After literally more than 8 hours of trying to figure this out. 经过8个多小时的尝试来解决这个问题。 I've come up with some sort of workaround (at least it seems to work for my case), the code surely need a lot of improvement so I'd be glad to accept any modifications and suggestions to my answer. 我已经提出了一些解决方法(至少似乎可以解决我的问题),代码肯定需要大量改进,因此我很乐意接受对我的答案的任何修改和建议。 Here's the code I came up with: 这是我想出的代码:

$filler = array();

$productCollection = Mage::getResourceModel('reports/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('type_id', 'configurable')
            ->addOrderedQty()
            ->setOrder('ordered_qty', 'desc')
            ->setPage(1, 10);

foreach($productCollection as $product):
    echo "name:".$product->getName()." - qty:".(int)$product->ordered_qty."<br/>";
    array_push($filler, $product->getId());
endforeach;

if(count($productCollection) < 10):
    $add = 10 - count($productCollection);

    $fillCollection = Mage::getResourceModel('reports/product_collection')
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('type_id', 'configurable')
            ->setOrder('name', 'desc');

    foreach($fillCollection as $item): 
        if(in_array($item->getId(), $filler)):
        else:
            if($add > 0):
                echo "name:".$item->getName()." - qty: 0 <br/>";
                $add = $add - 1;
            endif;
        endif;
    endforeach;
endif;  
unset($filler);

So the basic idea of this is I get the collection for products with orders only since as it appears, products with 0 orders are not retrieved. 因此,这的基本思想是,我仅获得具有订单的产品的集合,因为它看起来像是不检索具有0个订单的产品。 After that, I get the number of items I still need to display 10 products on my ranking. 之后,我得到了仍需要在我的排名上显示10个产品的项目数。 Then, I get another collection of products and echo them as long as there's still less than 10 products displayed. 然后,只要显示的产品少于10个,我就会获得另一个产品集合并回显它们。 I used the filler array to make sure that products already called by the first collection are not displayed. 我使用填充器数组来确保不显示第一个集合已调用的产品。 Given this code, I now have a ranking of best selling products in my magento site. 有了此代码,我现在在我的magento网站中对最畅销产品进行了排名。

Check this solution: 检查此解决方案:

I found that configurable product sales are being summed correctly but aren't being included in the results; 我发现可配置产品的销售量已正确汇总,但未包含在结果中; their child products appear instead. 他们的子产品出现了。 My solution was to include configurable products, do a left join on the catalog_product_super_link table, and filter out anything that has a parent_id . 我的解决方案是包括可配置的产品,对catalog_product_super_link表进行左连接,并过滤掉任何具有parent_id东西。 Here are the changes you'll need to make: 这是您需要进行的更改:

Collection.php: Collection.php:

    public function addOrderedQty($from = '', $to = '', $getComplexProducts=false, $getComplexChildProducts = true, $getRemovedProducts = true)
    {
        $qtyOrderedTableName = $this->getTable('sales/order_item');
        $qtyOrderedFieldName = 'qty_ordered';

        $productIdFieldName = 'product_id';

        if (!$getComplexProducts) {
            $compositeTypeIds = Mage::getSingleton('catalog/product_type')->getCompositeTypes();
            $productTypes = $this->getConnection()->quoteInto(' AND (e.type_id NOT IN (?))', $compositeTypeIds);
        } else {
            $productTypes = '';
        }

        if ($from != '' && $to != '') {
            $dateFilter = " AND `order`.created_at BETWEEN '{$from}' AND '{$to}'";
        } else {
            $dateFilter = "";
        }

        $this->getSelect()->reset()->from(
            array('order_items' => $qtyOrderedTableName),
            array(
                'ordered_qty' => "SUM(order_items.{$qtyOrderedFieldName})",
                'order_items_name' => 'order_items.name'
            )
        );

         $_joinCondition = $this->getConnection()->quoteInto(
                'order.entity_id = order_items.order_id AND order.state<>?', Mage_Sales_Model_Order::STATE_CANCELED
         );
         $_joinCondition .= $dateFilter;
         $this->getSelect()->joinInner(
            array('order' => $this->getTable('sales/order')),
            $_joinCondition,
            array()
         );

         // Add join to get the parent id for configurables
         $this->getSelect()->joinLeft(
             array('cpsl' => $this->getTable('catalog/product_super_link')),
             'cpsl.product_id = order_items.product_id',
             'cpsl.parent_id'
         );

        if(!$getComplexChildProducts)
            $this->getSelect()->having('parent_id IS NULL');

        if($getRemovedProducts)
        {
             $this->getSelect()
                ->joinLeft(array('e' => $this->getProductEntityTableName()),
                    "e.entity_id = order_items.{$productIdFieldName} AND e.entity_type_id = {$this->getProductEntityTypeId()}{$productTypes}")
                ->group('order_items.product_id');
        }
        else
        {
            $this->getSelect()
                ->joinInner(array('e' => $this->getProductEntityTableName()),
                    "e.entity_id = order_items.{$productIdFieldName} AND e.entity_type_id = {$this->getProductEntityTypeId()}{$productTypes}")
                ->group('e.entity_id');
        }


        $this->getSelect()->having('ordered_qty > 0');

        // This line is for debug purposes, in case you'd like to see what the SQL looks like
        // $x = $this->getSelect()->__toString();

        return $this;
    }

List.php - Find the following two lines... List.php-查找以下两行...

$bestsellers->addOrderedQty($startDate, $todayDate, true);
$bestsellers->addOrderedQty('', '', true);

... and change them to: ...并将其更改为:

$bestsellers->addOrderedQty($startDate, $todayDate, true, false, false);
$bestsellers->addOrderedQty('', '', true, false, false);

My changes added two new optional parameters, which both default to true , as to not break existing functionality. 我的更改添加了两个新的可选参数,它们都默认为true ,以免破坏现有功能。

  • When $getComplexChildProducts is set to false , all child items of the configurable product will be removed from the results. $getComplexChildProducts设置为false ,可配置产品的所有子项将从结果中删除。
  • $getRemovedProducts determines whether or not previously ordered products (which have since been deleted from Magento) should also appear. $getRemovedProducts确定是否还应显示以前订购的产品(此后已从Magento中删除)。

Please note that your report statistics will need to be up-to-date in order to get accurate results. 请注意,您的报告统计信息需要最新才能获得准确的结果。

Source and acknowledgements: https://stackoverflow.com/a/8419579/1136132 来源和确认: https : //stackoverflow.com/a/8419579/1136132

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM