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 3.9KB

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