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

RendersStorefront.php 3.1KB

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