AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

RendersStorefront.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Http\Controllers\Concerns;
  3. use App\Commerce\Cart;
  4. use App\Models\AutomaticDiscount;
  5. use App\Models\Collection;
  6. use App\Models\Product;
  7. use App\Models\Store;
  8. use App\Storefront\Announcements;
  9. use App\Storefront\Logos\StoreLogos;
  10. use App\Storefront\Storefront;
  11. use App\StorePlan\FeaturedProducts;
  12. use App\StorePlan\HeroImages;
  13. use App\StorePlan\PlanSchema;
  14. use Illuminate\Http\Request;
  15. use Inertia\Inertia;
  16. use Inertia\Response;
  17. /**
  18. * Shared by the controllers that show store pages, both live and in the owner's preview.
  19. */
  20. trait RendersStorefront
  21. {
  22. private function storefront(Request $request): Storefront
  23. {
  24. return Storefront::fromRequest($request);
  25. }
  26. private function cart(Request $request, Store $store): Cart
  27. {
  28. return new Cart($request->session(), $store);
  29. }
  30. /**
  31. * Every store page gets the same basics: the design, the fonts, the product list and the cart.
  32. *
  33. * @param array<string, mixed> $view Which page to show.
  34. */
  35. private function renderStorefront(Request $request, Storefront $storefront, array $view): Response
  36. {
  37. $store = $storefront->store;
  38. $cart = $this->cart($request, $store);
  39. return Inertia::render('stores/preview', [
  40. 'storeName' => $store->name,
  41. 'language' => $store->language->value,
  42. 'plan' => $storefront->plan,
  43. 'fonts' => PlanSchema::FONTS,
  44. // Links inside the store are built on top of this address. It is empty for a live store.
  45. 'baseUrl' => $storefront->baseUrl,
  46. 'preview' => $storefront->preview,
  47. 'view' => $view,
  48. 'products' => $store->products()
  49. ->active()
  50. ->with(Product::STOREFRONT_RELATIONS)
  51. ->latest('id')
  52. ->limit(60)
  53. ->get()
  54. ->map(fn (Product $product) => $product->toStorefront()),
  55. // Products that "featured product" sections point to, by web-address name.
  56. // Fetched separately so a featured product always shows, however many products the store has.
  57. 'featuredProducts' => (object) $store->products()
  58. ->active()
  59. ->with(Product::STOREFRONT_RELATIONS)
  60. ->whereIn('slug', FeaturedProducts::slugs($storefront->plan))
  61. ->get()
  62. ->mapWithKeys(fn (Product $product) => [$product->slug => $product->toStorefront()])
  63. ->all(),
  64. // Photos for hero sections: the ones the design points to, and product photos to use otherwise.
  65. 'heroImages' => HeroImages::forStorefront($store, $storefront->plan),
  66. 'cart' => $cart->toStorefront(),
  67. 'shop' => [
  68. // Digits only, such as 60123456789, or null when the merchant has not set one.
  69. 'whatsapp' => $store->whatsapp_number,
  70. // The store's logo (uploaded, a ready design, or drawn by AI), or null to show the name.
  71. 'logo' => app(StoreLogos::class)->present($store),
  72. // In ringgit, or null when shipping is never free.
  73. 'free_shipping_over' => $store->free_shipping_over !== null ? $store->free_shipping_over / 100 : null,
  74. // Lines for the slim bar above the header. Empty when the merchant switched it off.
  75. 'announcements' => Announcements::for($store, $cart->liveOffers()),
  76. // Shipping fees in ringgit, for the trust row and the product page.
  77. 'shipping_from' => min($store->shipping_west, $store->shipping_east) / 100,
  78. 'shipping_west' => $store->shipping_west / 100,
  79. 'shipping_east' => $store->shipping_east / 100,
  80. // Collections with at least one product shoppers can see, for "shop by" sections and the shop page.
  81. 'collections' => $store->collections()
  82. ->with(['products' => fn ($query) => $query->active()->with('images')])
  83. ->get()
  84. ->map(fn (Collection $collection) => $collection->toStorefront())
  85. ->filter(fn (array $collection) => $collection['count'] > 0)
  86. ->values()
  87. ->all(),
  88. // Automatic discounts running now, so product pages can mention them.
  89. 'offers' => $cart->liveOffers()->map(fn (AutomaticDiscount $offer) => [
  90. 'name' => $offer->name,
  91. 'text' => $offer->describe($store->language->value),
  92. 'product_ids' => $offer->product_ids,
  93. ])->values()->all(),
  94. ],
  95. ]);
  96. }
  97. }