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

RendersStorefront.php 3.4KB

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