-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtop_sales.php
46 lines (38 loc) · 1.19 KB
/
top_sales.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/**
* Created by PhpStorm.
* User: pc
* Date: 2017/10/6
* Time: 20:12
*/
include('app/Mage.php');
Mage::app();
$adapter = Mage::getSingleton('core/resource')->getConnection('core_write');
$date = date('Y-m-d',time()-3600*24*5);
$result = $adapter->query("SELECT product_id,SUM(qty_ordered) AS hot FROM sales_flat_order_item WHERE created_at>'".$date."' GROUP BY product_id");
$rows = $result->fetchAll();
$i = 0;
foreach($rows as $row){
$i++;
$top_sales_result = $adapter->query("select * from product_top_sales_month where item_id=".$i);
$top_sales = $top_sales_result->fetch();
if($top_sales){
$where = 'item_id='.$i;
$set = array(
'product_id'=> $row['product_id'],
'qty_ordered'=> $row['hot']
);
$adapter->update("product_top_sales_month",$set,$where);
}else{
$row = array(
'item_id' => $i,
'product_id'=>$row['product_id'],
'qty_ordered'=>$row['hot']
);
$adapter->insert("product_top_sales_month",$row);
}
}
if($i>0) {
$where = 'item_id>'.$i;
$adapter->delete('product_top_sales_month',$where);
}