{!! apply_filters(RENDER_PRODUCTS_IN_CHECKOUT_PAGE, $products) !!} @php $checkoutDiscounts = $discounts ?? null; if ($checkoutDiscounts === null) { $checkoutDiscountsQuery = \Botble\Ecommerce\Models\Discount::query() ->where('type', \Botble\Ecommerce\Enums\DiscountTypeEnum::COUPON) ->where('display_at_checkout', true) ->active() ->available(); $checkoutDiscounts = apply_filters('ecommerce_checkout_discounts_query', $checkoutDiscountsQuery, $products)->get(); } @endphp @include(EcommerceHelper::viewPath('discounts.partials.form'), ['discounts' => $checkoutDiscounts])
@php $checkoutCartItems = Cart::instance('cart')->content(); $checkoutFreeGiftDiscountAmount = $checkoutCartItems->sum(function ($cartItem): float { if (! (bool) ($cartItem->options['is_free_gift'] ?? false)) { return 0; } $giftOriginalPrice = (float) ($cartItem->options['gift_original_price'] ?? ($cartItem->priceTax == 0 ? $cartItem->price : $cartItem->priceTax)); return $giftOriginalPrice * (int) $cartItem->qty; }); $checkoutPaidCartItems = $checkoutCartItems->reject(fn ($cartItem) => (bool) ($cartItem->options['is_free_gift'] ?? false)); $checkoutSubtotal = $displayRawTotal ?? Cart::instance('cart')->rawTotalByItems($checkoutPaidCartItems); $checkoutRawPromotionDiscountAmount = (float) ($displayPromotionDiscountAmount ?? $promotionDiscountAmount ?? 0); if ($checkoutFreeGiftDiscountAmount > 0 && abs($checkoutRawPromotionDiscountAmount - $checkoutFreeGiftDiscountAmount) < 0.01) { $checkoutPromotionDiscountAmount = 0; } elseif ($checkoutRawPromotionDiscountAmount > $checkoutFreeGiftDiscountAmount) { $checkoutPromotionDiscountAmount = $checkoutRawPromotionDiscountAmount - $checkoutFreeGiftDiscountAmount; } else { $checkoutPromotionDiscountAmount = $checkoutRawPromotionDiscountAmount; } $marketplacePromotionDiscountAmount = collect(Arr::get($sessionCheckoutData ?? [], 'marketplace', [])) ->sum(function ($storeData) { if (Arr::has($storeData, 'display_promotion_discount_amount')) { return (float) Arr::get($storeData, 'display_promotion_discount_amount', 0); } $storePromotionDiscountAmount = (float) Arr::get($storeData, 'promotion_discount_amount', 0); $storeFreeGiftDiscountAmount = (float) Arr::get($storeData, 'free_gift_discount_amount', 0); if ($storePromotionDiscountAmount <= 0) { return 0; } if ($storeFreeGiftDiscountAmount > 0 && abs($storePromotionDiscountAmount - $storeFreeGiftDiscountAmount) < 0.01) { return 0; } if ($storePromotionDiscountAmount > $storeFreeGiftDiscountAmount) { return $storePromotionDiscountAmount - $storeFreeGiftDiscountAmount; } return $storePromotionDiscountAmount; }); $checkoutLinePromotionDiscountAmount = 0; $checkoutCartProducts = Cart::instance('cart')->products(); $buyXGetYPercentPromotions = \Botble\Ecommerce\Models\Discount::query() ->where('type', \Botble\Ecommerce\Enums\DiscountTypeEnum::PROMOTION) ->where('type_option', \Botble\Ecommerce\Enums\DiscountTypeOptionEnum::BUY_X_GET_Y_DISCOUNT) ->where('start_date', '<=', \Carbon\Carbon::now()) ->where(fn ($query) => $query->whereNull('end_date')->orWhere('end_date', '>=', \Carbon\Carbon::now())) ->with('products') ->get(); foreach ($buyXGetYPercentPromotions as $promotion) { $buyProductIds = $promotion->products->pluck('id')->map(fn ($id) => (int) $id)->all(); if (empty($buyProductIds)) { continue; } $buyQuantity = max((int) $promotion->product_quantity, 1); $buyCartQuantity = $checkoutPaidCartItems->sum(function ($cartItem) use ($buyProductIds, $checkoutCartProducts): int { $product = $checkoutCartProducts->firstWhere('id', $cartItem->id); if (! $product) { return 0; } return in_array((int) $cartItem->id, $buyProductIds, true) || in_array((int) $product->original_product->id, $buyProductIds, true) ? (int) $cartItem->qty : 0; }); $eligibleQuantity = (int) floor($buyCartQuantity / $buyQuantity); if ($eligibleQuantity < 1) { continue; } foreach (\Illuminate\Support\Arr::wrap(data_get($promotion->apply_conditions, 'free_products', [])) as $offerProduct) { $offerProductId = (int) data_get($offerProduct, 'product_id'); $discountPercent = min(max((float) data_get($offerProduct, 'discount_percent', $promotion->value), 1), 100); $remainingQuantity = $eligibleQuantity; foreach ($checkoutPaidCartItems as $cartItem) { if ($remainingQuantity < 1) { continue; } $product = $checkoutCartProducts->firstWhere('id', $cartItem->id); if ( ! $product || ( (int) $cartItem->id !== $offerProductId && (int) $product->original_product->id !== $offerProductId ) ) { continue; } $discountQuantity = min((int) $cartItem->qty, $remainingQuantity); $unitPrice = EcommerceHelper::isTaxEnabled() && $cartItem->priceTax > 0 ? (float) $cartItem->priceTax : (float) $cartItem->price; $checkoutLinePromotionDiscountAmount += $unitPrice * ($discountPercent / 100) * $discountQuantity; $remainingQuantity -= $discountQuantity; } } } $quantityBasedPromotions = \Botble\Ecommerce\Models\Discount::query() ->where('type', \Botble\Ecommerce\Enums\DiscountTypeEnum::PROMOTION) ->where('type_option', \Botble\Ecommerce\Enums\DiscountTypeOptionEnum::QUANTITY_BASED) ->where('start_date', '<=', \Carbon\Carbon::now()) ->where(fn ($query) => $query->whereNull('end_date')->orWhere('end_date', '>=', \Carbon\Carbon::now())) ->with(['products', 'productCollections', 'productCategories']) ->get(); foreach ($quantityBasedPromotions as $promotion) { if ( $promotion->target === \Botble\Ecommerce\Enums\DiscountTargetEnum::MINIMUM_ORDER_AMOUNT && (float) $promotion->min_order_price > (float) $checkoutSubtotal ) { continue; } $eligibleCartItems = $checkoutPaidCartItems->filter(function ($cartItem) use ($promotion, $checkoutCartProducts) { $product = $checkoutCartProducts->firstWhere('id', $cartItem->id); return match ($promotion->target) { \Botble\Ecommerce\Enums\DiscountTargetEnum::SPECIFIC_PRODUCT, \Botble\Ecommerce\Enums\DiscountTargetEnum::PRODUCT_VARIANT => in_array( (int) $cartItem->id, $promotion->products->pluck('id')->map(fn ($id) => (int) $id)->all(), true ) || ($product && in_array( (int) $product->original_product->id, $promotion->products->pluck('id')->map(fn ($id) => (int) $id)->all(), true )), \Botble\Ecommerce\Enums\DiscountTargetEnum::PRODUCT_COLLECTIONS => $product && $product->original_product ->productCollections() ->pluck('ec_product_collections.id') ->intersect($promotion->productCollections->pluck('id')) ->isNotEmpty(), \Botble\Ecommerce\Enums\DiscountTargetEnum::PRODUCT_CATEGORIES => $product && $product->original_product ->categories() ->pluck('ec_product_categories.id') ->intersect($promotion->productCategories->pluck('id')) ->isNotEmpty(), default => true, }; }); $discountPercent = min(max((float) $promotion->value, 0), 100); if ($discountPercent <= 0 || $eligibleCartItems->sum('qty') < max((int) $promotion->product_quantity, 1)) { continue; } $checkoutLinePromotionDiscountAmount += $eligibleCartItems->sum(function ($cartItem) use ($discountPercent): float { $unitPrice = EcommerceHelper::isTaxEnabled() && $cartItem->priceTax > 0 ? (float) $cartItem->priceTax : (float) $cartItem->price; return $unitPrice * (int) $cartItem->qty * ($discountPercent / 100); }); } $checkoutPromotionDiscountAmount = max( (float) $checkoutPromotionDiscountAmount, (float) $marketplacePromotionDiscountAmount, (float) $checkoutLinePromotionDiscountAmount ); $checkoutTotal = max($checkoutSubtotal - (float)$checkoutPromotionDiscountAmount - (float)$couponDiscountAmount, 0) + (float)($shippingAmount ?? 0) + (float)($paymentFee ?? 0); @endphp

{{ __('Subtotal') }}:

{{ format_price($checkoutSubtotal) }}

{{-- @if (EcommerceHelper::isTaxEnabled())

{{ __('Tax') }} @if (Cart::instance('cart')->rawTax()) ({{ Cart::instance('cart')->taxClassesName() }}) @endif

{{ format_price(Cart::instance('cart')->rawTax()) }}

@endif --}} @if (session('applied_coupon_code'))

{{ __('Coupon code') }}:

{{ session('applied_coupon_code') }}

@endif @if ($couponDiscountAmount > 0)

{{ __('Coupon code discount amount') }}:

{{ format_price($couponDiscountAmount) }}

@endif @if ($checkoutPromotionDiscountAmount > 0)

{{ __('Promotion discount') }}:

-{{ format_price($checkoutPromotionDiscountAmount) }}

@endif @if (!empty($shipping) && Arr::get($sessionCheckoutData, 'is_available_shipping', true))

{{ __('Shipping fee') }}:

{{ format_price($shippingAmount) }}

@endif @if (isset($paymentFee) && $paymentFee > 0)

{{ __('plugins/payment::payment.payment_fee') }}:

{{ format_price($paymentFee) }}

@endif

{{ __('Total') }}:

{{ format_price($checkoutTotal) }}