-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductSync.php
More file actions
355 lines (282 loc) · 9.58 KB
/
Copy pathProductSync.php
File metadata and controls
355 lines (282 loc) · 9.58 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
<?php
/**
* Created by PhpStorm.
* User: fred
* Date: 9/18/14
* Time: 10:08 AM
*/
namespace H2\ShipCompliant;
use H2\ShipCompliant\API\ProductService;
use H2\ShipCompliant\API\ProductTypes;
use H2\ShipCompliant\Model\Product;
use H2\ShipCompliant\Model\ProductMapper;
use H2\ShipCompliant\Util\Logger;
use H2\ShipCompliant\Util\Session;
class ProductSync {
protected static $instance = null;
protected $service = null;
protected $created = 0;
protected $updated = 0;
protected $errors = array();
private function __construct() {
$this->service = new ProductService( Plugin::getInstance()->getSecurity() );
}
public static function init() {
$instance = static::get_instance();
add_action( 'save_post_product', array( $instance, 'save_post' ) );
add_action( 'admin_notices', array( $instance, 'post_edit_notices' ) );
add_action( 'wp_ajax_shipcompliant_get_products', array( $instance, 'ajax_get_products' ) );
add_action( 'wp_ajax_shipcompliant_import_products', array( $instance, 'ajax_import_products' ) );
add_action( 'wp_ajax_shipcompliant_export_products', array( $instance, 'ajax_export_products' ) );
}
public static function get_instance() {
if ( static::$instance === null ) {
static::$instance = new static();
}
return static::$instance;
}
public static function remove_actions() {
remove_action( 'save_post_product', array( static::get_instance(), 'save_post' ) );
}
public static function register_actions() {
add_action( 'save_post_product', array( static::get_instance(), 'save_post' ) );
}
public function ajax_get_products() {
$products = ProductMapper::get_shipcompliant_products();
for ( $i = 0; $i < count( $products ); $i ++ ) {
if ( ProductMapper::product_exists( $products[ $i ]->ProductKey ) ) {
$products[ $i ]->ExistsInWooCommerce = true;
} else {
$products[ $i ]->ExistsInWooCommerce = false;
}
}
header( 'Content-type: application/json' );
echo json_encode( $products );
exit;
}
private function add_or_update_product( $scp ) {
// if product exists
$id = Product::find_by_sku( $scp->ProductKey );
if ( $id ) {
// update it
$product = new Product( $id );
// update title if neccesary
if ( trim( $scp->Description ) != trim( $product->get_title() ) ) {
$product->set_title( $scp->Description );
}
$this->updated ++;
} else {
// create a new product
try {
$product = Product::create( $scp->Description, $scp->ProductKey );
$this->created ++;
}
catch( \Exception $ex ) {
$this->errors[] = array(
'message' => $ex->getMessage(),
'product_data' => $scp
);
return false;
}
}
$product->set_brand_key( $scp->BrandKey )
->set_product_type( $scp->ProductType )
->set_bottle_size( $scp->VolumeAmount )
->set_bottle_units( $scp->VolumeUnit )
->set_vintage( $scp->Vintage )
->set_alcohol_by_volume( $scp->PercentAlcohol )
->set_age( $scp->Age )
->set_price( $scp->DefaultRetailUnitPrice );
if ( isset( $scp->Varietal ) ) {
$product->set_varietal( $scp->Varietal );
}
if ( isset( $scp->Label ) && isset( $scp->Label->SerialNumber ) ) {
$product->set_serial_number( $scp->Label->SerialNumber );
}
return $product;
}
public function ajax_import_products() {
$this->remove_actions();
$products = ProductMapper::get_shipcompliant_products();
foreach ( $products as $scp ) {
$this->add_or_update_product( $scp );
}
$response = array(
'created' => $this->created,
'updated' => $this->updated,
'errors' => $this->errors
);
header( 'Content-type: application/json' );
echo json_encode( $response );
exit;
}
public function ajax_export_products() {
$posts = get_posts( array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => - 1 ) );
$response = (object) array(
'success' => false,
'count' => 0,
'messages' => array(),
'errors' => array()
);
try {
foreach ( $posts as $post ) {
$sc_response = $this->update_sc_product_from_db( $post->ID );
if ( ! empty( $sc_response ) && $sc_response->AddUpdateProductResult->ResponseStatus != "Success" ) {
$response->errors[] = array(
'product' => $post,
'response' => $sc_response->AddUpdateProductResult->Errors
);
} else {
$response->count ++;
}
}
$response->success = true;
$response->messages[] = sprintf( "Successfully updated %d products in ShipCompliant", $response->count );
}
catch( \Exception $ex ) {
$response->success = false;
$response->messages[] = "There was a problem exporting your products";
$response->messages[] = $ex->getMessage();
}
header( 'Content-type: application/json' );
echo json_encode( $response );
exit;
}
/**
* Sync a WooCommerce Product to ShipCompliant
*
* @param $post_id
*
* @return mixed
*/
public function update_sc_product_from_post( $post_id ) {
Logger::debug( 'Syncing WC Product with SC' );
$product = new Product( $post_id );
$product_type = $_POST['_product_type'];
$data = array(
"ProductType" => $product_type,
"Description" => $product->get_title(),
"ProductDistribution" => "Direct",
"ProductKey" => $_POST['_sku'],
"BrandKey" => $_POST['_brand_key'],
"UnitPrice" => $_POST['_regular_price'],
"DefaultRetailUnitPrice" => $_POST['_regular_price'],
"ShippingWeightInLbs" => $_POST['_weight'],
"ContainersPerSellingUnit" => 1,
"PercentAlcohol" => 0,
"VolumeAmount" => 0,
"VolumeUnit" => 'milliliter',
);
if ( ProductTypes::is_beverage( $product_type ) ) {
$data = array_merge( $data, array(
"ContainersPerSellingUnit" => $_POST['_bottles_per_sku'],
"PercentAlcohol" => $_POST['_alcohol_by_volume'],
"VolumeAmount" => $_POST['_bottle_size'],
"VolumeUnit" => $_POST['_bottle_units'],
) );
}
if ( ProductTypes::is_wine_type( $product_type ) ) {
$data['Varietal'] = $_POST['_varietal'];
$data['Vintage'] = $_POST['_vintage'];
}
if ( ProductTypes::is_beer_type( $product_type ) ) {
$request['Style'] = $_POST['_style'];
}
if ( ProductTypes::has_container( $product_type ) ) {
$data['ContainerType'] = $_POST['_container_type'];
}
if ( ProductTypes::is_spirit( $product_type ) ) {
$data['Flavor'] = $_POST['_flavor'];
$data['Age'] = $_POST['_age'];
}
$response = $this->service->AddUpdateProduct( array(
'Product' => $data,
"UpdateMode" => "UpdateExisting"
) );
return $response;
}
public function update_sc_product_from_db( $post_id ) {
set_time_limit( 0 );
Logger::debug( 'Syncing WC Product with SC' );
$product = new Product( $post_id );
$data = array(
"ProductType" => $product->get_product_type(),
"Description" => $product->get_title(),
"BrandKey" => $product->get_brand_key(),
"ProductDistribution" => "Direct",
"ProductKey" => $product->get_sku(),
"UnitPrice" => $product->get_price(),
"DefaultRetailUnitPrice" => $product->get_price(),
"ShippingWeightInLbs" => $product->get_weight(),
"ContainersPerSellingUnit" => 1,
"PercentAlcohol" => 0,
"VolumeAmount" => 0,
"VolumeUnit" => 'milliliter',
);
if ( $product->is_beverage() ) {
$data = array_merge( $data, array(
"PercentAlcohol" => $product->get_alcohol_by_volume(),
"VolumeAmount" => $product->get_bottle_size(),
"VolumeUnit" => $product->get_bottle_units(),
'ContainersPerSellingUnit' => $product->get_bottles_per_sku()
) );
}
if ( $product->is_wine() ) {
$data['Varietal'] = $product->get_varietal();
$data['Vintage'] = $product->get_vintage();
}
if ( $product->is_beer() ) {
$data['Style'] = $product->get_style();
}
if ( $product->is_spirit() ) {
$data['Flavor'] = $product->get_flavor();
$data['Age'] = $product->get_age();
}
if ( $product->has_container() ) {
$data['ContainerType'] = $product->get_container_type();
}
$response = $this->service->AddUpdateProduct( array(
'Product' => $data,
"UpdateMode" => "UpdateExisting"
) );
return $response;
}
public function save_post( $post_id ) {
// if this is a revision, get the real post id
if ( $parent_id = wp_is_post_revision( $post_id ) ) {
$post_id = $parent_id;
}
// make sure post status isnt "trash" and we're not doing autosave
if ( 'trash' != get_post_status( $post_id ) && ! ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) ) {
$response = $this->update_sc_product_from_post( $post_id );
if ( ! empty( $response ) && $response->AddUpdateProductResult->ResponseStatus != "Success" ) {
$errors = $response->AddUpdateProductResult->Errors->Error;
Session::set( 'post_edit_messages', $errors );
return false;
}
return $post_id;
}
return false;
}
function post_edit_notices() {
if ( Session::has( 'post_edit_messages' ) ) {
$messages = Session::get( 'post_edit_messages' );
//var_dump( $messages );
if ( ! empty( $messages ) ): ?>
<div class="error">
<p> Error syncing product with ShipCompliant:</p>
<?php if(is_array($messages)): ?>
<ul>
<?php foreach ( $messages as $error ): ?>
<li><strong><?php echo $error->Message; ?></strong></li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p><?php echo $messages->Message; ?></p>
<?php endif; ?>
</div>
<?php endif;
}
Session::remove( 'post_edit_messages' );
}
}