| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Models\Store;
- use App\StorePlan\FeaturedProducts;
- use App\StorePlan\PlanValidator;
- use Illuminate\Http\RedirectResponse;
- use Illuminate\Http\Request;
- use Illuminate\Validation\Rule;
- use Inertia\Inertia;
-
- /**
- * Chooses which real product a "featured product" section of the design shows.
- * This changes the working design, so on a live store it shows after "Publish changes".
- */
- class FeaturedProductController extends Controller
- {
- public function update(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse
- {
- $store = $request->user()->stores()->where('slug', $slug)->firstOrFail();
-
- abort_if($store->plan === null, 404);
- abort_unless(in_array($sectionId, array_column(FeaturedProducts::sections($store->plan), 'section_id'), true), 404);
-
- $validated = $request->validate([
- // Only this store's own products that shoppers can see. Empty means "no product".
- 'product_slug' => ['nullable', 'string', Rule::exists('products', 'slug')
- ->where('store_id', $store->id)
- ->where('status', 'active')],
- ], [
- 'product_slug.exists' => __('Choose one of your active products.'),
- ]);
-
- $store->plan = $validator->validate(
- FeaturedProducts::link($store->plan, $sectionId, $validated['product_slug'] ?? null),
- );
- $store->save();
-
- Inertia::flash('toast', ['type' => 'success', 'message' => $store->isPublished()
- ? __('Saved. Click "Publish changes" to show it on your live store.')
- : __('Featured product saved.')]);
-
- return to_route('stores.show', $store->slug);
- }
- }
|