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 / Resource / OrderMetaResource.php

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

166 lines 5.4 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\Resource;
4
5 use FluentCart\App\App;
6 use FluentCart\App\Models\OrderMeta;
7 use FluentCart\Framework\Database\Orm\Builder;
8 use FluentCart\Framework\Support\Arr;
9
10 class OrderMetaResource extends BaseResourceApi
11 {
12
13 public static function getQuery(): Builder
14 {
15 return OrderMeta::query();
16 }
17
18 /**
19 * Retrieve order meta based on the provided parameters.
20 *
21 * @param array $params Optional. Additional parameters for data retrieval.
22 * [
23 * 'order_id' => (int) Required. The ID of the order to filter the data.
24 * ]
25 *
26 */
27 public static function get(array $params = [])
28 {
29 return static::getQuery()->where('order_id', Arr::get($params, 'order_id'))->get();
30 }
31
32 /**
33 * Find order meta data based on the provided order ID and params.
34 *
35 * @param int $id Required. The ID of the order to find the meta data.
36 * @param array $params Optional. Additional parameters for finding order meta data.
37 * [
38 * 'meta_key' => (string) Required. The key of the order meta data to retrieve.
39 * 'def' => (mixed) Optional. Default value to return if order meta is not found.
40 * ]
41 *
42 */
43 public static function find($id, $params = [])
44 {
45 $orderMeta = static::getQuery()->where('order_id', $id)->where('meta_key', Arr::get($params, 'meta_key'))->first();
46
47 if (empty($orderMeta)) {
48
49 return Arr::get($params, 'def', '');
50 }
51
52 return $orderMeta->meta_value;
53 }
54
55 /**
56 * Create order meta data with the provided information.
57 *
58 * @param array $data Required. Array containing the necessary parameters for data creation.
59 * [
60 * // Include required parameters for creating the data.
61 * ]
62 * @param array $params Optional. Additional parameters for data creation.
63 * [
64 * // Include optional parameters, if any.
65 * ]
66 *
67 */
68 public static function create($data, $params = [])
69 {
70 // if(is_array($data['meta_value']) || is_object($data['meta_value'])){
71 // $data['meta_value'] = $data['meta_value'];
72 // }
73
74 $isCreated = static::getQuery()->create($data);
75
76 if ($isCreated) {
77 return static::makeSuccessResponse(
78 $isCreated,
79 __('Created successfully!', 'fluent-cart')
80 );
81 }
82
83 return static::makeErrorResponse([
84 ['code' => 400, 'message' => __('Failed to create.', 'fluent-cart')]
85 ]);
86 }
87
88 /**
89 * Update order meta data with the provided information.
90 *
91 * @param array $data Required. Array containing the necessary parameters
92 * [
93 * // Include required parameters for updating the data.
94 * ]
95 * @param int $id Required. The ID of the order associated with the data to update.
96 * @param array $params Optional. Additional parameters for order meta update.
97 * [
98 * 'meta_key' => (string) Required. The key of the meta data to update.
99 * ]
100 *
101 */
102 public static function update($data, $id, $params = [])
103 {
104 $existingMeta = static::getQuery()->where('order_id', $id)->where('meta_key', Arr::get($params, 'meta_key'))->first();
105
106 if ($existingMeta) {
107 //phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
108 $existingMeta->update(['meta_value' => $data]);
109
110 return static::makeSuccessResponse(
111 $existingMeta,
112 __('Updated successfully', 'fluent-cart')
113 );
114 }
115
116 return static::makeErrorResponse([
117 ['code' => 400, 'message' => __('Failed to update', 'fluent-cart')]
118 ]);
119 }
120
121 /**
122 * Delete an order meta based on the given ID and parameters.
123 *
124 * @param int $id Required. The ID of the order meta.
125 * @param array $params Optional. Additional parameters for order meta deletion.
126 *
127 */
128 public static function delete($id, $params = [])
129 {
130 if (!$id) {
131 return static::makeErrorResponse([
132 ['code' => 403, 'message' => __('Please use a valid ID!', 'fluent-cart')]
133 ]);
134 }
135
136 $orderMeta = static::getQuery()->where('order_id', $id)->where('meta_key', Arr::get($params, 'meta_key'))->first();
137
138 $metaKey = str_replace('_', ' ', Arr::get($params, 'meta_key'));
139
140 if ($orderMeta) {
141 if ($orderMeta->delete()) {
142 static::makeSuccessResponse('', sprintf(
143 /* translators: %s is the meta key */
144 __("%s successfully deleted.", 'fluent-cart'), $metaKey));
145 }
146
147 return static::makeErrorResponse([
148 ['code' => 400, 'message' => sprintf(
149 /* translators: %s is the meta key */
150 __('%s deletion failed!', 'fluent-cart'), $metaKey)]
151 ]);
152 }
153
154 return static::makeErrorResponse([
155 ['code' => 404, 'message' => sprintf(
156 /* translators: %s is the meta key */
157 __('%s not found in database, failed to remove.', 'fluent-cart'), $metaKey)]
158 ]);
159 }
160
161 public static function bulkDeleteByOrderIds($ids, $params=[])
162 {
163 return static::getQuery()->whereIn('order_id', $ids)->delete();
164 }
165 }
166