PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.20
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.20
1.6.5 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 All 48 releases
fluent-cart / api / Resource / ProductDetailResource.php

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

175 lines 6.2 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\Events\StockChanged;
6 use FluentCart\App\Helpers\Helper;
7 use FluentCart\App\Helpers\ProductAdminHelper;
8 use FluentCart\App\Models\ProductDetail;
9 use FluentCart\Framework\Database\Orm\Builder;
10 use FluentCart\Framework\Support\Arr;
11
12 class ProductDetailResource extends BaseResourceApi
13 {
14
15 public static function getQuery(): Builder
16 {
17 return ProductDetail::query();
18 }
19
20 public static function get(array $params = [])
21 {
22 //
23 }
24
25 /**
26 * Find product detail by its id.
27 *
28 * @param int $id The id of the product detail.
29 * @param array $data Additional data for finding product (optional).
30 *
31 */
32 public static function find($id, $data = [])
33 {
34 return static::getQuery()->find($id);
35 }
36
37 /**
38 * Create a new product detail with the given data.
39 *
40 * @param array $data Array containing the necessary parameters.
41 *
42 * $data = [
43 * 'post_id' => (int) Required. The product ID.
44 * 'fulfillment_type' => (string) Required. The fulfillment type default:physical.
45 * 'variation_type' => (string) Required. The variation type default:simple.
46 * 'manage_stock' => (int) Required. The manage stock default:1.
47 * ];
48 */
49 public static function create($data, $params = [])
50 {
51 $isCreated = static::getQuery()->create($data);
52
53 if ($isCreated) {
54 return static::makeSuccessResponse(
55 $isCreated,
56 __('Product has been created successfully', 'fluent-cart')
57 );
58 }
59
60 return static::makeErrorResponse([
61 ['code' => 400, 'message' => __('Product creation failed!', 'fluent-cart')]
62 ]);
63 }
64
65 /**
66 * Update a product detail with the given data.
67 * @param int $id The id of the product detail to be updated.
68 * @param array $data Array containing the necessary parameters.
69 *
70 * $data = [
71 * 'id' => (int) Required. The detail id.
72 * 'post_id' => (int) Required. The product ID.
73 * 'fulfillment_type' => (string) Required. The fulfillment type.
74 * 'variation_type' => (string) Required. The variation type.
75 * 'default_variation_id' => (int) Required. The default variation ID.
76 * 'manage_stock' => (int) Required. The manage stock default:1.
77 * ];
78 * @param array $params Additional parameters for the update process.
79 * $params = [
80 * 'action' => (string) Required. This param will help to update detail based on the specific action i.e: variant_modified(Triggers when variant modified which covers all mutations), change_variation_type(Triggers when variation type will change).
81 * ];
82 */
83 public static function update($data, $id, $params = [])
84 {
85 $data ??= [];
86
87 if (!$id) {
88 return static::makeErrorResponse([
89 ['code' => 403, 'message' => __('Please edit a valid product!', 'fluent-cart')]
90 ]);
91 }
92
93 $detail = static::getQuery()->find($id);
94
95 if (!$detail) {
96 return static::makeErrorResponse([
97 ['code' => 404, 'message' => __('Product not found, please reload the page and try again!', 'fluent-cart')]
98 ]);
99 }
100
101 $triggeredAction = Arr::get($params, 'action');
102
103 // Stock & Price Range Handling
104 if ($triggeredAction === 'variant_modified') {
105 $manageStock = Arr::has($data, 'manage_stock') ? Arr::get($data, 'manage_stock') : $detail->manage_stock;
106
107 if (!$manageStock) {
108 $data['stock_availability'] = Helper::IN_STOCK;
109 } else {
110 $hasInStock = \FluentCart\App\Models\ProductVariation::query()
111 ->where('post_id', $detail->post_id)
112 ->where('stock_status', 'in-stock')
113 ->exists();
114 $data['stock_availability'] = $hasInStock ? Helper::IN_STOCK : Helper::OUT_OF_STOCK;
115 }
116 }
117
118 if ($triggeredAction === 'change_variation_type' && Arr::get($data, 'variation_type') === 'simple') {
119 $variationIds = Arr::get($data, 'variation_ids', []);
120 if (!empty($detail->post_id) && count($variationIds) > 0) {
121 ProductAdminHelper::deleteOrphanVariant($detail->post_id, $variationIds);
122
123 }
124 }
125
126 $data['min_price'] = Arr::get($data, 'min_price') ?: ($detail->min_price ?? 0);
127 $data['max_price'] = Arr::get($data, 'max_price') ?: ($detail->max_price ?? 0);
128
129 // Handle Default Variation
130 if (empty(Arr::get($data, 'default_variation_id'))) {
131 $data['default_variation_id'] = NULL;
132 }
133
134 // Handle other_info merge
135 if (Arr::has($data, 'other_info')) {
136 $existingOtherInfo = $detail->other_info ?? [];
137 $newOtherInfo = Arr::get($data, 'other_info', []);
138
139 // Merge existing with new data (new data overwrites existing)
140 $mergedOtherInfo = array_merge($existingOtherInfo, $newOtherInfo);
141
142 // Handle subscription-specific logic
143 if (Arr::get($mergedOtherInfo, 'payment_type') == 'subscription' && Arr::get($mergedOtherInfo, 'manage_setup_fee') == 'yes') {
144 $signupFee = Helper::toCent(floatval(Arr::get($mergedOtherInfo, 'signup_fee', 0)));
145 $mergedOtherInfo['signup_fee'] = $signupFee;
146 }
147
148 $data['other_info'] = $mergedOtherInfo;
149 }
150
151 $isUpdated = $detail->update($data);
152
153 if ($isUpdated) {
154 return static::makeSuccessResponse($isUpdated, __('Product pricing has been changed!', 'fluent-cart'));
155 }
156
157 return static::makeErrorResponse([
158 ['code' => 400, 'message' => __('Product update failed.', 'fluent-cart')]
159 ]);
160 }
161
162 /**
163 * Delete product detail and its associated data.
164 *
165 * @param int $id The id of the product detail to be deleted.
166 * @param array $params Additional parameters for the deletion process.
167 *
168 */
169 public static function delete($id, $params = [])
170 {
171 //
172 }
173
174 }
175