| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
-
- namespace App\Http\Controllers\Concerns;
-
- use App\Commerce\Cart;
- use App\Models\Product;
- use App\Models\Store;
- use App\Storefront\Storefront;
- 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;
-
- 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('images')
- ->latest('id')
- ->limit(60)
- ->get()
- ->map(fn (Product $product) => $product->toStorefront()),
- 'cart' => $this->cart($request, $store)->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,
- ],
- ]);
- }
- }
|