PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.21
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.21
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 / app / Http / Requests / ProductVariationRequest.php

ProductVariationRequest.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.21, at app/Http/Requests/ProductVariationRequest.php

245 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Http\Requests;
4
5 use FluentCart\Framework\Foundation\RequestGuard;
6 use FluentCart\Framework\Support\Arr;
7
8 class ProductVariationRequest extends RequestGuard
9 {
10 /**
11 * Normalize variant data before validation.
12 *
13 * This method is primarily used to ensure that the `variants` array contains
14 * the required structure, especially during data migration or when
15 * optional fields like `other_info` are not provided.
16 *
17 * Key operations:
18 * - Sets a default `fulfillment_type` (fallback to 'physical') if missing.
19 * - Sets a default `payment_type` (fallback to 'onetime') if missing.
20 * - Ensures `other_info` exists and assigns default billing/setup fee-related values if it's empty.
21 *
22 * This helps avoid issues during validation or processing by guaranteeing a consistent data structure.
23 *
24 * @return array The normalized data ready for validation.
25 */
26 public function beforeValidation()
27 {
28 $data = $this->all();
29 $fulfilmentType = Arr::get(
30 $data,
31 'variants.fulfillment_type',
32 Arr::get($data, 'variants.fulfillment_type', 'physical')
33 );
34 $paymentType = Arr::get($data, 'variants.payment_type', 'onetime');
35 $manageCost = Arr::get($data, 'variants.manage_cost');
36 if (empty($manageCost)) {
37 $manageCost = 'false';
38 }
39 $data['variants']['fulfillment_type'] = $fulfilmentType;
40 $data['variants']['manage_cost'] = $manageCost;
41
42 $variantOtherInfo = Arr::wrap(Arr::get($data, 'variants.other_info'));
43
44 // Ensure other_info is an array
45 if (empty($variantOtherInfo)) {
46 $variantOtherInfo = [
47 'payment_type' => $paymentType,
48 'times' => '',
49 'trial_days' => '',
50 'repeat_interval' => 'yearly',
51 'billing_summary' => '',
52 'manage_setup_fee' => 'no',
53 'signup_fee_name' => '',
54 'signup_fee' => '',
55 'setup_fee_per_item' => 'no',
56 //'purchasable' => 'yes',
57
58 ];
59 }
60 $data['variants']['other_info'] = $variantOtherInfo;
61
62 return $data;
63 }
64
65 /**
66 * @return array
67 */
68 public function rules()
69 {
70 return [
71 'variants.variation_title' => 'required|sanitizeText|maxLength:200',
72 'variants.sku' => 'nullable|sanitizeText|maxLength:30|unique:fct_product_variations,sku' . ($this->get('variants.id') ? ',' . $this->get('variants.id') : ''),
73 'variants.item_price' => 'nullable|numeric|min:0',
74 'variants.compare_price' => [
75 'nullable',
76 'numeric',
77 function ($attribute, $value) {
78 $itemPrice = $this->get("variants.item_price");
79 if (empty($itemPrice)) {
80 $itemPrice = 0;
81 }
82 if ($value !== null && $value < $itemPrice) {
83 return sprintf(__("Compare price must be greater than or equal to item price.", 'fluent-cart'));
84 }
85 return null;
86 },
87 ],
88 'variants.manage_cost' => 'nullable|sanitizeText|maxLength:10',
89 'variants.item_cost' => 'required_if:variants.manage_cost,true',
90 'variants.fulfillment_type' => 'required|sanitizeText|maxLength:100',
91 'variants.shipping_class' => function ($attr, $value) {
92 if ($value && !(\FluentCart\App\Models\ShippingClass::find(intval($value)))) {
93 return __('The selected shipping class does not exist.', 'fluent-cart');
94 }
95 return null;
96 },
97
98 'variants.manage_stock' => 'nullable|numeric',
99 'variants.stock_status' => 'required_if:variants.manage_stock,1|sanitizeText|maxLength:50',
100 'variants.total_stock' => 'required|numeric',
101 'variants.available' => 'required|numeric',
102 // 'variants.available' => [
103 // 'required',
104 // 'numeric',
105 // function ($attribute, $value, $fail) {
106 // if ($this->variants['stock_status'] == 'in-stock' && $value <= 0) {
107 // return __("The available stock must be greater than 0 when stock is set to in stock", 'fluent-cart');
108 // }
109 // return null;
110 // },
111 // ],
112 'variants.committed' => 'required|numeric',
113 'variants.on_hold' => 'required|numeric',
114
115 'variants.serial_index' => 'nullable|numeric',
116
117 'variants.other_info' => 'required|array',
118 'variants.other_info.description' => 'nullable|sanitizeTextArea|maxLength:255',
119 'variants.other_info.payment_type' => 'required|sanitizeText|in:onetime,subscription',
120 'variants.other_info.times' => 'nullable|numeric',
121 'variants.other_info.trial_days' => 'nullable|numeric|max:365',
122 'variants.other_info.repeat_interval' => 'required_if:variants.other_info.payment_type,subscription|sanitizeText|maxLength:100',
123 'variants.other_info.billing_summary' => 'nullable|sanitizeTextArea|maxLength:255',
124 'variants.other_info.manage_setup_fee' => 'required_if:variants.other_info.payment_type,subscription|sanitizeText|maxLength:100',
125 'variants.other_info.signup_fee' => 'required_if:variants.other_info.manage_setup_fee,yes',
126 'variants.other_info.signup_fee_name' => 'required_if:variants.other_info.manage_setup_fee,yes|sanitizeText|maxLength:100',
127 'variants.other_info.package_slug' => 'nullable|sanitizeText|maxLength:100',
128 'variants.other_info.weight' => 'nullable|numeric',
129 'variants.other_info.weight_unit' => 'nullable|sanitizeText|maxLength:10',
130 'variants.other_info.length' => 'nullable|numeric',
131 'variants.other_info.width' => 'nullable|numeric',
132 'variants.other_info.height' => 'nullable|numeric',
133
134 'variants.downloadable' => 'nullable|sanitizeText|maxLength:10',
135 ];
136 }
137
138
139 public function afterValidation($validator): array
140 {
141
142 $data = $this->get();
143
144 $price = $data['variants']['item_price'];
145
146 if (empty($price)) {
147 $data['variants']['item_price'] = 0;
148 }
149
150 return $data;
151 }
152
153
154 /**
155 * @return array
156 */
157 public function messages()
158 {
159 return [
160 'variants.variation_title.required' => esc_html__('Title is required.', 'fluent-cart'),
161 'variants.variation_title.max' => esc_html__('Title may not be greater than 200 characters.', 'fluent-cart'),
162 'variants.sku.max' => esc_html__('SKU may not be greater than 30 characters.', 'fluent-cart'),
163 'variants.sku.unique' => esc_html__('The SKU must be unique.', 'fluent-cart'),
164 'variants.item_price.required' => esc_html__('Price is required.', 'fluent-cart'),
165 'variants.item_price.numeric' => esc_html__('Price must be a number.', 'fluent-cart'),
166 'variants.item_price.min' => esc_html__('Price must be a positive number greater than 0.', 'fluent-cart'),
167 'variants.stock_status.required_if' => esc_html__('Stock status is required.', 'fluent-cart'),
168 'variants.item_cost.required_if' => esc_html__('Item cost is required.', 'fluent-cart'),
169 'variants.fulfillment_type.required' => esc_html__('Fulfilment Type is required.', 'fluent-cart'),
170
171 'variants.other_info.description.max' => esc_html__('Description may not be greater than 255 characters.', 'fluent-cart'),
172 'variants.other_info.payment_type.required' => esc_html__('Payment Type is required.', 'fluent-cart'),
173 'variants.other_info.times.required_if' => esc_html__('Times is required.', 'fluent-cart'),
174 'variants.other_info.repeat_interval.required_if' => esc_html__('Interval is required.', 'fluent-cart'),
175 'variants.other_info.signup_fee.required_if' => esc_html__('Setup Fee Amount is required.', 'fluent-cart'),
176 'variants.other_info.signup_fee_name.required_if' => esc_html__('Setup Fee Name is required.', 'fluent-cart'),
177 'variants.other_info.trial_days.numeric' => esc_html__('Trial days must be a number.', 'fluent-cart'),
178 'variants.other_info.trial_days.max' => esc_html__('Trial period cannot exceed 365 days.', 'fluent-cart'),
179 ];
180 }
181
182
183 /**
184 * @return array
185 */
186 public function sanitize()
187 {
188
189 return [
190 'variants.id' => 'intval',
191 'variants.rowId' => 'intval',
192 'variants.post_id' => 'intval',
193 'variants.variation_title' => 'sanitize_text_field',
194 'variants.sku' => 'sanitize_text_field',
195 'variants.item_price' => 'floatval',
196 'variants.compare_price' => 'floatval',
197 'variants.manage_cost' => 'sanitize_text_field',
198 'variants.fulfillment_type' => 'sanitize_text_field',
199 'variants.item_cost' => 'floatval',
200 'variants.total_stock' => 'intval',
201 'variants.available' => 'intval',
202 'variants.committed' => 'intval',
203 'variants.on_hold' => 'intval',
204 'variants.manage_stock' => 'intval',
205 'variants.shipping_class' => function ($value) {
206 return $value ? intval($value) : null;
207 },
208 'variants.stock_status' => 'sanitize_key',
209 'variants.serial_index' => 'intval',
210 'variants.media.*.id' => 'intval',
211 'variants.media.*.url' => function ($value) {
212 if (empty($value)) {
213 return '';
214 }
215
216 return sanitize_url($value);
217 },
218 'variants.media.*.title' => 'sanitize_text_field',
219
220 'variants.downloadable' => 'sanitize_text_field',
221
222 'variants.other_info' => function ($value) {
223 return is_array($value) ? $value : [];
224 },
225 'variants.other_info.description' => 'sanitize_text_field',
226 'variants.other_info.payment_type' => 'sanitize_text_field',
227 'variants.other_info.times' => 'sanitize_text_field',
228 'variants.other_info.trial_days' => 'sanitize_text_field',
229 'variants.other_info.repeat_interval' => 'sanitize_text_field',
230 'variants.other_info.billing_summary' => 'sanitize_text_field',
231 'variants.other_info.manage_setup_fee' => 'sanitize_text_field',
232 'variants.other_info.signup_fee' => 'floatval',
233 'variants.other_info.signup_fee_name' => 'sanitize_text_field',
234 'variants.other_info.package_slug' => 'sanitize_text_field',
235 'variants.other_info.weight' => 'floatval',
236 'variants.other_info.weight_unit' => 'sanitize_text_field',
237 'variants.other_info.length' => 'floatval',
238 'variants.other_info.width' => 'floatval',
239 'variants.other_info.height' => 'floatval',
240 //'variants.other_info.purchasable' => 'sanitize_text_field',
241 ];
242
243 }
244 }
245