PluginProbe
MailerLite – WooCommerce integration / 1.8.7
MailerLite – WooCommerce integration v1.8.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 1.8.7, at includes/shared/api/class.woo-mailerlite-api.php

1,239 lines 29.3 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 'enabled' => true,
337 'access_data' => '-'
338 ]);
339 } else {
340
341 $response = $this->client->remote_post('/ecommerce/shops', [
342 'name' => $shop_name,
343 'url' => $store,
344 'currency' => $currency,
345 'platform' => 'woocommerce',
346 'group_id' => $group_id,
347 'enable_popups' => $popups_enabled,
348 'enabled' => true,
349 'access_data' => '-'
350 ]);
351 }
352
353 $response = self::parseResponse($response);
354
355 return $response->data ?? false;
356 }
357
358 /**
359 * Send WooCommerce Subscriber Data
360 *
361 * @access public
362 * @return mixed
363 * @since 1.6.0
364 */
365 public function sendSubscriberData($shop_id, $data)
366 {
367
368 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/customers', [
369 'email' => $data['email'],
370 'accepts_marketing' => $data['checked_sub_to_mailist'],
371 'total_spent' => 0,
372 'create_subscriber' => $data['checked_sub_to_mailist']
373 ]);
374
375 $response = self::parseResponse($response);
376
377 return $response->data;
378 }
379
380 /**
381 * Save WooCommerce Order
382 *
383 * @access public
384 * @return mixed
385 * @since 1.6.0
386 */
387 public function saveOrder($shop_id, $orderData)
388 {
389
390 return true;
391 }
392
393 /**
394 * Send WooCommerce Order Processing
395 *
396 * @access public
397 * @return mixed
398 * @since 1.6.0
399 */
400 public function sendOrderProcessing($shop_id, $data)
401 {
402
403 $order_id = $data['order_id'];
404
405 $response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/orders/' . $order_id, [
406 'status' => 'processing'
407 ]);
408
409 $response = self::parseResponse($response);
410
411 return $response->data;
412 }
413
414 /**
415 * Update WooCommerce Cart
416 *
417 * @access public
418 * @return mixed
419 * @since 1.6.0
420 */
421 public function sendCart($shop_id, $cartData)
422 {
423
424 $response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/carts/' . $cartData['cart_id'], [
425 'checkout_url' => $cartData['abandoned_checkout_url'],
426 'cart_total' => 0
427 ]);
428
429 $response = self::parseResponse($response);
430
431 return $response->data;
432 }
433
434 /**
435 * Toggle WooCommerce Shop
436 *
437 * @access public
438 *
439 * @param $shop
440 * @param $activeState
441 * @param $shop_id
442 * @param $shop_name
443 *
444 * @return mixed
445 * @since 1.6.0
446 */
447 public function toggleShop($shop, $activeState, $shop_id, $shop_name)
448 {
449
450 $response = $this->client->remote_put('/ecommerce/shops/' . $shop_id, [
451 'name' => $shop_name,
452 'url' => $shop,
453 'enabled' => $activeState
454 ]);
455
456 $response = self::parseResponse($response);
457
458 return $response->data;
459 }
460
461 /**
462 * Get WooCommerce Shop Settings
463 *
464 * @access public
465 *
466 * @param $shop_id
467 *
468 * @return mixed
469 * @since 1.6.0
470 */
471 public function getShopSettings($shop_id)
472 {
473
474 $response = $this->client->remote_get('/ecommerce/shops/' . $shop_id);
475
476 $response = self::parseResponse($response);
477
478 return $response->data ?? false;
479 }
480
481 /**
482 * Validate WooCommerce Account
483 *
484 * @access public
485 * @return mixed
486 * @since 1.6.0
487 */
488 public function validateAccount()
489 {
490
491 $response = $this->client->remote_get('/account');
492
493 $response = self::parseResponse($response);
494
495 return $response->data ?? false;
496 }
497
498 /**
499 * Get list of shops
500 *
501 * @access public
502 * @return array
503 * @since 1.6.0
504 */
505 public function getShops()
506 {
507
508 $response = $this->client->remote_get('/ecommerce/shops');
509
510 $response = self::parseResponse($response);
511
512 return $response->data ?? [];
513 }
514
515 /**
516 * Get a shop
517 *
518 * @access public
519 * @return mixed
520 * @since 1.6.0
521 */
522 public function getShop($shop_id)
523 {
524
525 $response = $this->client->remote_get('/ecommerce/shops/' . $shop_id);
526
527 $response = self::parseResponse($response);
528
529 return $response->data ?? false;
530 }
531
532 /**
533 * Create shop
534 *
535 * @access public
536 *
537 * @param $name
538 * @param $url
539 * @param $currency
540 * @param $group_id
541 * @param $enable_popups
542 * @param $access_data
543 *
544 * @return mixed
545 * @since 1.6.0
546 */
547 public function createShop($name, $url, $currency, $group_id, $enable_popups, $access_data)
548 {
549 $response = $this->client->remote_post('/ecommerce/shops', [
550 'name' => $name,
551 'url' => $url,
552 'currency' => $currency,
553 'platform' => 'woocommerce',
554 'group_id' => $group_id,
555 'enable_popups' => $enable_popups,
556 'enabled' => true,
557 'access_data' => $access_data
558 ]);
559
560 $response = self::parseResponse($response);
561
562 return $response->data ?? false;
563 }
564
565 /**
566 * Delete shop
567 *
568 * @access public
569 *
570 * @param $shop
571 *
572 * @return bool
573 * @since 1.6.0
574 */
575 public function deleteshop($shop)
576 {
577 $response = $this->client->remote_delete('/ecommerce/shops/' . $shop);
578
579 $response = self::parseResponse($response);
580
581 return $response->data ?? false;
582 }
583
584 /**
585 * Fetch order
586 *
587 * @access public
588 *
589 * @param $shop_id
590 * @param $order_id
591 *
592 * @return mixed
593 * @since 1.6.0
594 */
595 public function fetchOrder($shop_id, $order_id)
596 {
597
598 $response = $this->client->remote_get('/ecommerce/shops/' . $shop_id . '/orders/' . $order_id . '?with_resource_id');
599
600 $response = self::parseResponse($response);
601
602 return $response->data ?? false;
603 }
604
605 /**
606 * Import orders
607 *
608 * @access public
609 *
610 * @param $shop_id
611 * @param $orders
612 *
613 * @return array
614 * @since 1.6.0
615 */
616 public function importOrders($shop_id, $orders)
617 {
618
619 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/orders/import?with_resource_id', [
620 'orders' => $orders
621 ]);
622
623 $response = self::parseResponse($response);
624
625 return $response->data ?? [];
626 }
627
628 /**
629 * Import customers
630 *
631 * @access public
632 *
633 * @param $shop_id
634 * @param $customers
635 *
636 * @return array
637 * @since 1.8.5
638 */
639 public function importCustomers($shop_id, $customers)
640 {
641
642 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/customers/import?with_resource_id',
643 $customers);
644
645 $response = self::parseResponse($response);
646
647 return $response->data ?? [];
648 }
649
650 /**
651 * Sync order
652 *
653 * @access public
654 *
655 * @param $shop_id
656 * @param $order_id
657 * @param $customer
658 * @param $cart
659 * @param $status
660 * @param $total_price
661 * @param $created_at
662 *
663 * @return mixed
664 * @since 1.6.0
665 */
666 public function syncOrder($shop_id, $order_id, $customer, $cart, $status, $total_price, $created_at)
667 {
668
669 $order_status = 'pending';
670
671 if ($status == 'completed' || $status == 'processing') {
672 $order_status = 'complete';
673 }
674
675 if ( ! isset($cart['resource_id'])) {
676 $cart['resource_id'] = (string)$order_id;
677 }
678
679 if ( ! isset($cart['cart_total'])) {
680 $cart['cart_total'] = $total_price;
681 }
682
683 $parameters = [
684 'resource_id' => (string)$order_id,
685 'customer' => $customer,
686 'cart' => $cart,
687 'status' => $order_status,
688 'total_price' => $total_price,
689 'created_at' => $created_at
690 ];
691
692 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/orders?with_resource_id',
693 $parameters);
694
695 $response = self::parseResponse($response);
696
697 return $response->data ?? false;
698 }
699
700 /**
701 * Delete order
702 *
703 * @access public
704 *
705 * @param $shop_id
706 * @param $order_id
707 *
708 * @return mixed
709 * @since 1.6.0
710 */
711 public function deleteOrder($shop_id, $order_id)
712 {
713
714 $response = $this->client->remote_delete('/ecommerce/shops/' . $shop_id . '/orders/' . $order_id . '?with_resource_id');
715
716 $response = self::parseResponse($response);
717
718 return $response->data ?? false;
719 }
720
721 /**
722 * Update order
723 *
724 * @access public
725 *
726 * @param $shop_id
727 * @param $order_id
728 * @param $status
729 * @param $total_price
730 *
731 * @return mixed
732 * @since 1.6.0
733 */
734 public function updateOrder($shop_id, $order_id, $status, $total_price)
735 {
736
737 $response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/orders/' . $order_id . '?with_resource_id',
738 [
739 'status' => $status,
740 'total_price' => (float)$total_price
741 ]);
742
743 $response = self::parseResponse($response);
744
745 return $response->data ?? false;
746 }
747
748 /**
749 * Sync product
750 *
751 * @access public
752 *
753 * @param $shop_id
754 * @param $product_id
755 * @param $name
756 * @param $price
757 * @param $exclude_automation
758 * @param $url
759 * @param $image
760 * @param $categories
761 *
762 * @return mixed
763 * @since 1.6.0
764 */
765 public function syncProduct($shop_id, $product_id, $name, $price, $exclude_automation, $url, $image, $categories)
766 {
767
768 $parameters = [
769 'resource_id' => (string)$product_id,
770 'name' => $name,
771 'price' => $price,
772 'exclude_from_automations' => $exclude_automation,
773 'url' => $url,
774 'categories' => $categories
775 ];
776
777 if ( ! empty($image)) {
778 $parameters['image'] = (string)$image;
779 }
780
781 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/products?with_resource_id',
782 $parameters);
783
784 $response = self::parseResponse($response);
785
786 return $response->data ?? false;
787 }
788
789 /**
790 * Replace product categories
791 *
792 * @access public
793 *
794 * @param $shop_id
795 * @param $product_id
796 * @param $categories
797 *
798 * @return mixed
799 * @since 1.6.0
800 */
801 public function replaceProductCategories($shop_id, $product_id, $categories)
802 {
803
804 $response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/products/' . $product_id . '/categories/multiple?with_resource_id',
805 [
806 'replace' => true,
807 'categories' => $categories
808 ]);
809
810 $response = self::parseResponse($response);
811
812 return $response->data ?? false;
813 }
814
815 /**
816 * Import products
817 *
818 * @access public
819 *
820 * @param @shop_id
821 * @param @products
822 *
823 * @return array
824 * @since 1.6.0
825 */
826 public function importProducts($shop_id, $products)
827 {
828
829 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/products/import?with_resource_id&replace_categories',
830 $products);
831
832 $response = self::parseResponse($response);
833
834 return $response->data ?? [];
835 }
836
837 /**
838 * Delete product
839 *
840 * @access public
841 *
842 * @param $shop_id
843 * @param $product_id
844 *
845 * @return mixed
846 * @since 1.6.0
847 */
848 public function deleteProduct($shop_id, $product_id)
849 {
850
851 $response = $this->client->remote_delete('/ecommerce/shops/' . $shop_id . '/products/' . $product_id . '?with_resource_id');
852
853 $response = self::parseResponse($response);
854
855 return $response->data ?? false;
856 }
857
858 /**
859 * Create product category
860 *
861 * @access public
862 *
863 * @param $shop_id
864 * @param $category_id
865 * @param $name
866 *
867 * @return mixed
868 * @since 1.6.0
869 */
870 public function syncCategory($shop_id, $category_id, $name)
871 {
872
873 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/categories?with_resource_id', [
874 "resource_id" => (string)$category_id,
875 "name" => $name
876 ]);
877
878 $response = self::parseResponse($response);
879
880 return $response->data ?? false;
881 }
882
883 /**
884 * Import categories
885 *
886 * @access public
887 *
888 * @param @shop_id
889 * @param @categories
890 *
891 * @return array
892 * @since 1.6.0
893 */
894 public function importCategories($shop_id, $categories)
895 {
896
897 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/categories/import?with_resource_id',
898 $categories);
899
900 $response = self::parseResponse($response);
901
902 return $response->data ?? [];
903 }
904
905
906 /**
907 * Delete product category
908 *
909 * @access public
910 *
911 * @param $shop_id
912 * @param $category_id
913 *
914 * @return mixed
915 * @since 1.6.0
916 */
917 public function deleteCategory($shop_id, $category_id)
918 {
919
920 $response = $this->client->remote_delete('/ecommerce/shops/' . $shop_id . '/categories/' . $category_id . '?with_resource_id');
921
922 $response = self::parseResponse($response);
923
924 return $response->data ?? false;
925 }
926
927 /**
928 * Sync customer
929 *
930 * @access public
931 *
932 * @param $shop_id
933 * @param $customer_id
934 * @param $email
935 * @param $fields
936 *
937 * @return mixed
938 * @since 1.6.0
939 */
940 public function syncCustomer($shop_id, $customer_id, $email, $fields)
941 {
942
943 if (strval($customer_id) === "0") {
944 return false;
945 }
946
947 $data = [
948 'resource_id' => (string)$customer_id,
949 'email' => $email,
950 ];
951
952 if (isset($fields['orders_count'])) {
953 $data['orders_count'] = $fields['orders_count'];
954
955 unset($fields['orders_count']);
956 }
957
958 if (isset($fields['total_spent'])) {
959 $data['total_spent'] = $fields['total_spent'];
960
961 unset($fields['total_spent']);
962 }
963
964 $data['subscriber_fields'] = $fields;
965
966 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/customers' . '?with_resource_id',
967 $data);
968
969 $response = self::parseResponse($response);
970
971 return $response->data ?? false;
972 }
973
974 /**
975 * Update customer
976 *
977 * @access public
978 *
979 * @param $shop_id
980 * @param $customer_id
981 * @param $email
982 * @param $accepts_marketing
983 * @param $create_subscriber
984 *
985 * @return mixed
986 * @since 1.6.0
987 */
988 public function updateCustomer($shop_id, $customer_id, $email, $accepts_marketing, $create_subscriber)
989 {
990
991 $response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/customers/' . $customer_id, [
992 'email' => $email,
993 'accepts_marketing' => $accepts_marketing,
994 'create_subscriber' => $create_subscriber
995 ]);
996
997 $response = self::parseResponse($response);
998
999 return $response->data ?? false;
1000 }
1001
1002 /**
1003 * Delete customer
1004 *
1005 * @access public
1006 *
1007 * @param $shop_id
1008 * @param $customer_id
1009 *
1010 * @return bool
1011 * @since 1.6.0
1012 */
1013 public function deleteCustomer($shop_id, $customer_id)
1014 {
1015 $response = $this->client->remote_delete('/ecommerce/shops/' . $shop_id . '/customers/' . $customer_id . '?with_resource_id');
1016
1017 $response = self::parseResponse($response);
1018
1019 return $response->data ?? false;
1020 }
1021
1022 /**
1023 * Fetch order
1024 *
1025 * @access public
1026 *
1027 * @param $shop_id
1028 * @param $customer_id
1029 *
1030 * @return mixed
1031 * @since 1.6.0
1032 */
1033 public function fetchCustomer($shop_id, $customer_id)
1034 {
1035
1036 $response = $this->client->remote_get('/ecommerce/shops/' . $shop_id . '/customers/' . $customer_id . '?with_resource_id');
1037
1038 $response = self::parseResponse($response);
1039
1040 return $response->data ?? false;
1041 }
1042
1043 /**
1044 * Update cart
1045 *
1046 * @access public
1047 *
1048 * @param $shop_id
1049 * @param $cart_id
1050 * @param $checkout_url
1051 * @param $cart_total
1052 *
1053 * @return mixed
1054 * @since 1.6.0
1055 */
1056 public function updateCart($shop_id, $cart_id, $checkout_url, $cart_total, $order_id)
1057 {
1058
1059 $parameters = [
1060 'resource_id' => (string)$order_id,
1061 'cart_total' => $cart_total
1062 ];
1063
1064 if ( ! empty($checkout_url)) {
1065 $parameters['checkout_url'] = (string)$checkout_url;
1066 }
1067
1068 $response = $this->client->remote_put('/ecommerce/shops/' . $shop_id . '/carts/' . $cart_id, $parameters);
1069
1070 $response = self::parseResponse($response);
1071
1072 return $response->data ?? false;
1073 }
1074
1075 /**
1076 * Add cart items
1077 *
1078 * @access public
1079 *
1080 * @param $shop_id
1081 * @param $cart_id
1082 * @param $item
1083 *
1084 * @return mixed
1085 * @since 1.6.0
1086 */
1087 public function addCartItem($shop_id, $cart_id, $item)
1088 {
1089
1090 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/carts/' . $cart_id . '/items', $item);
1091
1092 $response = self::parseResponse($response);
1093
1094 return $response->data ?? false;
1095 }
1096
1097 /**
1098 * Replace cart items
1099 *
1100 * @access public
1101 *
1102 * @param $shop_id
1103 * @param $cart_id
1104 * @param $items
1105 *
1106 * @return mixed
1107 * @since 1.6.0
1108 */
1109 public function replaceCartItems($shop_id, $cart_id, $items)
1110 {
1111
1112 $response = $this->client->remote_post('/ecommerce/shops/' . $shop_id . '/carts/' . $cart_id . '/items/replace?with_resource_id',
1113 [
1114 'items' => $items
1115 ]);
1116
1117 $response = self::parseResponse($response);
1118
1119 return $response->data ?? false;
1120 }
1121
1122 /**
1123 * Get cart items
1124 *
1125 * @access public
1126 *
1127 * @param $shop_id
1128 * @param $cart_id
1129 *
1130 * @return mixed
1131 * @since 1.6.0
1132 */
1133 public function getCartItems($shop_id, $cart_id)
1134 {
1135
1136 $response = $this->client->remote_get('/ecommerce/shops/' . $shop_id . '/carts/' . $cart_id . '/items');
1137
1138 $response = self::parseResponse($response);
1139
1140 return $response->data ?? false;
1141 }
1142
1143 /**
1144 * Delete cart items
1145 *
1146 * @access public
1147 *
1148 * @param $shop_id
1149 * @param $cart_id
1150 * @param $item_id
1151 *
1152 * @return mixed
1153 * @since 1.6.0
1154 */
1155 public function deleteCartItem($shop_id, $cart_id, $item_id)
1156 {
1157
1158 $response = $this->client->remote_delete('/ecommerce/shops/' . $shop_id . '/carts/' . $cart_id . '/items/' . $item_id);
1159
1160 $response = self::parseResponse($response);
1161
1162 return $response->data ?? false;
1163 }
1164
1165 /**
1166 * Batch process requests
1167 *
1168 * @access public
1169 *
1170 * @param $requests
1171 *
1172 * @return mixed
1173 * @since 1.6.6
1174 */
1175 public function batch($requests)
1176 {
1177 $response = $this->client->remote_post('/batch', $requests);
1178
1179 $response = self::parseResponse($response);
1180
1181 return $response->responses ?? false;
1182 }
1183
1184 /**
1185 * Get raw response body
1186 *
1187 * @access public
1188 * @return string
1189 * @since 1.6.0
1190 */
1191 public function getResponseBody()
1192 {
1193 return $this->response;
1194 }
1195
1196 /**
1197 * Get response code
1198 *
1199 * @access public
1200 * @return int
1201 * @since 1.6.0
1202 */
1203 public function responseCode()
1204 {
1205 return $this->response_code;
1206 }
1207
1208 /**
1209 * Get response code and body
1210 *
1211 * @access private
1212 * @return mixed
1213 * @since 1.6.0
1214 */
1215 private function parseResponse($response)
1216 {
1217 if ( ! is_wp_error($response)) {
1218
1219 $this->response = wp_remote_retrieve_body($response);
1220 $this->response_code = wp_remote_retrieve_response_code($response);
1221
1222 if ( ! is_wp_error($this->response)) {
1223 $response = json_decode($this->response);
1224
1225 if (json_last_error() == JSON_ERROR_NONE) {
1226
1227 if ( ! isset($response->error)) {
1228 return $response;
1229 }
1230 }
1231 }
1232 } else {
1233 $this->response = $response->get_error_message();
1234 $this->response_code = $response->get_error_code();
1235 }
1236
1237 return false;
1238 }
1239 }