AffinityEcommerceHooks.php
14.3 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of AffinityEcommerceHooks
*
* @author mlacava
*/
class AffinityEcommerceHooks {
public function initHooks() {
//Product and Order Hooks
add_action('wp_insert_post', array($this, 'wpPostAfterSaved'), 999, 3);
add_action('wp_trash_post', array($this, 'woo_trash'));
add_action('woocommerce_product_options_general_product_data', array($this, 'woo_add_desc'));
add_action('woocommerce_process_product_meta', array($this, 'woo_save_desc'));
add_action('woocommerce_product_after_variable_attributes', array($this, 'woo_add_var'), 10, 3);
add_action('woocommerce_save_product_variation', array($this, 'woo_save_var'), 10, 1);
add_action('woocommerce_duplicate_product', array($this, 'woo_dupe'));
add_action('woocommerce_admin_order_data_after_shipping_address', array($this, 'woo_add_order_fields'));
add_action('restrict_manage_posts', array($this, 'woo_add_filter'), 50);
add_action('posts_where', array($this, 'woo_order_filter'));
add_filter('manage_edit-shop_order_columns', array($this, 'woo_order_column'));
add_action('manage_shop_order_posts_custom_column' , array($this, 'woo_order_dat'), 10, 2);
add_action('admin_notices', array($this, 'woo_notice'));
add_action('woocommerce_payment_complete', array($this, 'woo_order_handler'), 10, 1);
add_action('pmxi_before_xml_import', array($this, 'before_xml_import'), 10, 1);
add_action('pmxi_after_xml_import', array($this, 'after_xml_import'), 10, 1);
add_action('pmxi_saved_post', array($this, 'post_saved'), 10, 1);
add_action('admin_notices', array($this, 'woo_admin_notices'));
}
public function woo_admin_notices() {
if ((!empty($_GET['page'])) && $_GET['page'] === 'ebay-sync-settings' && empty($_GET['pnum'])) {
return;
}
$url = 'admin.php?page=ebay-sync-settings';
$setup_ebayuserid = get_option('ebayaffinity_ebayuserid');
$setup_token = get_option('affinityPushAccessToken');
if (empty($setup_ebayuserid) || empty($setup_token)) {
echo '<div class="updated fade"><p>' . sprintf( __( '%seBay Sync is almost ready.%s To get started, %sconnect your eBay account%s.', 'woocommerce-ebay-integration' ), '<strong>', '</strong>', '<a href="' . esc_url( $url ) . '">', '</a>' ) . '</p></div>' . "\n";
}
}
function before_xml_import($import_id) {
global $wpdb;
$wpdb->query("DROP TABLE IF EXISTS ".$wpdb->prefix."ebayaffinity_postmeta_bak");
$wpdb->query("CREATE TABLE ".$wpdb->prefix."ebayaffinity_postmeta_bak LIKE ".$wpdb->prefix."postmeta");
$wpdb->query("INSERT INTO ".$wpdb->prefix."ebayaffinity_postmeta_bak SELECT DISTINCT pm.* FROM ".$wpdb->prefix."posts AS p, ".$wpdb->prefix."postmeta AS pm WHERE p.post_type IN ('product', 'product_variation') AND
p.id = pm.post_id AND pm.meta_key IN ('_affinity_suggestedCatId', '_affinity_ebaycategory', '_affinity_ebayitemid', '_affinity_item_id', '_affinity_prod_all_active_variants',
'_affinity_prod_all_variants', '_affinity_prod_arr_adaptation_errors', '_affinity_prod_arr_adaptation_warnings','_affinity_prod_arr_client_errors',
'_affinity_prod_arr_client_warnings', '_affinity_prod_ebay_sku', '_affinity_prod_last_successful_update', '_affinity_prod_update_failure_count',
'_affinity_prod_update_status', '_affinity_shiprule',
'_affinity_titlerule', '_affinity_titleopt', '_affinity_block', '_affinity_ebaydesc', '_affinity_ebayprice', '_ebayprice', '_ebayuseshort', '_ebaytemplate', '_ebaydesc')");
}
function post_saved($id) {
global $wpdb;
$wpdb->query("INSERT IGNORE INTO ".$wpdb->prefix."postmeta SELECT * FROM ".$wpdb->prefix."ebayaffinity_postmeta_bak WHERE post_id = ".intval($id));
}
function after_xml_import($import_id) {
global $wpdb;
$wpdb->query("DROP TABLE IF EXISTS ".$wpdb->prefix."ebayaffinity_postmeta_bak");
}
function woo_order_handler($order_id) {
require_once(__DIR__ . "/AffinityEcommerceProduct.php");
$order = new WC_Order($order_id);
$items = $order->get_items();
foreach ($items as $item) {
$post = get_post($item['product_id']);
AffinityEcommerceProduct::productHasChanged($post, false);
}
}
function woo_notice() {
$ordererror = get_option('_affinity_order_error');
if (!empty($ordererror)) {
print '<div class="notice notice-error"><p>'.htmlspecialchars($ordererror).'</p></div>';
delete_option('_affinity_order_error');
}
}
function woo_dupe($new_id, $post) {
delete_post_meta($new_id, '_affinity_ebayitemid');
update_post_meta($new_id, '_affinity_prod_update_status', 1);
delete_post_meta($new_id, '_affinity_prod_ebay_sku');
delete_post_meta($new_id, '_affinity_prod_arr_client_warnings');
delete_post_meta($new_id, '_affinity_prod_arr_client_errors');
delete_post_meta($new_id, '_affinity_prod_arr_adaptation_warnings');
delete_post_meta($new_id, '_affinity_prod_arr_adaptation_errors');
delete_post_meta($new_id, '_affinity_prod_update_failure_count');
delete_post_meta($new_id, '_affinity_prod_update_failure_count');
delete_post_meta($new_id, '_affinity_prod_active_variants');
delete_post_meta($new_id, '_affinity_prod_all_active_variants');
delete_post_meta($new_id, '_affinity_prod_all_inactive_variants');
delete_post_meta($new_id, '_affinity_prod_all_variants');
delete_post_meta($new_id, '_affinity_prod_active_variants');
delete_post_meta($new_id, '_affinity_prod_inactive_variants');
$variationloop = new WP_Query(array('post_type' => 'product_variation', 'post_parent' => $new_id));
while ($variationloop->have_posts()) {
$variationloop->the_post();
$new_id = get_the_ID();
delete_post_meta($new_id, '_affinity_ebayitemid');
delete_post_meta($new_id, '_affinity_prod_ebay_sku');
delete_post_meta($new_id, '_affinity_prod_arr_client_warnings');
delete_post_meta($new_id, '_affinity_prod_arr_client_errors');
delete_post_meta($new_id, '_affinity_prod_arr_adaptation_warnings');
delete_post_meta($new_id, '_affinity_prod_arr_adaptation_errors');
delete_post_meta($new_id, '_affinity_prod_update_failure_count');
delete_post_meta($new_id, '_affinity_prod_update_failure_count');
delete_post_meta($new_id, '_affinity_prod_active_variants');
delete_post_meta($new_id, '_affinity_prod_all_active_variants');
delete_post_meta($new_id, '_affinity_prod_all_inactive_variants');
delete_post_meta($new_id, '_affinity_prod_all_variants');
delete_post_meta($new_id, '_affinity_prod_active_variants');
delete_post_meta($new_id, '_affinity_prod_inactive_variants');
}
}
public function wpPostAfterSaved($postId, $post, $isAnUpdate) {
$postType = $post->post_type;
if($postType === "product") {
require_once(__DIR__ . "/AffinityEcommerceProduct.php");
AffinityEcommerceProduct::productHasChanged($post, !empty($_POST['syncit']));
return;
}
if($postType === "shop_order") {
require_once(__DIR__ . "/AffinityEcommerceOrder.php");
AffinityEcommerceOrder::orderHasChanged($post);
require_once(__DIR__ . "/AffinityEcommerceProduct.php");
$order = new WC_Order($postId);
$items = $order->get_items();
foreach ($items as $item) {
$post = get_post($item['product_id']);
if ($post instanceof WP_Post) {
AffinityEcommerceProduct::productHasChanged($post, false);
}
}
}
}
function woo_trash($id) {
require_once(__DIR__.'/../model/AffinityEbayInventory.php');
update_post_meta($id, '_affinity_prod_update_status', '1');
}
function woo_add_var($loop, $variation_data, $variation) {
woocommerce_wp_text_input(
array(
'id' => '_ebayprices[' . $variation->ID . ']',
'label' => 'eBay Price: ('.get_woocommerce_currency_symbol().')',
'value' => get_post_meta($variation->ID, '_ebayprice', true),
'data_type' => 'price'
)
);
}
function woo_save_var($post_id) {
foreach ($_POST['_ebayprices'] as $k=>$v) {
if (!empty($v)) {
update_post_meta($k, '_ebayprice', stripslashes($v));
} else {
delete_post_meta($k, '_ebayprice');
}
}
if (!empty($_POST['product_id'])) {
$variationloop = new WP_Query(array('post_type' => 'product_variation', 'post_parent' => $_POST['product_id']));
$max = 0;
while ($variationloop->have_posts()) {
$variationloop->the_post();
$a = get_post_meta(get_the_ID(), '_ebayprice', true);
if ($a > $max) {
$max = $a;
}
}
if (!empty($max)) {
update_post_meta($_POST['product_id'], '_ebayprice', $max);
} else {
delete_post_meta($_POST['product_id'], '_ebayprice');
}
}
}
function woo_add_desc() {
global $woocommerce, $post;
echo '';
woocommerce_wp_textarea_input(
array(
'id' => '_ebaydesc',
'label' => __('eBay Description', 'woocommerce'),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __('eBay Description', 'woocommerce')
)
);
$ebaytemplate = get_post_meta($post->ID, '_ebaytemplate', true);
if (strlen($ebaytemplate) === 0) {
update_post_meta($post->ID, '_ebaytemplate', 1);
}
woocommerce_wp_select(
array(
'id' => '_ebaycondition',
'label' => 'eBay Condition *',
'options' => array(
'NEW' => __('New', 'woocommerce'),
'NEW_OTHER' => __('New other (category specific)', 'woocommerce'),
'MANUFACTURER_REFURBISHED' => __('Manufacturer refurbished', 'woocommerce'),
'SELLER_REFURBISHED' => __('Seller refurbished', 'woocommerce'),
'USED_EXCELLENT' => __('Used', 'woocommerce'),
'FOR_PARTS_OR_NOT_WORKING' => __('For parts or not working', 'woocommerce')
)
)
);
woocommerce_wp_text_input(
array(
'id' => '_ebayprice',
'label' => 'eBay Price ('.get_woocommerce_currency_symbol().')',
'data_type' => 'price'
)
);
woocommerce_wp_checkbox(
array(
'id' => '_ebaytemplate',
'label' => __('Use eBay/Custom Template', 'woocommerce'),
'cbvalue' => 1
)
);
$useshort = get_option('ebayaffinity_useshort');
if (empty($useshort)) {
woocommerce_wp_checkbox(
array(
'id' => '_ebayuseshort',
'label' => __('Use short description for eBay', 'woocommerce'),
'cbvalue' => 1
)
);
} else {
woocommerce_wp_checkbox(
array(
'id' => '_ebayuseshort',
'label' => __('Use short description for eBay', 'woocommerce'),
'cbvalue' => 5000
)
);
}
}
function woo_add_order_fields() {
woocommerce_wp_checkbox(
array(
'id' => '_affinity_marked_as_sent',
'wrapper_class' => 'show_if_simple',
'label' => __('', 'woocommerce' ),
'description' => __( 'Mark as sent', 'woocommerce' )
)
);
woocommerce_wp_text_input(
array(
'id' => '_affinity_tracking_number',
'label' => __('Tracking Number', 'woocommerce'),
'placeholder' => '# Tracking Number',
)
);
woocommerce_wp_select(
array(
'id' => '_affinity_carrier_name',
'label' => __( 'Carrier Name', 'woocommerce' ),
'options' => array(
'' => __( '', 'woocommerce' ),
'australiapost' => __( 'Australia Post', 'woocommerce' ),
'toll' => __( 'Toll', 'woocommerce' ),
'couriersplease' => __( 'Couriers Please', 'woocommerce' ),
'fastway' => __( 'Fastway', 'woocommerce' ),
'dhl' => __( 'DHL', 'woocommerce' ),
'startrack' => __( 'StarTrack', 'woocommerce' ),
'temando' => __( 'Temando', 'woocommerce' ),
'allied' => __( 'Allied Express', 'woocommerce' ),
'tnt' => __( 'TNT', 'woocommerce' ),
'packsend' => __( 'Pack & Send', 'woocommerce' ),
'smartsend' => __( 'SmartSend', 'woocommerce' ),
'ego' => __( 'e-go', 'woocommerce' ),
'fedex' => __( 'FedEx', 'woocommerce' ),
'other' => __( 'Other', 'woocommerce' )
)
)
);
}
function woo_save_desc($post_id) {
delete_post_meta($post_id, '_affinity_suggestedCatId');
if ((!empty($_POST['ebaydesc'])) && (!empty($_POST['affinity-copy']))) {
update_post_meta($post_id, '_ebaydesc', stripslashes($_POST['ebaydesc']));
} else {
delete_post_meta($post_id, '_ebaydesc');
}
if (empty($_POST['_ebayprices'])) {
if ((!empty($_POST['_ebayprice'])) && (!empty($_POST['_ebayprice']))) {
update_post_meta($post_id, '_ebayprice', stripslashes($_POST['_ebayprice']));
} else {
delete_post_meta($post_id, '_ebayprice');
}
}
if (!empty($_POST['_ebaytemplate'])) {
update_post_meta($post_id, '_ebaytemplate', 1);
} else {
update_post_meta($post_id, '_ebaytemplate', 0);
}
if (!empty($_POST['_ebaycondition'])) {
update_post_meta($post_id, '_ebaycondition', $_POST['_ebaycondition']);
} else {
delete_post_meta($post_id, '_ebaycondition');
}
$useshort = get_option('ebayaffinity_useshort');
if (empty($useshort)) {
if (!empty($_POST['_ebayuseshort'])) {
update_post_meta($post_id, '_ebayuseshort', 1);
} else {
update_post_meta($post_id, '_ebayuseshort', 0);
}
}
}
function woo_add_filter() {
global $typenow;
if ($typenow === 'shop_order') {
?>
<input type="checkbox" name="affinity_ebayorders" id="affinity_ebayorders" style="margin-left: 4px; margin-top: 7px;" <?php print empty($_GET['affinity_ebayorders'])?'':'checked'?> value="1">
<label style="padding-top: 7px; margin-right: 4px;" for="affinity_ebayorders">eBay orders only</label>
<?php
}
}
function woo_order_filter($where) {
if (is_search()) {
if (!empty($_GET['affinity_ebayorders'])) {
global $wpdb;
$where .= " AND ".$wpdb->prefix."posts.id IN (SELECT post_id FROM ".$wpdb->prefix."postmeta WHERE ".$wpdb->prefix."postmeta.meta_key = '_affinity_ebayorder')";
}
}
return $where;
}
function woo_order_column($columns) {
$asset_dir = plugins_url('/../assets/', __FILE__);
$columns = array_slice($columns, 0, 3, true) + array('_affinity_ebayorder' => '<img src="'.htmlspecialchars($asset_dir).'ebay.png" alt="eBay order" title="eBay order">') + array_slice($columns, 3, count($columns) - 1, true);
return $columns;
}
function woo_order_dat($column) {
global $post, $woocommerce, $the_order;
switch ($column) {
case '_affinity_ebayorder':
$a = get_post_meta($the_order->id, '_affinity_ebayorder', true);
if (!empty($a)) {
$asset_dir = plugins_url('/../assets/', __FILE__);
?>
<img src="<?php print htmlspecialchars($asset_dir).'ebay.png'?>" alt="eBay order" title="eBay order">
<?php
}
break;
}
}
}