| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
-
- namespace App\Http\Controllers\Concerns;
-
- use App\Commerce\Cart;
- use App\Models\AutomaticDiscount;
- use App\Models\Product;
- use App\Models\Store;
- use App\Storefront\Storefront;
- use App\StorePlan\FeaturedProducts;
- use App\StorePlan\HeroImages;
- use App\StorePlan\PlanSchema;
- use Illuminate\Http\Request;
- use Inertia\Inertia;
- use Inertia\Response;
-
- /**
- * Shared by the controllers that show store pages, both live and in the owner's preview.
- */
- trait RendersStorefront
- {
- private function storefront(Request $request): Storefront
- {
- return Storefront::fromRequest($request);
- }
-
- private function cart(Request $request, Store $store): Cart
- {
- return new Cart($request->session(), $store);
- }
-
- /**
- * Every store page gets the same basics: the design, the fonts, the product list and the cart.
- *
- * @param array<string, mixed> $view Which page to show.
- */
- private function renderStorefront(Request $request, Storefront $storefront, array $view): Response
- {
- $store = $storefront->store;
- $cart = $this->cart($request, $store);
-
- return Inertia::render('stores/preview', [
- 'storeName' => $store->name,
- 'language' => $store->language->value,
- 'plan' => $storefront->plan,
- 'fonts' => PlanSchema::FONTS,
- // Links inside the store are built on top of this address. It is empty for a live store.
- 'baseUrl' => $storefront->baseUrl,
- 'preview' => $storefront->preview,
- 'view' => $view,
- 'products' => $store->products()
- ->active()
- ->with(Product::STOREFRONT_RELATIONS)
- ->latest('id')
- ->limit(60)
- ->get()
- ->map(fn (Product $product) => $product->toStorefront()),
- // Products that "featured product" sections point to, by web-address name.
- // Fetched separately so a featured product always shows, however many products the store has.
- 'featuredProducts' => (object) $store->products()
- ->active()
- ->with(Product::STOREFRONT_RELATIONS)
- ->whereIn('slug', FeaturedProducts::slugs($storefront->plan))
- ->get()
- ->mapWithKeys(fn (Product $product) => [$product->slug => $product->toStorefront()])
- ->all(),
- // Photos for hero sections: the ones the design points to, and product photos to use otherwise.
- 'heroImages' => HeroImages::forStorefront($store, $storefront->plan),
- 'cart' => $cart->toStorefront(),
- 'shop' => [
- // Digits only, such as 60123456789, or null when the merchant has not set one.
- 'whatsapp' => $store->whatsapp_number,
- // In ringgit, or null when shipping is never free.
- 'free_shipping_over' => $store->free_shipping_over !== null ? $store->free_shipping_over / 100 : null,
- // Automatic discounts running now, so product pages can mention them.
- 'offers' => $cart->liveOffers()->map(fn (AutomaticDiscount $offer) => [
- 'name' => $offer->name,
- 'text' => $offer->describe($store->language->value),
- 'product_ids' => $offer->product_ids,
- ])->values()->all(),
- ],
- ]);
- }
- }
|