我设法覆盖Mage / Catalog / Model / Config.php,并创建一个方法,添加一个名为" Sort By Top Sellers"的新排序选项。
我还添加了一个 where 语句,我可以从上个月获取数据,但该集合不输出所有产品,只输出上个月购买的产品(我需要排序)所有产品,不仅仅是上个月的产品。
如果没有 where 语句,查询输出正确。
关于如何解决这个问题的任何想法?
public function sortByTopSelling($dir){
$today = time();
$last = $today - (60*60*24*30);
$from = date("Y-m-d H:i:s", $last);
$to = date("Y-m-d H:i:s", $today);
$this->getSelect()->joinLeft('sales_flat_order_item AS order_item','e.entity_id = order_item.product_id',
'SUM(order_item.qty_ordered) AS ordered_qty')->where('`order_item`.`created_at` > "'.$from.'"')->group('e.entity_id')->order('ordered_qty DESC');
}
答案 0 :(得分:1)
在回复评论或回答之间争论一段时间,因为我对Magento准则不是100%肯定。
MYSQL:
<?php
$sql = 'SELECT * FROM some_table WHERE some_value > :value OR some_value < :value';
$stmt = $dbh->prepare($sql);
$stmt->execute( array( ':value' => 3 ) );
?>
我没有可靠的方法来测试Magento代码,但它应该是以下内容:
select e.entity_id, SUM(order_item.qty_ordered) AS ordered_qty
from magento.catalog_product_entity as e
left join magento.sales_flat_order_item AS order_item on e.entity_id = order_item.product_id and order_item.created_at > DATE_FORMAT(NOW() ,'%Y-%m-01')
group by e.entity_id
order by ordered_qty DESC;
另一种方法是将 $this->getSelect()->joinLeft('sales_flat_order_item AS order_item',"e.entity_id = order_item.product_id and order_item.created_at > $from",
'SUM(order_item.qty_ordered) AS ordered_qty')->group('e.entity_id')->order('ordered_qty DESC');
添加到where,但这比其他连接条件需要更长时间才能运行,而在我的Magento DB中,查询已经花了90秒,但是我的表格很大,可能适用于您的实施。