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

RendersStorefront.php 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // The cheapest shipping fee in ringgit, for the trust row.
  73. 'shipping_from' => min($store->shipping_west, $store->shipping_east) / 100,
  74. // Automatic discounts running now, so product pages can mention them.
  75. 'offers' => $cart->liveOffers()->map(fn (AutomaticDiscount $offer) => [
  76. 'name' => $offer->name,
  77. 'text' => $offer->describe($store->language->value),
  78. 'product_ids' => $offer->product_ids,
  79. ])->values()->all(),
  80. ],
  81. ]);
  82. }
  83. }