| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | namespace FluentCart\App\Http\Requests; |
| 4 | 4 | |
| 5 | 5 | use FluentCart\App\Models\ShippingClass; |
| 6 | 6 | use FluentCart\App\Services\DateTime\DateTime; |
| 7 | +use FluentCart\App\Http\Rules\RequiredWhenRule; | |
| 7 | 8 | use FluentCart\Framework\Foundation\RequestGuard; |
| 8 | 9 | use FluentCart\Framework\Support\Arr; |
| 9 | 10 | |
| 10 | 11 | class ProductRequest extends RequestGuard |
| @@ -43,9 +44,10 @@ | ||
| 43 | 44 | |
| 44 | 45 | foreach ($variants as $index => &$variant) { |
| 45 | 46 | $variant['fulfillment_type'] = $variant['fulfillment_type'] ?? $fulfilmentType; |
| 46 | 47 | |
| 47 | - $variant['other_info'] = Arr::wrap(Arr::get($variant, 'other_info')) ?? []; | |
| 48 | + $originalOtherInfo = Arr::wrap(Arr::get($variant, 'other_info')) ?? []; | |
| 49 | + $variant['other_info'] = $originalOtherInfo; | |
| 48 | 50 | |
| 49 | 51 | // get payment_type from $variant |
| 50 | 52 | $paymentType = Arr::get($variant, 'other_info.payment_type', 'onetime'); |
| 51 | 53 | |
| @@ -50,8 +52,10 @@ | ||
| 50 | 52 | $paymentType = Arr::get($variant, 'other_info.payment_type', 'onetime'); |
| 51 | 53 | |
| 52 | 54 | $variant['shipping_class'] = Arr::get($variant, 'shipping_class', null); |
| 53 | 55 | |
| 56 | + // Preserve tax fields during normalization so they survive the same | |
| 57 | + // save pipeline as the rest of variant other_info. | |
| 54 | 58 | $variant['other_info'] = [ |
| 55 | 59 | 'payment_type' => $paymentType, |
| 56 | 60 | 'times' => Arr::get($variant, 'other_info.times', ''), |
| 57 | 61 | 'trial_days' => Arr::get($variant, 'other_info.trial_days', ''), |
| @@ -64,8 +68,16 @@ | ||
| 64 | 68 | 'setup_fee_per_item' => Arr::get($variant, 'other_info.setup_fee_per_item', 'no'), |
| 65 | 69 | 'installment' => Arr::get($variant, 'other_info.installment', 'no'), |
| 66 | 70 | ]; |
| 67 | 71 | |
| 72 | + if (Arr::has($originalOtherInfo, 'tax_class')) { | |
| 73 | + $variant['other_info']['tax_class'] = Arr::get($originalOtherInfo, 'tax_class'); | |
| 74 | + } | |
| 75 | + | |
| 76 | + if (Arr::has($originalOtherInfo, 'tax_exempt')) { | |
| 77 | + $variant['other_info']['tax_exempt'] = Arr::get($originalOtherInfo, 'tax_exempt'); | |
| 78 | + } | |
| 79 | + | |
| 68 | 80 | $data['variants'][$index] = $variant; |
| 69 | 81 | } |
| 70 | 82 | |
| 71 | 83 | return $data; |
| @@ -124,8 +136,30 @@ | ||
| 124 | 136 | return null; |
| 125 | 137 | |
| 126 | 138 | } |
| 127 | 139 | |
| 140 | + function validateTaxClassSlug($attribute, $value): ?string | |
| 141 | + { | |
| 142 | + static $checked = []; | |
| 143 | + | |
| 144 | + if (empty($value)) { | |
| 145 | + return null; | |
| 146 | + } | |
| 147 | + | |
| 148 | + $value = sanitize_text_field($value); | |
| 149 | + | |
| 150 | + if (isset($checked[$value])) { | |
| 151 | + return $checked[$value]; | |
| 152 | + } | |
| 153 | + | |
| 154 | + if (empty(\FluentCart\App\Models\TaxClass::query()->where('slug', $value)->first())) { | |
| 155 | + $checked[$value] = __("Invalid Tax Class.", 'fluent-cart'); | |
| 156 | + return $checked[$value]; | |
| 157 | + } | |
| 158 | + | |
| 159 | + return null; | |
| 160 | + } | |
| 161 | + | |
| 128 | 162 | public function validatePostDate($attribute, $value): ?string |
| 129 | 163 | { |
| 130 | 164 | if ($this->get('post_status') !== 'future') { |
| 131 | 165 | return null; |
| @@ -194,8 +228,9 @@ | ||
| 194 | 228 | }], |
| 195 | 229 | 'detail.other_info.tax_class' => ['nullable', function ($attribute, $value) { |
| 196 | 230 | return $this->validateTaxClassId($attribute, $value); |
| 197 | 231 | }], |
| 232 | + 'detail.other_info.tax_exempt' => 'nullable|sanitizeText|in:yes,no', | |
| 198 | 233 | 'detail.other_info.active_editor' => 'nullable|sanitizeText', |
| 199 | 234 | 'product_terms' => 'nullable|array', |
| 200 | 235 | 'product_terms.*' => 'nullable|array', |
| 201 | 236 | 'product_terms.*.*' => 'nullable|numeric', |
| @@ -219,8 +254,14 @@ | ||
| 219 | 254 | return null; |
| 220 | 255 | }, |
| 221 | 256 | ], |
| 222 | 257 | 'variants.*.manage_cost' => 'nullable|sanitizeText|maxLength:10', |
| 258 | + // Variant tax classes are stored as slugs, not numeric IDs like the | |
| 259 | + // older product-detail tax field. | |
| 260 | + 'variants.*.other_info.tax_class' => ['nullable', function ($attribute, $value) { | |
| 261 | + return $this->validateTaxClassSlug($attribute, $value); | |
| 262 | + }], | |
| 263 | + 'variants.*.other_info.tax_exempt' => 'nullable|sanitizeText|in:yes,no', | |
| 223 | 264 | // 'variants.*.shipping_class' => ['nullable', 'numeric', function ($attribute, $value) { |
| 224 | 265 | // return $this->validateShippingClassId($attribute, $value); |
| 225 | 266 | // }], |
| 226 | 267 | // 'variants.*.item_cost' => 'required_if:variants.*.manage_cost,true', |
| @@ -235,13 +276,43 @@ | ||
| 235 | 276 | 'variants.*.other_info.description' => 'nullable|sanitizeTextArea|maxLength:255', |
| 236 | 277 | 'variants.*.other_info.payment_type' => 'required|sanitizeText|in:onetime,subscription', |
| 237 | 278 | 'variants.*.other_info.times' => 'nullable|sanitizeText|maxLength:50', |
| 238 | 279 | 'variants.*.other_info.trial_days' => 'nullable|sanitizeText|maxLength:365', |
| 239 | - 'variants.*.other_info.repeat_interval' => 'required_if:variants.*.other_info.payment_type,subscription|sanitizeText|maxLength:100', | |
| 280 | + 'variants.*.other_info.repeat_interval' => [ | |
| 281 | + RequiredWhenRule::make( | |
| 282 | + 'variants.*.other_info.payment_type', | |
| 283 | + 'subscription', | |
| 284 | + esc_html__('Interval is required.', 'fluent-cart') | |
| 285 | + ), | |
| 286 | + 'sanitizeText', | |
| 287 | + 'maxLength:100', | |
| 288 | + ], | |
| 240 | 289 | 'variants.*.other_info.billing_summary' => 'nullable|sanitizeTextArea|maxLength:255', |
| 241 | - 'variants.*.other_info.manage_setup_fee' => 'required_if:variants.*.other_info.payment_type,subscription|sanitizeText|maxLength:100', | |
| 242 | - 'variants.*.other_info.signup_fee' => 'required_if:variants.*.other_info.manage_setup_fee,yes', | |
| 243 | - 'variants.*.other_info.signup_fee_name' => 'required_if:variants.*.other_info.manage_setup_fee,yes|sanitizeText|maxLength:100', | |
| 290 | + 'variants.*.other_info.manage_setup_fee' => [ | |
| 291 | + RequiredWhenRule::make( | |
| 292 | + 'variants.*.other_info.payment_type', | |
| 293 | + 'subscription', | |
| 294 | + esc_html__('Setup Fee option is required.', 'fluent-cart') | |
| 295 | + ), | |
| 296 | + 'sanitizeText', | |
| 297 | + 'maxLength:100', | |
| 298 | + ], | |
| 299 | + 'variants.*.other_info.signup_fee' => [ | |
| 300 | + RequiredWhenRule::make( | |
| 301 | + 'variants.*.other_info.manage_setup_fee', | |
| 302 | + 'yes', | |
| 303 | + esc_html__('Setup Fee Amount is required.', 'fluent-cart') | |
| 304 | + ), | |
| 305 | + ], | |
| 306 | + 'variants.*.other_info.signup_fee_name' => [ | |
| 307 | + RequiredWhenRule::make( | |
| 308 | + 'variants.*.other_info.manage_setup_fee', | |
| 309 | + 'yes', | |
| 310 | + esc_html__('Setup Fee Name is required.', 'fluent-cart') | |
| 311 | + ), | |
| 312 | + 'sanitizeText', | |
| 313 | + 'maxLength:100', | |
| 314 | + ], | |
| 244 | 315 | ]; |
| 245 | 316 | $rules = array_merge($rules, $variantsOtherInfoRules); |
| 246 | 317 | |
| 247 | 318 | } |
| @@ -300,11 +371,8 @@ | ||
| 300 | 371 | 'variants.*.item_cost.required_if' => esc_html__('Item cost is required.', 'fluent-cart'), |
| 301 | 372 | 'variants.*.other_info.description.max' => esc_html__('Description may not be greater than 255 characters.', 'fluent-cart'), |
| 302 | 373 | 'variants.*.other_info.payment_type.required' => esc_html__('Payment Type is required.', 'fluent-cart'), |
| 303 | 374 | 'variants.*.other_info.times.required_if' => esc_html__('Times is required.', 'fluent-cart'), |
| 304 | - 'variants.*.other_info.repeat_interval.required_if' => esc_html__('Interval is required.', 'fluent-cart'), | |
| 305 | - 'variants.*.other_info.signup_fee.required_if' => esc_html__('Setup Fee Amount is required.', 'fluent-cart'), | |
| 306 | - 'variants.*.other_info.signup_fee_name.required_if' => esc_html__('Setup Fee Name is required.', 'fluent-cart'), | |
| 307 | 375 | ]; |
| 308 | 376 | |
| 309 | 377 | $messages = array_merge($messages, $otherInfoMessages); |
| 310 | 378 | } |
| @@ -341,8 +409,9 @@ | ||
| 341 | 409 | 'detail.other_info.sold_individually' => 'sanitize_text_field', |
| 342 | 410 | 'detail.other_info.use_pricing_table' => 'sanitize_text_field', |
| 343 | 411 | 'detail.other_info.shipping_class' => 'intval', |
| 344 | 412 | 'detail.other_info.tax_class' => 'intval', |
| 413 | + 'detail.other_info.tax_exempt' => 'sanitize_text_field', | |
| 345 | 414 | 'detail.other_info.active_editor' => 'sanitize_text_field', |
| 346 | 415 | 'variants.*.id' => 'intval', |
| 347 | 416 | 'variants.*.rowId' => 'intval', |
| 348 | 417 | 'variants.*.post_id' => 'intval', |
| @@ -403,8 +472,10 @@ | ||
| 403 | 472 | 'variants.*.other_info.manage_setup_fee' => 'sanitize_text_field', |
| 404 | 473 | 'variants.*.other_info.signup_fee' => 'floatval', |
| 405 | 474 | 'variants.*.other_info.signup_fee_name' => 'sanitize_text_field', |
| 406 | 475 | 'variants.*.other_info.installment' => 'sanitize_text_field', |
| 476 | + 'variants.*.other_info.tax_class' => 'sanitize_text_field', | |
| 477 | + 'variants.*.other_info.tax_exempt' => 'sanitize_text_field', | |
| 407 | 478 | ]; |
| 408 | 479 | |
| 409 | 480 | |
| 410 | 481 | $sanitizer = array_merge($sanitizer, $otherInfoSanitizer); |