PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / api / OrderMetaApi.php

OrderMetaApi.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at api/OrderMetaApi.php

148 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\Api;
4
5 use FluentCart\App\Models\OrderMeta;
6
7 class OrderMetaApi
8 {
9
10 /**
11 *
12 * @param $orderId
13 * @param $val
14 * @return bool
15 */
16 public static function updateDiscountMeta($orderId, $val): bool
17 {
18 $ins = new static();
19
20 return $ins->update($orderId, 'order_discount', $val);
21 }
22
23
24 /**
25 *
26 * @param $orderId
27 * @param $val
28 * @return bool
29 */
30 public static function updateShippingMeta($orderId, $val): bool
31 {
32 $ins = new static();
33
34 return $ins->update($orderId, 'order_shipping', $val);
35 }
36
37
38 /**
39 *
40 * @param $orderId
41 * @return mixed|string
42 */
43 public static function getDiscountMeta($orderId)
44 {
45 $ins = new static();
46
47 return $ins->getMetaByKey('order_discount', $orderId);
48 }
49
50
51 /**
52 *
53 * @param $orderId
54 * @return mixed|string
55 */
56 public static function getShippingMeta($orderId)
57 {
58 $ins = new static();
59
60 return $ins->getMetaByKey('order_shipping', $orderId);
61 }
62
63
64 /**
65 * Alias of all
66 * @param $orderId
67 * @return null
68 */
69 protected function all($orderId)
70 {
71 return $this->getAll($orderId);
72 }
73
74
75 /**
76 *
77 * @param $orderId
78 * @param $meta_key
79 * @param $meta_value
80 * @return bool
81 */
82 public function update($orderId, $meta_key, $meta_value)
83 {
84 $meta = new OrderMeta();
85
86 $meta_key = sanitize_text_field($meta_key);
87
88 $existingMeta = $meta::where('order_id', $orderId)->where('meta_key', $meta_key)->first();
89
90 if ($existingMeta) {
91 $existingMeta->meta_value = $meta_value;
92 return $existingMeta->save();
93 }
94
95 $meta->order_id = $orderId;
96 $meta->meta_key = $meta_key;
97 $meta->meta_value = $meta_value;
98
99 return $meta->save();
100 }
101
102
103 /**
104 *
105 * @param $orderId
106 * @return mixed
107 */
108 public function getAll($orderId)
109 {
110
111 return OrderMeta::query()->where('order_id', $orderId)->get();
112 }
113
114
115 /**
116 *
117 * @param $key
118 * @param $orderId
119 * @return mixed
120 */
121 protected function getMetaByKey($key, $orderId, $def = '')
122 {
123 $vl = OrderMeta::query()->where('order_id', $orderId)->where('meta_key', $key)->first();
124
125 if (empty($vl)) {
126
127 return $def;
128 }
129
130 return $vl->meta_value;
131 }
132
133
134 /**
135 *
136 * @param $method
137 * @param $arguments
138 * @return mixed
139 */
140 public static function __callStatic($method, $arguments)
141 {
142 $ins = new static();
143
144 return call_user_func_array([$ins, $method], $arguments);
145 }
146 }
147
148