AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

RendersStorefront.php 4.4KB

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