@php $itemGroups = $items->groupBy(fn ($item) => (int) $item['product']->tenant_id); $quoteGroups = $shippingQuotes->groupBy(fn ($quote) => (int) $quote->tenant_id); $selectedQuotes = $quoteGroups->map(fn ($quotes, $tenantId) => $quotes->firstWhere('id', old("shipping_quote_ids.$tenantId")) ?? $quotes->first()); $subtotal = (int) $items->sum('subtotal'); $shipping = (int) $selectedQuotes->sum('price'); $rawDiscount = $coupon?->discount_type === 'fixed' ? $coupon->discount_value : (int) round($subtotal * (($coupon?->discount_value ?? 0) / 100)); $discount = $coupon && $subtotal >= $coupon->min_purchase ? min($rawDiscount, $coupon->max_discount ?? PHP_INT_MAX, $subtotal) : 0; $allShippingReady = $itemGroups->isNotEmpty() && $itemGroups->keys()->diff($quoteGroups->keys())->isEmpty(); $hasPrescription = $items->contains(fn ($item) => $item['policy']['requires_prescription']); $shippingErrorCode = session('shipping_error_code'); $shippingIssueTitle = match ($shippingErrorCode) { 'ADDRESS_INCOMPLETE', 'ADDRESS_INVALID' => 'Alamat pengiriman belum lengkap', 'FULFILLMENT_UNAVAILABLE' => 'Beberapa produk belum siap dikirim', 'PROVIDER_UNAVAILABLE' => 'Layanan pengiriman sedang diperbarui', default => 'Pengiriman belum tersedia', }; $shippingIssueCopy = session('error') ?: match ($shippingErrorCode) { 'ADDRESS_INCOMPLETE', 'ADDRESS_INVALID' => 'Lengkapi kode pos atau pin lokasi agar kami dapat mencari pilihan kurir.', 'FULFILLMENT_UNAVAILABLE' => 'Periksa kembali jumlah dan ketersediaan produk di keranjang Anda.', 'PROVIDER_UNAVAILABLE' => 'Tarif kurir belum dapat dimuat. Coba kembali dalam beberapa saat.', default => 'Belum ada layanan kurir untuk alamat ini. Coba alamat lain atau muat ulang pilihan.', }; $disabledReason = ! $selectedAddress ? 'Pilih alamat pengiriman untuk melanjutkan.' : (! $allShippingReady ? 'Pilih metode pengiriman untuk melanjutkan.' : null); $grandTotal = max(0, $subtotal + $shipping - $discount); @endphp

Langkah 2 dari 3

Checkout

@if($items->isEmpty())

Keranjang Anda masih kosong.

Temukan produk
@else @if($itemGroups->count() > 1)

Pesanan dari beberapa apotek

Produk dapat dikirim secara terpisah oleh masing-masing apotek, tetapi pembayaran tetap dilakukan dalam satu checkout.

@endif

Alamat pengiriman

Ubah
@if($selectedAddress) {{ $selectedAddress->label }} · {{ $selectedAddress->recipient_name }} {{ $selectedAddress->formattedAddress() }} @else
Alamat pengiriman belum dipilih. Tambah alamat
@endif

Pesanan

@foreach($itemGroups as $tenantId => $group) @php $tenant = $group->first()['product']->tenant; $groupQuotes = $quoteGroups->get($tenantId, collect()); $requiresPrescription = $group->contains(fn ($item) => $item['policy']['requires_prescription']); @endphp

{{ $tenant->name }}

{{ $group->sum('quantity') }} produk

@if($requiresPrescription) Perlu resep @endif
@foreach($group as $item)

{{ $item['product']->name }}

{{ $item['variant']->name }} · {{ $item['quantity'] }} × Rp{{ number_format($item['variant']->price, 0, ',', '.') }}

Rp{{ number_format($item['subtotal'], 0, ',', '.') }}
@endforeach

Pengiriman

@forelse($groupQuotes as $quote) @empty
!

{{ $shippingIssueTitle }}

{{ $shippingIssueCopy }}

Ganti alamat
@csrf
@endforelse
@endforeach

Voucher

@if($coupon)

{{ $coupon->code }}

✓ Voucher digunakan

{{ $discount > 0 ? 'Perkiraan hemat Rp'.number_format($discount, 0, ',', '.') : 'Belanja minimum Rp'.number_format($coupon->min_purchase, 0, ',', '.') }}

@csrf
@else @if($claimedCoupons->isNotEmpty()) @endif
@csrf
@endif

Total pembayaran

Rp{{ number_format($grandTotal, 0, ',', '.') }}

@if($disabledReason)

{{ $disabledReason }}

@endif
@if($claimedCoupons->isNotEmpty() && !$coupon) @endif @endif