-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget_orders.php
183 lines (133 loc) · 5.83 KB
/
get_orders.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
ini_set('error_reporting', E_ALL);
error_reporting(1);
error_reporting(E_ALL);
/*~ get_orders.php
.---------------------------------------------------------------------------.
| Software: eKomi - get_orders module |
| Version: 2.0.2 |
|Compatible: eKomi Core 2.0.0 - 2.0.2 |
| Updated: 2013-03-15 |
| Contact: +49 30 2000 444 999 | [email protected] |
| Info: http://ekomi.de |
| Support: http://ekomi.de |
| ------------------------------------------------------------------------- |
| Authors: Simon Becker, Boris Iakovenko, Philipp Dahse | eKomi |
| Copyright: (c) 2008-2013, eKomi Ltd. All Rights Reserved. |
'--------------------------------------------------------------------------*/
require_once( 'includes/config.inc.php' );
require_once( 'includes/func.ekomi.php' );
require_once( 'includes/func.log.php' );
require_once( 'includes/class.phpmailer.php' );
require_once( 'includes/func.api.' . check_api_type() . '.php' );
check_auth();
startup();
define('ORDERS_STATUS', "'complete'"); // you can use multiple stati - comma separated in single quotes. example: define('ORDERS_STATUS', "'status_1','status_2','status_3'");
define('STORE_ID', '1');
define('READ_FROM', date('Y-m-d 00:00:00', time()-14*24*60*60));
define('ENABLE_PRODUCT_REVIEWS', 1); //0: No product reviews, 1: Product reviews
define('TEST_MODE', 0); //0: No test mode, 1: test_mode
send_ping( 'getorders_start' );
$count=0;
$sql = "
SELECT orders.`entity_id`, `customer_firstname`, `customer_lastname`, `customer_email`, history.`created_at`
FROM
`sales_flat_order` as orders
JOIN
`sales_flat_order_status_history` as history ON orders.`entity_id` = `parent_id`
WHERE
orders.`status` IN (".ORDERS_STATUS.") and `store_id` IN (".STORE_ID.") AND history.status IN (".ORDERS_STATUS.") AND history.created_at >= '".READ_FROM."'";
$get = mysql_query($sql);
// Daten in Array speichern
while($fetch = mysql_fetch_assoc($get)) {
// Kundenname vorbereiten
$data[$fetch['entity_id']]['customer_first_name'] = $fetch['customer_firstname'];
$data[$fetch['entity_id']]['customer_last_name'] = $fetch['customer_lastname'];
$data[$fetch['entity_id']]['customer_email'] = $fetch['customer_email'];
$data[$fetch['entity_id']]['order_id'] = $fetch['entity_id'];
}
if (TEST_MODE){
echo '<pre>';
var_dump($data);
}
$num_orders = count( $data );
if ($num_orders ==0) {
write_log(201, 'no orders found', true);
}
else {
write_log(202, $num_orders .' orders found', true);
}
// build query and select customers ready to send
$sql = "SELECT `order_id` FROM `" . EKOMI_DB_TABLE . "` WHERE 1";
$get_existing_orders = mysql_query($sql) OR handle_mysql_error(__FILE__, __LINE__, $sql);
unset($sql);
// loop thru
while( $_existing_orders = mysql_fetch_assoc( $get_existing_orders ) ) {
// write to array
$existing_orders[] = trim($_existing_orders['order_id']);
}
echo "<b>" . $num_orders . ' orders found.</b><br />';
flush();
foreach ( $data AS $fetch_data) {
//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+//
// Kunde für den E-Mail Versand eintragen //
//+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+//
// Kunde schon eingetragen?
if ( !empty($fetch_data) && !in_array( $fetch_data['order_id'], $existing_orders)) {
if(ENABLE_PRODUCT_REVIEWS) {
// get & save product data
$sql = "
SELECT
sku, name
FROM
`sales_flat_order_item`
WHERE
order_id = '" . $fetch_data['order_id'] ."'";
$get_products = mysql_query($sql) OR handle_mysql_error(__FILE__, __LINE__, $sql);
// Daten in Array speichern
while($fetch_products = mysql_fetch_assoc($get_products)) {
$fetch_data['products'][$fetch_products['sku']] = str_replace(array(',', '"', "'", '|', "\r", "\n"), '', trim($fetch_products['name']));
}
}
if (TEST_MODE){
if (ENABLE_PRODUCT_REVIEWS){
echo '<pre>';
var_dump($fetch_data);
exit;
}
else exit;
}
if (stripos($fetch_data['customer_email'], 'marketplace.amazon.') !== FALSE || preg_match('~amazon@~', $fetch_data['customer_email'])){
write_log(106, 'Skipping amazon email address: '. $fetch_data['customer_email'], false);
}
else {
// Kunde eintragen
$sql = "
INSERT INTO
`" . EKOMI_DB_TABLE . "`
SET
`order_id` = '".mysql_real_escape_string($fetch_data['order_id'])."',
`first_name` = '".mysql_real_escape_string($fetch_data['customer_first_name'])."',
`last_name` = '".mysql_real_escape_string($fetch_data['customer_last_name'])."',
`email` = '".mysql_real_escape_string($fetch_data['customer_email'])."',
`product_ids` = '',
`product_names` = '".((count($fetch_data['products'])>0)?mysql_real_escape_string(serialize($fetch_data['products'])):'')."',
`time_read` = ".time();
// filter wrong email addresses
if ( !preg_match( '~[a-z0-9!#$%&\'*+/=?^_`{|}\~-]+(?:\.[a-z0-9!#$%&\'*+/=?^_`{|}\~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?~i', $fetch_data['customer_email'])) {
$sql .= ', `time_sent` = 0';
write_log(104, 'Skipping incorrect email address: '. $fetch_data['customer_email'], false);
}
// end filter
mysql_query($sql) OR handle_mysql_error(__FILE__, __LINE__, $sql);
$existing_orders[] = $fetch_data['order_id'];
write_log(200, 'order added: '.$fetch_data['order_id'], false);
$count++;
}
unset ($fetch_data['products'], $fetch_products);
}
}
// log found mails
write_log(203, $count . ' orders added', true);
send_ping( 'getorders_success' );
?>