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.

RendersStorefront.php 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Http\Controllers\Concerns;
  3. use App\Commerce\Cart;
  4. use App\Models\Product;
  5. use App\Models\Store;
  6. use App\Storefront\Storefront;
  7. use App\StorePlan\FeaturedProducts;
  8. use App\StorePlan\PlanSchema;
  9. use Illuminate\Http\Request;
  10. use Inertia\Inertia;
  11. use Inertia\Response;
  12. /**
  13. * Shared by the controllers that show store pages, both live and in the owner's preview.
  14. */
  15. trait RendersStorefront
  16. {
  17. private function storefront(Request $request): Storefront
  18. {
  19. return Storefront::fromRequest($request);
  20. }
  21. private function cart(Request $request, Store $store): Cart
  22. {
  23. return new Cart($request->session(), $store);
  24. }
  25. /**
  26. * Every store page gets the same basics: the design, the fonts, the product list and the cart.
  27. *
  28. * @param array<string, mixed> $view Which page to show.
  29. */
  30. private function renderStorefront(Request $request, Storefront $storefront, array $view): Response
  31. {
  32. $store = $storefront->store;
  33. return Inertia::render('stores/preview', [
  34. 'storeName' => $store->name,
  35. 'language' => $store->language->value,
  36. 'plan' => $storefront->plan,
  37. 'fonts' => PlanSchema::FONTS,
  38. // Links inside the store are built on top of this address. It is empty for a live store.
  39. 'baseUrl' => $storefront->baseUrl,
  40. 'preview' => $storefront->preview,
  41. 'view' => $view,
  42. 'products' => $store->products()
  43. ->active()
  44. ->with('images')
  45. ->latest('id')
  46. ->limit(60)
  47. ->get()
  48. ->map(fn (Product $product) => $product->toStorefront()),
  49. // Products that "featured product" sections point to, by web-address name.
  50. // Fetched separately so a featured product always shows, however many products the store has.
  51. 'featuredProducts' => (object) $store->products()
  52. ->active()
  53. ->with('images')
  54. ->whereIn('slug', FeaturedProducts::slugs($storefront->plan))
  55. ->get()
  56. ->mapWithKeys(fn (Product $product) => [$product->slug => $product->toStorefront()])
  57. ->all(),
  58. 'cart' => $this->cart($request, $store)->toStorefront(),
  59. 'shop' => [
  60. // Digits only, such as 60123456789, or null when the merchant has not set one.
  61. 'whatsapp' => $store->whatsapp_number,
  62. // In ringgit, or null when shipping is never free.
  63. 'free_shipping_over' => $store->free_shipping_over !== null ? $store->free_shipping_over / 100 : null,
  64. ],
  65. ]);
  66. }
  67. }