PluginProbe
MailerLite – WooCommerce integration / 2.0.7
MailerLite – WooCommerce integration v2.0.7
3.1.27 3.1.25 3.1.26 3.1.24 3.1.23 3.1.22 3.1.21 3.1.20 3.1.19 3.1.18 3.1.17 3.1.16 3.1.15 1.8.7 1.8.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 All 148 releases
woo-mailerlite / includes / shared / api / class.woo-mailerlite-api.php

class.woo-mailerlite-api.php in MailerLite – WooCommerce integration 2.0.7, at includes/shared/api/class.woo-mailerlite-api.php

1,281 lines 30.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace MailerLite\Includes\Shared\Api;
4
5 class MailerLiteAPI
6 {
7
8 private $url = 'https://connect.mailerlite.com/api';
9 private $client;
10 private $response;
11 private $response_code;
12
13 /**
14 * MailerLiteAPI constructor
15 *
16 * @access public
17 * @return void
18 * @since 1.6.0
19 */
20 public function __construct($api_key)
21 {
22 $this->client = new MailerLiteClient($this->url, [
23 'Authorization' => 'Bearer ' . $api_key,
24 'Content-Type' => 'application/json',
25 'Accept' => 'application/json',
26 'X-Version' => '2022-11-21',
27 ]);
28 }
29
30 /**
31 * Validate API Key
32 *
33 * @access public
34 * @return mixed
35 * @since 1.6.0
36 */
37 public function validateKey()
38 {
39 $response = $this->client->remote_get('/account');
40
41 $response = self::parseResponse($response);
42
43 return $response->data ?? false;
44 }
45
46 /**
47 * Update subscriber
48 *
49 * @access public
50 * @return mixed
51 * @since 1.6.0
52 */
53 public function updateSubscriber($subscriber_email, $subscriber_data)
54 {
55
56 // TODO - implement update subscriber
57 }
58
59 /**
60 * Update subscriber status
61 *
62 * @access public
63 * @return mixed
64 * @since 1.7.4
65 */
66 public function updateSubscriberStatus($email, $status)
67 {
68
69 $response = $this->client->remote_post('/subscribers', [
70 'email' => $email,
71 'status' => $status
72 ]);
73
74 $response = self::parseResponse($response);
75
76 return $response->data ?? false;
77 }
78
79 /**
80 * Search for a subscriber
81 *
82 * @access public
83 * @return mixed
84 * @since 1.8.5
85 */
86 public function searchSubscriber($query)
87 {
88 $response = $this->client->remote_get('/subscribers', $query);
89 $response = self::parseResponse($response);
90
91 return $response->data ?? false;
92 }
93
94 /**
95 * Get groups
96 *
97 * @access public
98 * @return array[]
99 * @since 1.6.0
100 */
101 public function getGroups($params)
102 {
103 $response = $this->client->remote_get('/groups', $params);
104 $response = self::parseResponse($response);
105
106 $groups = [];
107
108 if (isset($response->data)) {
109
110 foreach ($response->data as $record) {
111
112 $group = [];
113 $group['id'] = $record->id;
114 $group['name'] = $record->name;
115 $group['total'] = $record->active_count;
116 $group['opened'] = $record->open_rate->float;
117 $group['clicked'] = $record->click_rate->float;
118 $group['date_created'] = $record->created_at;
119
120 $groups[] = $group;
121 }
122 }
123
124 return $groups;
125 }
126
127 /**
128 * Check if more groups need to be loaded
129 *
130 * @access public
131 * @return mixed
132 * @since 1.6.0
133 */
134 public function checkMoreGroups($limit, $offset)
135 {
136
137 $response = $this->client->remote_get('/groups', [
138 'limit' => $limit,
139 'page' => $offset
140 ]);
141
142 $response = self::parseResponse($response);
143
144 return count($response->data ?? []) > 0;
145 }
146
147 /**
148 * Get more groups
149 *
150 * @access public
151 * @return mixed
152 * @since 1.6.0
153 */
154 public function getMoreGroups($limit, $offset)
155 {
156
157 $response = $this->client->remote_get('/groups', [
158 'limit' => $limit,
159 'page' => $offset
160 ]);
161
162 $response = self::parseResponse($response);
163
164 return $response->data ?? [];
165 }
166
167 /**
168 * Get Double opt-in setting
169 *
170 * @access public
171 * @return bool
172 * @since 1.6.0
173 */
174 public function getDoubleOptin()
175 {
176 $response = $this->client->remote_get('/subscribe-settings/double-optin');
177
178 if (isset($response)) {
179 $response = self::parseResponse($response);
180
181 if (isset($response->double_optin) && $response->double_optin) {
182
183 return true;
184 }
185 }
186
187 return false;
188 }
189
190 /**
191 * Toggle Double opt-in setting
192 *
193 * @access public
194 * @return bool
195 * @since 1.6.0
196 */
197 public function setDoubleOptin()
198 {
199 $this->client->remote_post('/subscribe-settings/double-optin/toggle');
200
201 return $this->getDoubleOptin();
202 }
203
204 /**
205 * Get Fields
206 *
207 * @access public
208 *
209 * @param $params
210 *
211 * @return array[]
212 * @since 1.6.0
213 */
214 public function getFields($params): array
215 {
216 $response = $this->client->remote_get('/fields', $params);
217 $response = self::parseResponse($response);
218
219 $fields = [];
220
221 if (isset($response->data)) {
222 foreach ($response->data as $field) {
223 $ml_field = [];
224 $ml_field['id'] = $field->id;
225 $ml_field['title'] = $field->name;
226 $ml_field['key'] = $field->key;
227 $ml_field['type'] = $field->type;
228
229 $fields[] = $ml_field;
230 }
231 }
232
233 return $fields;
234 }
235
236 /**
237 * Create Custom Field
238 *
239 * @access public
240 * @return mixed
241 * @since 1.6.0
242 */
243 public function createField($title, $type)
244 {
245
246 $response = $this->client->remote_post('/fields', [
247 'name' => $title,
248 'type' => $type
249 ]);
250
251 $response = self::parseResponse($response);
252
253 return $response->data ?? false;
254 }
255
256 /**
257 * Update Custom Field
258 *
259 * @access public
260 * @return mixed
261 * @since 1.7.11
262 */
263 public function updateField($id, $name)
264 {
265
266 $response = $this->client->remote_put('/fields/' . $id, [
267 'name' => $name
268 ]);
269
270 return self::parseResponse($response);
271 }
272
273 /**
274 * Sync WooCommerce Customer
275 *
276 * @access public
277 * @return mixed
278 * @since 1.6.0
279 */
280 public function syncCustomerWooCommerce($customer_id, $email, $fields, $shop_id)
281 {
282
283 if ($customer_id !== '0') {
284
285 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/customers', [
286 'email' => $email,
287 'accepts_marketing' => true, // TODO
288 'total_spent' => 0,
289 'create_subscriber' => true // TODO
290 ]);
291 } else {
292
293 $response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/customers/' . $customer_id, [
294 'email' => $email,
295 'accepts_marketing' => true, // TODO
296 'total_spent' => 0,
297 'create_subscriber' => true // TODO
298 ]);
299 }
300
301 $response = self::parseResponse($response);
302
303 return $response->data;
304 }
305
306 /**
307 * Set WooCommerce Consumer Data
308 *
309 * @access public
310 * @return mixed
311 * @since 1.6.0
312 */
313 public function setConsumerData(
314 $consumerKey,
315 $consumerSecret,
316 $store,
317 $currency,
318 $group_id,
319 $resubscribe,
320 $ignoreList,
321 $create_segments,
322 $shop_name,
323 $shop_id,
324 $popups_enabled
325 ) {
326
327 if ($shop_id !== false) {
328
329 $response = $this->client->remote_put('/ecommerce/shops/' . $shop_id, [
330 'name' => $shop_name,
331 'url' => $store,
332 'currency' => $currency,
333 'platform' => 'woocommerce',
334 'group_id' => $group_id,
335 'enable_popups' => $popups_enabled,
336 'enable_resubscribe' => $resubscribe,
337 'enabled' => true,
338 'access_data' => '-'
339 ]);
340 } else {
341
342 $response = $this->client->remote_post('/ecommerce/shops', [
343 'name' => $shop_name,
344 'url' => $store,
345 'currency' => $currency,
346 'platform' => 'woocommerce',
347 'group_id' => $group_id,
348 'enable_popups' => $popups_enabled,
349 'enable_resubscribe' => $resubscribe,
350 'enabled' => true,
351 'access_data' => '-'
352 ]);
353 }
354
355 $response = self::parseResponse($response);
356
357 return $response->data ?? false;
358 }
359
360 /**
361 * Send WooCommerce Subscriber Data
362 *
363 * @access public
364 * @return mixed
365 * @since 1.6.0
366 */
367 public function sendSubscriberData($shop_id, $data)
368 {
369
370 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/customers', [
371 'email' => $data['email'],
372 'accepts_marketing' => $data['checked_sub_to_mailist'],
373 'total_spent' => 0,
374 'create_subscriber' => $data['checked_sub_to_mailist']
375 ]);
376
377 $response = self::parseResponse($response);
378
379 return $response->data;
380 }
381
382 /**
383 * Save WooCommerce Order
384 *
385 * @access public
386 * @return mixed
387 * @since 1.6.0
388 */
389 public function saveOrder($shop_id, $orderData)
390 {
391
392 return true;
393 }
394
395 /**
396 * Send WooCommerce Order Processing
397 *
398 * @access public
399 * @return mixed
400 * @since 1.6.0
401 */
402 public function sendOrderProcessing($shop_id, $data)
403 {
404
405 $order_id = $data['order_id'];
406
407 $response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/orders/' . $order_id, [
408 'status' => 'processing'
409 ]);
410
411 $response = self::parseResponse($response);
412
413 return $response->data;
414 }
415
416 /**
417 * Update WooCommerce Cart
418 *
419 * @access public
420 * @return mixed
421 * @since 1.6.0
422 */
423 public function sendCart($shop_id, $cartData)
424 {
425
426 $response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/carts/' . $cartData['cart_id'], [
427 'checkout_url' => $cartData['abandoned_checkout_url'],
428 'cart_total' => 0
429 ]);
430
431 $response = self::parseResponse($response);
432
433 return $response->data;
434 }
435
436 /**
437 * Toggle WooCommerce Shop
438 *
439 * @access public
440 *
441 * @param $shop
442 * @param $activeState
443 * @param $shop_id
444 * @param $shop_name
445 *
446 * @return mixed
447 * @since 1.6.0
448 */
449 public function toggleShop($shop, $activeState, $shop_id, $shop_name)
450 {
451
452 $response = $this->client->remote_put('/ecommerce/shops/' . $shop_id, [
453 'name' => $shop_name,
454 'url' => $shop,
455 'enabled' => $activeState
456 ]);
457
458 $response = self::parseResponse($response);
459
460 return $response->data;
461 }
462
463 /**
464 * Get WooCommerce Shop Settings
465 *
466 * @access public
467 *
468 * @param $shop_id
469 *
470 * @return mixed
471 * @since 1.6.0
472 */
473 public function getShopSettings($shop_id)
474 {
475
476 $response = $this->client->remote_get('/ecommerce/shops/' . $shop_id);
477
478 $response = self::parseResponse($response);
479
480 return $response->data ?? false;
481 }
482
483 /**
484 * Validate WooCommerce Account
485 *
486 * @access public
487 * @return mixed
488 * @since 1.6.0
489 */
490 public function validateAccount()
491 {
492
493 $response = $this->client->remote_get('/account');
494
495 $response = self::parseResponse($response);
496
497 return $response->data ?? false;
498 }
499
500 /**
501 * Get list of shops
502 *
503 * @access public
504 * @return array
505 * @since 1.6.0
506 */
507 public function getShops()
508 {
509
510 $response = $this->client->remote_get('/ecommerce/shops');
511
512 $response = self::parseResponse($response);
513
514 return $response->data ?? [];
515 }
516
517 /**
518 * Get a shop
519 *
520 * @access public
521 * @return mixed
522 * @since 1.6.0
523 */
524 public function getShop($shop_id)
525 {
526
527 $response = $this->client->remote_get('/ecommerce/shops/' . $shop_id);
528
529 $response = self::parseResponse($response);
530
531 return $response->data ?? false;
532 }
533
534 /**
535 * Create shop
536 *
537 * @access public
538 *
539 * @param $name
540 * @param $url
541 * @param $currency
542 * @param $group_id
543 * @param $enable_popups
544 * @param $access_data
545 * @param $enable_resubscribe
546 *
547 * @return mixed
548 * @since 1.6.0
549 */
550 public function createShop($name, $url, $currency, $group_id, $enable_popups, $access_data, $enable_resubscribe)
551 {
552 $response = $this->client->remote_post('/ecommerce/shops', [
553 'name' => $name,
554 'url' => $url,
555 'currency' => $currency,
556 'platform' => 'woocommerce',
557 'group_id' => $group_id,
558 'enable_popups' => $enable_popups,
559 'enable_resubscribe' => $enable_resubscribe,
560 'enabled' => true,
561 'access_data' => $access_data
562 ]);
563
564 $response = self::parseResponse($response);
565
566 return $response->data ?? false;
567 }
568
569 /**
570 * Delete shop
571 *
572 * @access public
573 *
574 * @param $shop
575 *
576 * @return bool
577 * @since 1.6.0
578 */
579 public function deleteshop($shop)
580 {
581 $response = $this->client->remote_delete('/ecommerce/shops/' . $shop);
582
583 $response = self::parseResponse($response);
584
585 return $response->data ?? false;
586 }
587
588 /**
589 * Fetch order
590 *
591 * @access public
592 *
593 * @param $shop_id
594 * @param $order_id
595 *
596 * @return mixed
597 * @since 1.6.0
598 */
599 public function fetchOrder($shop_id, $order_id)
600 {
601
602 $response = $this->client->remote_get('/ecommerce/shops/' . $shop_id . '/orders/' . $order_id . '?with_resource_id');
603
604 $response = self::parseResponse($response);
605
606 return $response->data ?? false;
607 }
608
609 /**
610 * Import orders
611 *
612 * @access public
613 *
614 * @param $shop_id
615 * @param $orders
616 *
617 * @return array
618 * @since 1.6.0
619 */
620 public function importOrders($shop_id, $orders)
621 {
622
623 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/orders/import?with_resource_id', [
624 'orders' => $orders
625 ]);
626
627 $response = self::parseResponse($response);
628
629 return $response->data ?? [];
630 }
631
632 /**
633 * Import customers
634 *
635 * @access public
636 *
637 * @param $shop_id
638 * @param $customers
639 *
640 * @return array
641 * @since 1.8.5
642 */
643 public function importCustomers($shop_id, $customers)
644 {
645
646 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/customers/import?with_resource_id',
647 $customers);
648
649 $response = self::parseResponse($response);
650
651 return $response->data ?? [];
652 }
653
654 /**
655 * Sync order
656 *
657 * @access public
658 *
659 * @param $shop_id
660 * @param $order_id
661 * @param $customer
662 * @param $cart
663 * @param $status
664 * @param $total_price
665 * @param $created_at
666 *
667 * @return mixed
668 * @since 1.6.0
669 */
670 public function syncOrder($shop_id, $order_id, $customer, $cart, $status, $total_price, $created_at)
671 {
672
673 $order_status = 'pending';
674
675 if ($status == 'completed' || $status == 'processing') {
676 if (isset($customer['resource_id']) && (int)$customer['resource_id']) {
677 $lastSyncedCustomer = get_option('woo_ml_last_synced_customer', 0);
678 if (($lastSyncedCustomer > 0) && ($lastSyncedCustomer < $customer['resource_id'])) {
679 update_option('woo_ml_last_synced_customer', $customer['resource_id']);
680 }
681 }
682 $order_status = 'complete';
683 }
684
685 if ( ! isset($cart['resource_id'])) {
686 $cart['resource_id'] = (string)$order_id;
687 }
688
689 if ( ! isset($cart['cart_total'])) {
690 $cart['cart_total'] = $total_price;
691 }
692
693 $parameters = [
694 'resource_id' => (string)$order_id,
695 'customer' => $customer,
696 'cart' => $cart,
697 'status' => $order_status,
698 'total_price' => $total_price,
699 'created_at' => $created_at
700 ];
701
702 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/orders?with_resource_id',
703 $parameters);
704
705 $response = self::parseResponse($response);
706
707 return $response->data ?? false;
708 }
709
710 /**
711 * Delete order
712 *
713 * @access public
714 *
715 * @param $shop_id
716 * @param $order_id
717 *
718 * @return mixed
719 * @since 1.6.0
720 */
721 public function deleteOrder($shop_id, $order_id)
722 {
723
724 $response = $this->client->remote_delete('/ecommerce/shops/' . $shop_id . '/orders/' . $order_id . '?with_resource_id');
725
726 $response = self::parseResponse($response);
727
728 return $response->data ?? false;
729 }
730
731 /**
732 * Update order
733 *
734 * @access public
735 *
736 * @param $shop_id
737 * @param $order_id
738 * @param $status
739 * @param $total_price
740 *
741 * @return mixed
742 * @since 1.6.0
743 */
744 public function updateOrder($shop_id, $order_id, $status, $total_price)
745 {
746
747 $response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/orders/' . $order_id . '?with_resource_id',
748 [
749 'status' => $status,
750 'total_price' => (float)$total_price
751 ]);
752
753 $response = self::parseResponse($response);
754
755 return $response->data ?? false;
756 }
757
758 /**
759 * Sync product
760 *
761 * @access public
762 *
763 * @param $shop_id
764 * @param $product_id
765 * @param $name
766 * @param $price
767 * @param $exclude_automation
768 * @param $url
769 * @param $image
770 * @param $categories
771 *
772 * @return mixed
773 * @since 1.6.0
774 */
775 public function syncProduct($shop_id, $product_id, $name, $price, $exclude_automation, $url, $image, $categories)
776 {
777
778 $parameters = [
779 'resource_id' => (string)$product_id,
780 'name' => $name,
781 'price' => $price,
782 'exclude_from_automations' => $exclude_automation,
783 'url' => $url,
784 'categories' => $categories
785 ];
786
787 if ( ! empty($image)) {
788 $parameters['image'] = (string)$image;
789 }
790
791 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/products?with_resource_id',
792 $parameters);
793
794 $response = self::parseResponse($response);
795
796 return $response->data ?? false;
797 }
798
799 /**
800 * Replace product categories
801 *
802 * @access public
803 *
804 * @param $shop_id
805 * @param $product_id
806 * @param $categories
807 *
808 * @return mixed
809 * @since 1.6.0
810 */
811 public function replaceProductCategories($shop_id, $product_id, $categories)
812 {
813
814 $response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/products/' . $product_id . '/categories/multiple?with_resource_id',
815 [
816 'replace' => true,
817 'categories' => $categories
818 ]);
819
820 $response = self::parseResponse($response);
821
822 return $response->data ?? false;
823 }
824
825 /**
826 * Import products
827 *
828 * @access public
829 *
830 * @param @shop_id
831 * @param @products
832 *
833 * @return array
834 * @since 1.6.0
835 */
836 public function importProducts($shop_id, $products)
837 {
838
839 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/products/import?with_resource_id&replace_categories',
840 $products);
841
842 $response = self::parseResponse($response);
843
844 return $response->data ?? [];
845 }
846
847 /**
848 * Delete product
849 *
850 * @access public
851 *
852 * @param $shop_id
853 * @param $product_id
854 *
855 * @return mixed
856 * @since 1.6.0
857 */
858 public function deleteProduct($shop_id, $product_id)
859 {
860
861 $response = $this->client->remote_delete('/ecommerce/shops/' . $shop_id . '/products/' . $product_id . '?with_resource_id');
862
863 $response = self::parseResponse($response);
864
865 return $response->data ?? false;
866 }
867
868 /**
869 * Create product category
870 *
871 * @access public
872 *
873 * @param $shop_id
874 * @param $category_id
875 * @param $name
876 *
877 * @return mixed
878 * @since 1.6.0
879 */
880 public function syncCategory($shop_id, $category_id, $name)
881 {
882
883 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/categories?with_resource_id', [
884 "resource_id" => (string)$category_id,
885 "name" => $name
886 ]);
887
888 $response = self::parseResponse($response);
889
890 return $response->data ?? false;
891 }
892
893 /**
894 * Import categories
895 *
896 * @access public
897 *
898 * @param @shop_id
899 * @param @categories
900 *
901 * @return array
902 * @since 1.6.0
903 */
904 public function importCategories($shop_id, $categories)
905 {
906
907 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/categories/import?with_resource_id',
908 $categories);
909
910 $response = self::parseResponse($response);
911
912 return $response->data ?? [];
913 }
914
915
916 /**
917 * Delete product category
918 *
919 * @access public
920 *
921 * @param $shop_id
922 * @param $category_id
923 *
924 * @return mixed
925 * @since 1.6.0
926 */
927 public function deleteCategory($shop_id, $category_id)
928 {
929
930 $response = $this->client->remote_delete('/ecommerce/shops/' . $shop_id . '/categories/' . $category_id . '?with_resource_id');
931
932 $response = self::parseResponse($response);
933
934 return $response->data ?? false;
935 }
936
937 /**
938 * Sync customer
939 *
940 * @access public
941 *
942 * @param $shop_id
943 * @param $customer_id
944 * @param $email
945 * @param $fields
946 *
947 * @return mixed
948 * @since 1.6.0
949 */
950 public function syncCustomer($shop_id, $customer_id, $email, $fields)
951 {
952
953 if (strval($customer_id) === "0") {
954 return false;
955 }
956
957 $data = [
958 'resource_id' => (string)$customer_id,
959 'email' => $email,
960 ];
961
962 if (isset($fields['orders_count'])) {
963 $data['orders_count'] = $fields['orders_count'];
964
965 unset($fields['orders_count']);
966 }
967
968 if (isset($fields['total_spent'])) {
969 $data['total_spent'] = $fields['total_spent'];
970
971 unset($fields['total_spent']);
972 }
973
974 $data['subscriber_fields'] = $fields;
975
976 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/customers' . '?with_resource_id',
977 $data);
978
979 $response = self::parseResponse($response);
980
981 return $response->data ?? false;
982 }
983
984 /**
985 * Update customer
986 *
987 * @access public
988 *
989 * @param $shop_id
990 * @param $customer_id
991 * @param $email
992 * @param $accepts_marketing
993 * @param $create_subscriber
994 *
995 * @return mixed
996 * @since 1.6.0
997 */
998 public function updateCustomer($shop_id, $customer_id, $email, $accepts_marketing, $create_subscriber)
999 {
1000
1001 $response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/customers/' . $customer_id, [
1002 'email' => $email,
1003 'accepts_marketing' => $accepts_marketing,
1004 'create_subscriber' => $create_subscriber
1005 ]);
1006
1007 $response = self::parseResponse($response);
1008
1009 return $response->data ?? false;
1010 }
1011
1012 /**
1013 * Delete customer
1014 *
1015 * @access public
1016 *
1017 * @param $shop_id
1018 * @param $customer_id
1019 *
1020 * @return bool
1021 * @since 1.6.0
1022 */
1023 public function deleteCustomer($shop_id, $customer_id)
1024 {
1025 $response = $this->client->remote_delete('/ecommerce/shops/' . $shop_id . '/customers/' . $customer_id . '?with_resource_id');
1026
1027 $response = self::parseResponse($response);
1028
1029 return $response->data ?? false;
1030 }
1031
1032 /**
1033 * Fetch order
1034 *
1035 * @access public
1036 *
1037 * @param $shop_id
1038 * @param $customer_id
1039 *
1040 * @return mixed
1041 * @since 1.6.0
1042 */
1043 public function fetchCustomer($shop_id, $customer_id)
1044 {
1045
1046 $response = $this->client->remote_get('/ecommerce/shops/' . $shop_id . '/customers/' . $customer_id . '?with_resource_id');
1047
1048 $response = self::parseResponse($response);
1049
1050 return $response->data ?? false;
1051 }
1052
1053 /**
1054 * Update cart
1055 *
1056 * @access public
1057 *
1058 * @param $shop_id
1059 * @param $cart_id
1060 * @param $checkout_url
1061 * @param $cart_total
1062 *
1063 * @return mixed
1064 * @since 1.6.0
1065 */
1066 public function updateCart($shop_id, $cart_id, $checkout_url, $cart_total, $order_id)
1067 {
1068
1069 $parameters = [
1070 'resource_id' => (string)$order_id,
1071 'cart_total' => $cart_total
1072 ];
1073
1074 if ( ! empty($checkout_url)) {
1075 $parameters['checkout_url'] = (string)$checkout_url;
1076 }
1077
1078 $response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/carts/' . $cart_id, $parameters);
1079
1080 $response = self::parseResponse($response);
1081
1082 return $response->data ?? false;
1083 }
1084
1085 /**
1086 * Add cart items
1087 *
1088 * @access public
1089 *
1090 * @param $shop_id
1091 * @param $cart_id
1092 * @param $item
1093 *
1094 * @return mixed
1095 * @since 1.6.0
1096 */
1097 public function addCartItem($shop_id, $cart_id, $item)
1098 {
1099
1100 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/carts/' . $cart_id . '/items', $item);
1101
1102 $response = self::parseResponse($response);
1103
1104 return $response->data ?? false;
1105 }
1106
1107 /**
1108 * Replace cart items
1109 *
1110 * @access public
1111 *
1112 * @param $shop_id
1113 * @param $cart_id
1114 * @param $items
1115 *
1116 * @return mixed
1117 * @since 1.6.0
1118 */
1119 public function replaceCartItems($shop_id, $cart_id, $items)
1120 {
1121
1122 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/carts/' . $cart_id . '/items/replace?with_resource_id',
1123 [
1124 'items' => $items
1125 ]);
1126
1127 $response = self::parseResponse($response);
1128
1129 return $response->data ?? false;
1130 }
1131
1132 /**
1133 * Get cart items
1134 *
1135 * @access public
1136 *
1137 * @param $shop_id
1138 * @param $cart_id
1139 *
1140 * @return mixed
1141 * @since 1.6.0
1142 */
1143 public function getCartItems($shop_id, $cart_id)
1144 {
1145
1146 $response = $this->client->remote_get('/ecommerce/shops/' . $shop_id . '/carts/' . $cart_id . '/items');
1147
1148 $response = self::parseResponse($response);
1149
1150 return $response->data ?? false;
1151 }
1152
1153 /**
1154 * Delete cart items
1155 *
1156 * @access public
1157 *
1158 * @param $shop_id
1159 * @param $cart_id
1160 * @param $item_id
1161 *
1162 * @return mixed
1163 * @since 1.6.0
1164 */
1165 public function deleteCartItem($shop_id, $cart_id, $item_id)
1166 {
1167
1168 $response = $this->client->remote_delete('/ecommerce/shops/' . $shop_id . '/carts/' . $cart_id . '/items/' . $item_id);
1169
1170 $response = self::parseResponse($response);
1171
1172 return $response->data ?? false;
1173 }
1174
1175 /**
1176 * Batch process requests
1177 *
1178 * @access public
1179 *
1180 * @param $requests
1181 *
1182 * @return mixed
1183 * @since 1.6.6
1184 */
1185 public function batch($requests)
1186 {
1187 $response = $this->client->remote_post('/batch', $requests);
1188
1189 $response = self::parseResponse($response);
1190
1191 return $response->responses ?? false;
1192 }
1193
1194 /**
1195 * Get cart items
1196 *
1197 * @access public
1198 *
1199 * @return boolean
1200 * @since 1.6.0
1201 */
1202 public function getAccountDetails()
1203 {
1204
1205 return false;
1206 }
1207
1208 /**
1209 * Create Group
1210 *
1211 * @access public
1212 *
1213 * @param $name
1214 *
1215 * @return mixed
1216 * @since 1.6.6
1217 */
1218 public function createGroup($name)
1219 {
1220 $response = $this->client->remote_post('/groups', [
1221 'name' => $name
1222 ]);
1223 return self::parseResponse($response);
1224 }
1225
1226 /**
1227 * Get raw response body
1228 *
1229 * @access public
1230 * @return string
1231 * @since 1.6.0
1232 */
1233 public function getResponseBody()
1234 {
1235 return $this->response;
1236 }
1237
1238 /**
1239 * Get response code
1240 *
1241 * @access public
1242 * @return int
1243 * @since 1.6.0
1244 */
1245 public function responseCode()
1246 {
1247 return $this->response_code;
1248 }
1249
1250 /**
1251 * Get response code and body
1252 *
1253 * @access private
1254 * @return mixed
1255 * @since 1.6.0
1256 */
1257 private function parseResponse($response)
1258 {
1259 if ( ! is_wp_error($response)) {
1260
1261 $this->response = wp_remote_retrieve_body($response);
1262 $this->response_code = wp_remote_retrieve_response_code($response);
1263
1264 if ( ! is_wp_error($this->response)) {
1265 $response = json_decode($this->response);
1266
1267 if (json_last_error() == JSON_ERROR_NONE) {
1268
1269 if ( ! isset($response->error)) {
1270 return $response;
1271 }
1272 }
1273 }
1274 } else {
1275 $this->response = $response->get_error_message();
1276 $this->response_code = $response->get_error_code();
1277 }
1278
1279 return false;
1280 }
1281 }