问题描述
我正在尝试在我的magento网站上生成最畅销产品"列表.我尝试从.这样做可以,但是,我意识到这仅是简单产品的理想选择.我正在尝试为可配置产品创建排名,因此我认为我需要首先获取简单产品的总订单,但我不确定该如何以编程方式进行.这基本上是我想到的主意:
I am trying to generate a "best selling products" list on my magento site. 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 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;
但是,在尝试对其进行测试时,我所有的数量都返回0,所以我被卡住了.如何实现可配置产品的排名?请指教.另外,如果可能的话,我想对核心文件进行尽可能少的更改.
However, while trying to test it, all of my quantities return 0 so I'm stuck. 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.
此外,我的排名列表限制为10,如果其他产品的销量为0,则它们将自动按字母顺序排序.例如,如果2种产品的销售额相同,则情况相同.
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. Same goes if for example 2 products have the same amount sold.
推荐答案
经过8个多小时的尝试来弄清楚这一点.我想出了一些解决方法(至少似乎可以解决我的问题),代码肯定需要大量改进,因此我很乐意接受对我的答案的任何修改和建议.这是我想出的代码:
After literally more than 8 hours of trying to figure this out. 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);
所以这的基本思想是我仅获得具有订单的产品的集合,因为在出现时,不会检索到具有0个订单的产品.在那之后,我得到了仍需要在我的排名上显示10个产品的项目数.然后,只要显示的产品少于10个,我就会获得另一个产品集合并回显它们.我使用填充器数组来确保不会显示第一个集合已调用的产品.有了此代码,我现在在我的magento网站中获得了最畅销产品的排名.
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. After that, I get the number of items I still need to display 10 products on my ranking. Then, I get another collection of products and echo them as long as there's still less than 10 products displayed. 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中的排名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!