-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct_status_update.module
285 lines (240 loc) · 7.96 KB
/
product_status_update.module
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
* Implements hook_help().
*
* Displays help and module information.
*
* @param path
* Which path of the site we're using to display help
* @param arg
* Array that holds the current path as returned from arg() function
*/
function product_status_update_help($path, $arg) {
switch ($path) {
case "admin/help#product_status_update":
return t("Check Status of Products on Vendor Site and Update Accordingly");
break;
}
}
/**
* Implements hook_menu().
*/
function product_status_update_menu() {
$items = array();
$items['admin/config/content/product_status_update'] = array(
'title' => 'Product Status Update',
'description' => 'Configuration and Administration for Product Status Update Module',
'page callback' => 'drupal_get_form',
'page arguments' => array('product_status_update_form'),
'access arguments' => array('access administration pages'),
'file' => 'product_status_update.admin.inc',
'type' => MENU_NORMAL_ITEM,
);
$items['admin/config/content/product_status_update/main'] = array(
'title' => t('Settings'),
'file' => 'product_status_update.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 10,
);
$items['admin/config/content/product_status_update/discontinued'] = array(
'title' => t('Discontinued'),
'description' => t('View/Delete Discontinued Items'),
'page callback' => 'drupal_get_form',
'page arguments' => array('product_status_update_discontinued'),
'access arguments' => array('access administration pages'),
'file' => 'product_status_update.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 20,
);
$items['admin/config/content/product_status_update/outofstock'] = array(
'title' => t('Out of Stock'),
'description' => t('View/Disable Out-of-Stock Items'),
'page callback' => 'drupal_get_form',
'page arguments' => array('product_status_update_outofstock'),
'access arguments' => array('access administration pages'),
'file' => 'product_status_update.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 30,
);
$items['admin/config/content/product_status_update/restocked'] = array(
'title' => t('Restocked'),
'description' => t('View/Enable Items that are Back in Stock'),
'page callback' => 'drupal_get_form',
'page arguments' => array('product_status_update_restocked'),
'access arguments' => array('access administration pages'),
'file' => 'product_status_update.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 40,
);
$items['admin/config/content/product_status_update/errorlog'] = array(
'title' => t('Error Log'),
'description' => t('Items that could not be processed'),
'page callback' => 'drupal_get_form',
'page arguments' => array('product_status_update_error_log'),
'access arguments' => array('access administration pages'),
'file' => 'product_status_update.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 50,
);
return $items;
}
/**
* Implements hook_cron_queue_info().
*/
function product_status_update_cron_queue_info() {
$queues['product_status_update'] = array(
'worker callback' => 'process_product_batch',
'time' => 150,
);
return $queues;
}
/**
* Implements hook_cron().
*/
function product_status_update_cron() {
$queue = DrupalQueue::get('product_status_update');
$unprocessed_count = db_query('SELECT COUNT(product_id) FROM {product_status_update} WHERE processed = 0')->fetchField();
if ($unprocessed_count > 0) {
for ($i = 0; $i < variable_get('psu_batches_per_run', 5); $i++) {
$queue->createItem($i);
}
}
else {
db_update('product_status_update')
->fields(array(
'processed' => 0,
)
)
->execute();
}
}
/**
*
* Process one batch of products--look up SKU numbers on vendor site
* and set database flags accordingly
*
*/
function process_product_batch($queue_item) {
$url = "https://qgold.com//ProductListings/SearchProducts";
$user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36";
$limit = variable_get('psu_batch_size', 20);
$result = db_query_range("SELECT product_id, sku, out_of_stock FROM {product_status_update} WHERE processed = 0", 0, $limit);
foreach ($result as $product) {
$post_string = 'searchTerms='.$product->sku;
$connection = curl_init($url);
curl_setopt($connection, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($connection, CURLOPT_USERAGENT, $user_agent);
curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($connection, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($connection, CURLOPT_POSTFIELDS, $post_string);
$result = curl_exec($connection);
curl_close($connection);
if (strpos ($result, '"IsDiscontinued":true') !== false) {
$updated = db_update('product_status_update')
->fields(array(
'discontinued' => 1,
'processed' => 1,
)
)
->condition('sku', $product->sku)
->execute();
}
else {
if (preg_match('/(?<="MSRP":)\d+(?=\.)/', $result, $msrpArr)) {
$msrp = $msrpArr[0];
if ($msrp <= 500) $perc = 85;
else if ($msrp < 2000) $perc = 80;
else $perc = 70;
$salePrice = $perc * $msrp;
$msrp *= 100;
$updated = db_update('field_data_field_regular_price_if_on_sale_')
->fields(array(
'field_regular_price_if_on_sale__amount' => $msrp
))
->condition('entity_id', $product->product_id)
->execute();
$updated = db_update('field_data_commerce_price')
->fields(array(
'commerce_price_amount' => $salePrice
))
->condition('entity_id', $product->product_id)
->execute();
}
if (strpos ($result, '"InStock":0') !== false && $product->out_of_stock != 3) {
$oos_flag = strpos($result, '"IsBuildable":true') !== false ? 2 : 1;
$updated = db_update('product_status_update')
->fields(array(
'out_of_stock' => $oos_flag,
'processed' => 1,
)
)
->condition('sku', $product->sku)
->execute();
}
else if (strpos ($result, 'no_products') !== false) {
$updated = db_delete('product_status_update')
->condition('sku', $product->sku)
->execute();
}
else if (strpos($result, 'product_detail') !== false) {
$updated = db_update('product_status_update')
->fields(array(
'out_of_stock' => 0,
'processed' => 1,
)
)
->condition('sku', $product->sku)
->execute();
}
else {
$updated = db_update('product_status_update')
->fields(array(
'processed' => 2,
)
)
->condition('sku', $product->sku)
->execute();
}
}
}
drupal_set_message(t('Test batch run successfully'));
}
/*
TODO: Rewrite install function to call this one
*/
function import_new_items() {
variable_set('psu_batch_size', '100');
variable_set('psu_batches_per_run', '5');
$max_imported_id = db_query('SELECT MAX(product_id) FROM {product_status_update}')->fetchField();
if (!$max_imported_id) {
$max_imported_id = 0;
}
$num_products = db_query('SELECT COUNT(DISTINCT product_id) FROM {commerce_product} WHERE product_id > '.$max_imported_id)->fetchField();
$cur_id = db_query('SELECT MIN(product_id) FROM {commerce_product} WHERE product_id > '.$max_imported_id)->fetchField() - 1;
$max_id = db_query('SELECT MAX(product_id) FROM {commerce_product} WHERE product_id > '.$max_imported_id)->fetchField();
while ($cur_id < $max_id) {
$products = db_select('commerce_product', 'cp')
->fields('cp', array('product_id', 'sku'))
->condition('product_id', $cur_id, '>')
->range(0, 50)
->orderBy('product_id', 'ASC')
->execute()->fetchAllAssoc('product_id', PDO::FETCH_ASSOC);
foreach ($products as $product) {
$pid = db_insert('product_status_update')
->fields(array(
'product_id' => $product['product_id'],
'sku' => $product['sku'],
'out_of_stock' => 0,
'discontinued' => 0,
'processed' => 0,
)
)
->execute();
$cur_id = $product['product_id'];
}
}
return t('New Inventory Imported Successfully');
}
function delete_old_items() {
}