← All changes
|
app/Modules/Shipping/Http/Requests/ShippingZoneRequest.php
+27
-7
1.3.20
→
1.6.5
View file →
| @@ -19,8 +19,24 @@ | ||
| 19 | 19 | // Only include shipping_class_id if explicitly submitted — prevents wiping on edit |
| 20 | 20 | if (array_key_exists('shipping_class_id', $data)) { |
| 21 | 21 | $classId = $data['shipping_class_id']; |
| 22 | 22 | $data['shipping_class_id'] = $classId ? intval($classId) : null; |
| 23 | + } else { | |
| 24 | + // Omitted on update means "preserve the stored class". Resolve the | |
| 25 | + // effective class HERE (this return is merged into the request) so | |
| 26 | + // the whole-world uniqueness rule below validates the same class | |
| 27 | + // bucket the row will actually keep — and the controller writes | |
| 28 | + // that same value back. Without this, a class-scoped zone going | |
| 29 | + // region=all was checked against general zones: falsely rejected by | |
| 30 | + // an unrelated general whole-world zone, while a real conflict in | |
| 31 | + // its own class went unchecked. | |
| 32 | + $currentId = Arr::get($data, 'id') ?: App::getInstance()->request->get('id'); | |
| 33 | + if ($currentId) { | |
| 34 | + $storedClassId = \FluentCart\App\Models\ShippingZone::query() | |
| 35 | + ->where('id', intval($currentId)) | |
| 36 | + ->value('shipping_class_id'); | |
| 37 | + $data['shipping_class_id'] = $storedClassId ? intval($storedClassId) : null; | |
| 38 | + } | |
| 23 | 39 | } |
| 24 | 40 | |
| 25 | 41 | return $data; |
| 26 | 42 | } |
| @@ -35,19 +51,23 @@ | ||
| 35 | 51 | 'name' => 'required|string|maxLength:192', |
| 36 | 52 | 'region' => function ($attr, $value) { |
| 37 | 53 | if ($value === 'all') { |
| 38 | 54 | $shippingClassId = Arr::get($this->all(), 'shipping_class_id', null); |
| 39 | - $zone = \FluentCart\App\Models\ShippingZone::query()->where('region', 'all'); | |
| 55 | + $query = \FluentCart\App\Models\ShippingZone::query()->where('region', 'all'); | |
| 40 | 56 | if ($shippingClassId) { |
| 41 | - $zone = $zone->where('shipping_class_id', $shippingClassId); | |
| 57 | + $query->where('shipping_class_id', $shippingClassId); | |
| 42 | 58 | } else { |
| 43 | - $zone = $zone->whereNull('shipping_class_id'); | |
| 59 | + $query->where(function ($q) { | |
| 60 | + $q->whereNull('shipping_class_id') | |
| 61 | + ->orWhere('shipping_class_id', 0); | |
| 62 | + }); | |
| 44 | 63 | } |
| 45 | - if($this->id){ | |
| 46 | - $zone = $zone->where('id', '!=', $this->id); | |
| 64 | + // Exclude current zone on update | |
| 65 | + $currentId = Arr::get($this->all(), 'id') ?: App::getInstance()->request->get('id'); | |
| 66 | + if ($currentId) { | |
| 67 | + $query->where('id', '!=', intval($currentId)); | |
| 47 | 68 | } |
| 48 | - $zone = $zone->first(); | |
| 49 | - if ($zone) { | |
| 69 | + if ($query->first()) { | |
| 50 | 70 | return __('Only one "Whole World" shipping zone is allowed.', 'fluent-cart'); |
| 51 | 71 | } |
| 52 | 72 | } |
| 53 | 73 | return null; |