AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FeaturedProductController.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Store;
  4. use App\StorePlan\FeaturedProducts;
  5. use App\StorePlan\PlanValidator;
  6. use Illuminate\Http\RedirectResponse;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Validation\Rule;
  9. use Inertia\Inertia;
  10. /**
  11. * Chooses which real product a "featured product" section of the design shows.
  12. * This changes the working design, so on a live store it shows after "Publish changes".
  13. */
  14. class FeaturedProductController extends Controller
  15. {
  16. public function update(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse
  17. {
  18. $store = $request->user()->stores()->where('slug', $slug)->firstOrFail();
  19. abort_if($store->plan === null, 404);
  20. abort_unless(in_array($sectionId, array_column(FeaturedProducts::sections($store->plan), 'section_id'), true), 404);
  21. $validated = $request->validate([
  22. // Only this store's own products that shoppers can see. Empty means "no product".
  23. 'product_slug' => ['nullable', 'string', Rule::exists('products', 'slug')
  24. ->where('store_id', $store->id)
  25. ->where('status', 'active')],
  26. ], [
  27. 'product_slug.exists' => __('Choose one of your active products.'),
  28. ]);
  29. $store->plan = $validator->validate(
  30. FeaturedProducts::link($store->plan, $sectionId, $validated['product_slug'] ?? null),
  31. );
  32. $store->save();
  33. Inertia::flash('toast', ['type' => 'success', 'message' => $store->isPublished()
  34. ? __('Saved. Click "Publish changes" to show it on your live store.')
  35. : __('Featured product saved.')]);
  36. return to_route('stores.show', $store->slug);
  37. }
  38. }