@php
$products = Cart::instance('cart')->products();
$miniCartContents = Cart::instance('cart')->content();
$miniCartLineOffers = [];
$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 = 0;
foreach ($miniCartContents as $ci) {
if ((bool)($ci->options['is_free_gift'] ?? false)) continue;
$ciProduct = $products->find($ci->id);
if (!$ciProduct) continue;
if (
in_array((int)$ci->id, $buyProductIds, true) ||
in_array((int)$ciProduct->original_product->id, $buyProductIds, true)
) {
$buyCartQuantity += (int)$ci->qty;
}
}
$eligibleQty = (int)floor($buyCartQuantity / $buyQuantity);
if ($eligibleQty < 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);
$remainingQty = $eligibleQty;
foreach ($miniCartContents as $ci) {
if ($remainingQty < 1 || (bool)($ci->options['is_free_gift'] ?? false)) continue;
$ciProduct = $products->find($ci->id);
if (!$ciProduct) continue;
if (
(int)$ci->id !== $offerProductId &&
(int)$ciProduct->original_product->id !== $offerProductId
) {
continue;
}
$discountQty = min((int)$ci->qty, $remainingQty);
$unitPrice = EcommerceHelper::isTaxEnabled() && $ci->priceTax > 0 ? (float)$ci->priceTax : (float)$ci->price;
$discountAmount = $unitPrice * ($discountPercent / 100) * $discountQty;
if ($discountAmount > 0) {
$miniCartLineOffers[$ci->rowId] = [
'amount' => ($miniCartLineOffers[$ci->rowId]['amount'] ?? 0) + $discountAmount,
'percent' => $discountPercent,
'discount_qty' => ($miniCartLineOffers[$ci->rowId]['discount_qty'] ?? 0) + $discountQty,
'type' => 'buy_x_get_y_percent',
];
}
$remainingQty -= $discountQty;
}
}
}
$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();
$miniCartRawTotal = (float) Cart::instance('cart')->rawSubTotal();
foreach ($quantityBasedPromotions as $promotion) {
if (
$promotion->target === \Botble\Ecommerce\Enums\DiscountTargetEnum::MINIMUM_ORDER_AMOUNT &&
(float) $promotion->min_order_price > $miniCartRawTotal
) {
continue;
}
$eligibleCartItems = $miniCartContents
->reject(fn($ci) => (bool)($ci->options['is_free_gift'] ?? false))
->filter(function ($ci) use ($promotion, $products) {
$ciProduct = $products->find($ci->id);
return match ($promotion->target) {
\Botble\Ecommerce\Enums\DiscountTargetEnum::SPECIFIC_PRODUCT,
\Botble\Ecommerce\Enums\DiscountTargetEnum::PRODUCT_VARIANT => in_array(
(int)$ci->id,
$promotion->products->pluck('id')->map(fn($id) => (int)$id)->all(),
true
) || ($ciProduct && in_array(
(int)$ciProduct->original_product->id,
$promotion->products->pluck('id')->map(fn($id) => (int)$id)->all(),
true
)),
\Botble\Ecommerce\Enums\DiscountTargetEnum::PRODUCT_COLLECTIONS => $ciProduct && $ciProduct->original_product
->productCollections()
->pluck('ec_product_collections.id')
->intersect($promotion->productCollections->pluck('id'))
->isNotEmpty(),
\Botble\Ecommerce\Enums\DiscountTargetEnum::PRODUCT_CATEGORIES => $ciProduct && $ciProduct->original_product
->categories()
->pluck('ec_product_categories.id')
->intersect($promotion->productCategories->pluck('id'))
->isNotEmpty(),
default => true,
};
});
$requiredQuantity = max((int)$promotion->product_quantity, 1);
$discountPercent = min(max((float)$promotion->value, 0), 100);
if ($discountPercent <= 0 || $eligibleCartItems->sum('qty') < $requiredQuantity) {
continue;
}
foreach ($eligibleCartItems as $ci) {
$unitPrice = EcommerceHelper::isTaxEnabled() && $ci->priceTax > 0 ? (float)$ci->priceTax : (float)$ci->price;
$discountAmount = $unitPrice * (int)$ci->qty * ($discountPercent / 100);
if ($discountAmount > 0) {
$miniCartLineOffers[$ci->rowId] = [
'amount' => ($miniCartLineOffers[$ci->rowId]['amount'] ?? 0) + $discountAmount,
'percent' => $miniCartLineOffers[$ci->rowId]['percent'] ?? $discountPercent,
'discount_qty' => (int) $ci->qty,
'type' => $miniCartLineOffers[$ci->rowId]['type'] ?? 'quantity_based',
];
}
}
}
$miniCartContents = $miniCartContents
->sortBy(fn($ci) => (bool)($ci->options['is_free_gift'] ?? false));
@endphp
@if (count($products))
@foreach($miniCartContents as $key => $cartItem)
@php
$product = $products->find($cartItem->id);
$isFreeGift = (bool) ($cartItem->options['is_free_gift'] ?? false);
$miniCartLineOffer = $miniCartLineOffers[$cartItem->rowId] ?? null;
$miniCartUnitPrice = EcommerceHelper::isTaxEnabled() && $cartItem->priceTax > 0 ? (float)$cartItem->priceTax : (float)$cartItem->price;
$miniCartLineTotal = $miniCartUnitPrice * (int)$cartItem->qty;
$miniCartLineOfferAmount = $miniCartLineOffer ? min((float)$miniCartLineOffer['amount'], (float)$miniCartLineTotal) : 0;
$miniCartDiscountQty = $miniCartLineOffer ? min((int) ($miniCartLineOffer['discount_qty'] ?? $cartItem->qty), (int) $cartItem->qty) : 0;
$miniCartShouldSplit = ! $isFreeGift
&& $miniCartLineOfferAmount > 0
&& ($miniCartLineOffer['type'] ?? null) === 'buy_x_get_y_percent'
&& $miniCartDiscountQty > 0
&& $miniCartDiscountQty < (int) $cartItem->qty;
$miniCartRegularQty = (int) $cartItem->qty - $miniCartDiscountQty;
@endphp
@if (!empty($product))
@if ($miniCartShouldSplit)
{{ $product->original_product->name }}
{{ $miniCartDiscountQty }} x
{{ format_price($miniCartUnitPrice * (1 - ((float)$miniCartLineOffer['percent'] / 100))) }}
{{ format_price($miniCartUnitPrice) }}
{{ __('Offer') }}
{{ (float)$miniCartLineOffer['percent'] }}% {{ __('off') }}
@if (!empty($cartItem->options['attributes']))
{{ $cartItem->options['attributes'] }}
@endif
@if (is_plugin_active('marketplace') && $product->original_product->store->id)
{{ __('Sold by') }}: {{ $product->original_product->store->name }}
@endif
@else
@unless ($isFreeGift)
@endunless
{{ $product->original_product->name }}
@if ($isFreeGift)
🎁 {{ __('Free Gift') }}
@endif
@if ($product->isOutOfStock())
({!! $product->stock_status_html !!})
@endif
{{ $cartItem->qty }} x
@if ($isFreeGift)
{{ __('FREE') }}
@if (($cartItem->options['gift_original_price'] ?? 0) > 0)
{{ format_price($cartItem->options['gift_original_price']) }}
@endif
@else
@php
$miniCartUnitPriceAfterOffer = (int)$cartItem->qty > 0
? max(($miniCartLineTotal - $miniCartLineOfferAmount) / (int)$cartItem->qty, 0)
: $miniCartUnitPrice;
@endphp
@if ($miniCartLineOfferAmount > 0)
{{ format_price($miniCartUnitPriceAfterOffer) }}
{{ format_price($miniCartUnitPrice) }}
{{ (float)$miniCartLineOffer['percent'] }}% {{ __('off') }}
@else
{{ format_price($cartItem->price) }}
@if ($product->front_sale_price != $product->price)
{{ format_price($product->price) }}
@endif
@endif
@endif
@if (!empty($cartItem->options['attributes']))
{{ $cartItem->options['attributes'] }}
@endif
@if (!empty($cartItem->options['options']))
{!! render_product_options_info($cartItem->options['options'], $product, true) !!}
@endif
@if (!empty($cartItem->options['extras']) && is_array($cartItem->options['extras']))
@foreach($cartItem->options['extras'] as $option)
@if (!empty($option['key']) && !empty($option['value']) && strtolower($option['key']) !== 'price')
{{ $option['key'] }}: {{ $option['value'] }}
@endif
@endforeach
@endif
@if (is_plugin_active('marketplace') && $product->original_product->store->id)
{{ __('Sold by') }}: {{ $product->original_product->store->name }}
@endif
@endif
@endif
@endforeach
@endif