2019-12-21 05:25:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\CartRule\Helpers;
|
|
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use Webkul\CartRule\Repositories\CartRuleRepository;
|
|
|
|
|
use Webkul\CartRule\Repositories\CartRuleCouponRepository;
|
|
|
|
|
use Webkul\CartRule\Repositories\CartRuleCouponUsageRepository;
|
|
|
|
|
use Webkul\CartRule\Repositories\CartRuleCustomerRepository;
|
|
|
|
|
use Webkul\Customer\Repositories\CustomerGroupRepository;
|
2020-02-25 08:33:47 +00:00
|
|
|
use Webkul\Checkout\Models\CartItem;
|
2019-12-21 05:25:32 +00:00
|
|
|
use Webkul\Rule\Helpers\Validator;
|
|
|
|
|
use Webkul\Checkout\Facades\Cart;
|
|
|
|
|
|
|
|
|
|
class CartRule
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* CartRuleRepository object
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @var \Webkul\CartRule\Repositories\CartRuleRepository
|
2019-12-21 05:25:32 +00:00
|
|
|
*/
|
|
|
|
|
protected $cartRuleRepository;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* CartRuleCouponRepository object
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @var \Webkul\CartRule\Repositories\CartRuleCouponRepository
|
2019-12-21 05:25:32 +00:00
|
|
|
*/
|
|
|
|
|
protected $cartRuleCouponRepository;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* CartRuleCouponUsageRepository object
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @var \Webkul\CartRule\Repositories\CartRuleCouponUsageRepository
|
2019-12-21 05:25:32 +00:00
|
|
|
*/
|
|
|
|
|
protected $cartRuleCouponUsageRepository;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* CartRuleCustomerRepository object
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @var \Webkul\CartRule\Repositories\CartRuleCustomerRepository
|
2019-12-21 05:25:32 +00:00
|
|
|
*/
|
|
|
|
|
protected $cartRuleCustomerRepository;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* CustomerGroupRepository object
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @var \Webkul\Customer\Repositories\CustomerGroupRepository
|
2019-12-21 05:25:32 +00:00
|
|
|
*/
|
|
|
|
|
protected $customerGroupRepository;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Validator object
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @var \Webkul\Rule\Helpers\Validator
|
2019-12-21 05:25:32 +00:00
|
|
|
*/
|
|
|
|
|
protected $validator;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $itemTotals = [];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Create a new helper instance.
|
|
|
|
|
*
|
2020-03-05 14:19:14 +00:00
|
|
|
* @param \Webkul\CartRule\Repositories\CartRuleRepository $cartRuleRepository
|
|
|
|
|
* @param \Webkul\CartRule\Repositories\CartRuleCouponRepository $cartRuleCouponRepository
|
2020-03-05 05:34:57 +00:00
|
|
|
* @param \Webkul\CartRule\Repositories\CartRuleCouponUsageRepository $cartRuleCouponUsageRepository
|
2020-03-05 14:19:14 +00:00
|
|
|
* @param \Webkul\CartRule\Repositories\CartRuleCustomerRepository $cartRuleCustomerRepository
|
|
|
|
|
* @param \Webkul\Customer\Repositories\CustomerGroupRepository $customerGroupRepository
|
|
|
|
|
* @param \Webkul\Rule\Helpers\Validator $validator
|
2020-02-26 15:41:40 +00:00
|
|
|
*
|
2019-12-21 05:25:32 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
CartRuleRepository $cartRuleRepository,
|
|
|
|
|
CartRuleCouponRepository $cartRuleCouponRepository,
|
|
|
|
|
CartRuleCouponUsageRepository $cartRuleCouponUsageRepository,
|
|
|
|
|
CartRuleCustomerRepository $cartRuleCustomerRepository,
|
|
|
|
|
CustomerGroupRepository $customerGroupRepository,
|
|
|
|
|
Validator $validator
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
$this->cartRuleRepository = $cartRuleRepository;
|
|
|
|
|
|
|
|
|
|
$this->cartRuleCouponRepository = $cartRuleCouponRepository;
|
|
|
|
|
|
|
|
|
|
$this->cartRuleCouponUsageRepository = $cartRuleCouponUsageRepository;
|
|
|
|
|
|
|
|
|
|
$this->cartRuleCustomerRepository = $cartRuleCustomerRepository;
|
|
|
|
|
|
|
|
|
|
$this->customerGroupRepository = $customerGroupRepository;
|
|
|
|
|
|
|
|
|
|
$this->validator = $validator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Collect discount on cart
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function collect()
|
|
|
|
|
{
|
|
|
|
|
$cart = Cart::getCart();
|
2020-02-25 08:33:47 +00:00
|
|
|
$appliedCartRuleIds = [];
|
2019-12-21 05:25:32 +00:00
|
|
|
|
|
|
|
|
$this->calculateCartItemTotals($cart->items()->get());
|
|
|
|
|
|
|
|
|
|
foreach ($cart->items()->get() as $item) {
|
2020-02-25 08:33:47 +00:00
|
|
|
$itemCartRuleIds = $this->process($item);
|
|
|
|
|
$appliedCartRuleIds = array_merge($appliedCartRuleIds, $itemCartRuleIds);
|
2019-12-25 14:27:05 +00:00
|
|
|
|
2020-02-19 11:49:52 +00:00
|
|
|
if ($item->children()->count() && $item->product->getTypeInstance()->isChildrenCalculated()) {
|
2020-02-26 17:31:49 +00:00
|
|
|
$this->divideDiscount($item);
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-25 08:33:47 +00:00
|
|
|
$cart->applied_cart_rule_ids = implode(',', array_unique($appliedCartRuleIds, SORT_REGULAR));
|
|
|
|
|
$cart->save();
|
|
|
|
|
$cart->refresh();
|
|
|
|
|
|
2019-12-21 05:25:32 +00:00
|
|
|
$this->processShippingDiscount($cart);
|
|
|
|
|
|
|
|
|
|
$this->processFreeShippingDiscount($cart);
|
|
|
|
|
|
|
|
|
|
$this->validateCouponCode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns cart rules
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @return \Illuminate\Support\Collection
|
2019-12-21 05:25:32 +00:00
|
|
|
*/
|
|
|
|
|
public function getCartRules()
|
|
|
|
|
{
|
|
|
|
|
static $cartRules;
|
|
|
|
|
|
2020-02-19 11:49:52 +00:00
|
|
|
if ($cartRules) {
|
2019-12-21 05:25:32 +00:00
|
|
|
return $cartRules;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
|
|
|
|
|
$customerGroupId = null;
|
|
|
|
|
|
|
|
|
|
if (Cart::getCurrentCustomer()->check()) {
|
|
|
|
|
$customerGroupId = Cart::getCurrentCustomer()->user()->customer_group_id;
|
|
|
|
|
} else {
|
2020-02-19 11:49:52 +00:00
|
|
|
if ($customerGuestGroup = $this->customerGroupRepository->findOneByField('code', 'guest')) {
|
2019-12-21 05:25:32 +00:00
|
|
|
$customerGroupId = $customerGuestGroup->id;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-26 15:41:40 +00:00
|
|
|
$cartRules = $this->cartRuleRepository->scopeQuery(function ($query) use ($customerGroupId) {
|
2019-12-21 05:25:32 +00:00
|
|
|
return $query->leftJoin('cart_rule_customer_groups', 'cart_rules.id', '=', 'cart_rule_customer_groups.cart_rule_id')
|
2020-02-27 08:03:03 +00:00
|
|
|
->leftJoin('cart_rule_channels', 'cart_rules.id', '=', 'cart_rule_channels.cart_rule_id')
|
|
|
|
|
->where('cart_rule_customer_groups.customer_group_id', $customerGroupId)
|
|
|
|
|
->where('cart_rule_channels.channel_id', core()->getCurrentChannel()->id)
|
|
|
|
|
->where(function ($query1) {
|
|
|
|
|
$query1->where('cart_rules.starts_from', '<=', Carbon::now()->format('Y-m-d'))
|
|
|
|
|
->orWhereNull('cart_rules.starts_from');
|
|
|
|
|
})
|
|
|
|
|
->where(function ($query2) {
|
|
|
|
|
$query2->where('cart_rules.ends_till', '>=', Carbon::now()->format('Y-m-d'))
|
|
|
|
|
->orWhereNull('cart_rules.ends_till');
|
|
|
|
|
})
|
|
|
|
|
->orderBy('sort_order', 'asc');
|
2019-12-21 05:25:32 +00:00
|
|
|
})->findWhere(['status' => 1]);
|
|
|
|
|
|
|
|
|
|
return $cartRules;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if cart rule can be applied
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @param \Webkul\CartRule\Contracts\CartRule $rule
|
|
|
|
|
* @return bool
|
2019-12-21 05:25:32 +00:00
|
|
|
*/
|
2020-02-26 17:31:49 +00:00
|
|
|
public function canProcessRule($rule): bool
|
2019-12-21 05:25:32 +00:00
|
|
|
{
|
|
|
|
|
$cart = Cart::getCart();
|
|
|
|
|
|
|
|
|
|
if ($rule->coupon_type) {
|
|
|
|
|
if (strlen($cart->coupon_code)) {
|
|
|
|
|
$coupon = $this->cartRuleCouponRepository->findOneWhere([
|
2020-02-26 15:41:40 +00:00
|
|
|
'cart_rule_id' => $rule->id,
|
|
|
|
|
'code' => $cart->coupon_code,
|
|
|
|
|
]);
|
2019-12-21 05:25:32 +00:00
|
|
|
|
|
|
|
|
if ($coupon) {
|
2020-02-19 11:49:52 +00:00
|
|
|
if ($coupon->usage_limit && $coupon->times_used >= $coupon->usage_limit) {
|
2019-12-21 05:25:32 +00:00
|
|
|
return false;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2020-02-25 08:33:47 +00:00
|
|
|
|
2019-12-21 05:25:32 +00:00
|
|
|
if ($cart->customer_id && $coupon->usage_per_customer) {
|
|
|
|
|
$couponUsage = $this->cartRuleCouponUsageRepository->findOneWhere([
|
2020-02-26 15:41:40 +00:00
|
|
|
'cart_rule_coupon_id' => $coupon->id,
|
|
|
|
|
'customer_id' => $cart->customer_id,
|
|
|
|
|
]);
|
2019-12-21 05:25:32 +00:00
|
|
|
|
2020-02-19 11:49:52 +00:00
|
|
|
if ($couponUsage && $couponUsage->times_used >= $coupon->usage_per_customer) {
|
2019-12-21 05:25:32 +00:00
|
|
|
return false;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($rule->usage_per_customer) {
|
|
|
|
|
$ruleCustomer = $this->cartRuleCustomerRepository->findOneWhere([
|
2020-02-26 15:41:40 +00:00
|
|
|
'cart_rule_id' => $rule->id,
|
|
|
|
|
'customer_id' => $cart->customer_id,
|
|
|
|
|
]);
|
2019-12-21 05:25:32 +00:00
|
|
|
|
2020-02-19 11:49:52 +00:00
|
|
|
if ($ruleCustomer && $ruleCustomer->times_used >= $rule->usage_per_customer) {
|
2019-12-21 05:25:32 +00:00
|
|
|
return false;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cart item discount calculation process
|
|
|
|
|
*
|
2020-02-25 08:33:47 +00:00
|
|
|
* @param \Webkul\Checkout\Models\CartItem $item
|
|
|
|
|
* @return array
|
2019-12-21 05:25:32 +00:00
|
|
|
*/
|
2020-02-25 08:33:47 +00:00
|
|
|
public function process(CartItem $item): array
|
2019-12-21 05:25:32 +00:00
|
|
|
{
|
|
|
|
|
$item->discount_percent = 0;
|
|
|
|
|
$item->discount_amount = 0;
|
|
|
|
|
$item->base_discount_amount = 0;
|
|
|
|
|
|
|
|
|
|
$appliedRuleIds = [];
|
|
|
|
|
|
|
|
|
|
foreach ($this->getCartRules() as $rule) {
|
2020-02-19 11:49:52 +00:00
|
|
|
if (! $this->canProcessRule($rule)) {
|
2019-12-21 05:25:32 +00:00
|
|
|
continue;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
|
2020-02-19 11:49:52 +00:00
|
|
|
if (! $this->validator->validate($rule, $item)) {
|
2019-12-21 05:25:32 +00:00
|
|
|
continue;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
|
|
|
|
|
$quantity = $rule->discount_quantity ? min($item->quantity, $rule->discount_quantity) : $item->quantity;
|
|
|
|
|
|
2019-12-25 14:27:05 +00:00
|
|
|
$discountAmount = $baseDiscountAmount = 0;
|
|
|
|
|
|
2019-12-21 05:25:32 +00:00
|
|
|
switch ($rule->action_type) {
|
|
|
|
|
case 'by_percent':
|
|
|
|
|
$rulePercent = min(100, $rule->discount_amount);
|
|
|
|
|
|
|
|
|
|
$discountAmount = ($quantity * $item->price - $item->discount_amount) * ($rulePercent / 100);
|
|
|
|
|
|
|
|
|
|
$baseDiscountAmount = ($quantity * $item->base_price - $item->base_discount_amount) * ($rulePercent / 100);
|
|
|
|
|
|
|
|
|
|
if (! $rule->discount_quantity || $rule->discount_quantity > $quantity) {
|
|
|
|
|
$discountPercent = min(100, $item->discount_percent + $rulePercent);
|
|
|
|
|
|
|
|
|
|
$item->discount_percent = $discountPercent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'by_fixed':
|
|
|
|
|
$discountAmount = $quantity * core()->convertPrice($rule->discount_amount);
|
|
|
|
|
|
|
|
|
|
$baseDiscountAmount = $quantity * $rule->discount_amount;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'cart_fixed':
|
|
|
|
|
if ($this->itemTotals[$rule->id]['total_items'] <= 1) {
|
|
|
|
|
$discountAmount = core()->convertPrice($rule->discount_amount);
|
|
|
|
|
|
|
|
|
|
$baseDiscountAmount = min($item->base_price * $quantity, $rule->discount_amount);
|
|
|
|
|
} else {
|
|
|
|
|
$discountRate = $item->base_price * $quantity / $this->itemTotals[$rule->id]['base_total_price'];
|
|
|
|
|
|
|
|
|
|
$maxDiscount = $rule->discount_amount * $discountRate;
|
|
|
|
|
|
|
|
|
|
$discountAmount = core()->convertPrice($maxDiscount);
|
|
|
|
|
|
|
|
|
|
$baseDiscountAmount = min($item->base_price * $quantity, $maxDiscount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$discountAmount = min($item->price * $quantity, $discountAmount);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'buy_x_get_y':
|
2020-02-19 11:49:52 +00:00
|
|
|
if (! $rule->discount_step || $rule->discount_amount > $rule->discount_step) {
|
2019-12-21 05:25:32 +00:00
|
|
|
break;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
|
|
|
|
|
$buyAndDiscountQty = $rule->discount_step + $rule->discount_amount;
|
|
|
|
|
|
|
|
|
|
$qtyPeriod = floor($quantity / $buyAndDiscountQty);
|
|
|
|
|
|
|
|
|
|
$freeQty = $quantity - $qtyPeriod * $buyAndDiscountQty;
|
|
|
|
|
|
|
|
|
|
$discountQty = $qtyPeriod * $rule->discount_amount;
|
|
|
|
|
|
2020-02-19 11:49:52 +00:00
|
|
|
if ($freeQty > $rule->discount_step) {
|
2019-12-21 05:25:32 +00:00
|
|
|
$discountQty += $freeQty - $rule->discount_step;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
|
|
|
|
|
$discountAmount = $discountQty * $item->price;
|
|
|
|
|
|
|
|
|
|
$baseDiscountAmount = $discountQty * $item->base_price;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-26 15:41:40 +00:00
|
|
|
$item->discount_amount = min($item->discount_amount + $discountAmount, $item->price * $quantity + $item->tax_amount);
|
|
|
|
|
$item->base_discount_amount = min($item->base_discount_amount + $baseDiscountAmount, $item->base_price * $quantity + $item->base_tax_amount);
|
2019-12-21 05:25:32 +00:00
|
|
|
|
|
|
|
|
$appliedRuleIds[$rule->id] = $rule->id;
|
|
|
|
|
|
2020-02-19 11:49:52 +00:00
|
|
|
if ($rule->end_other_rules) {
|
2019-12-21 05:25:32 +00:00
|
|
|
break;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
}
|
|
|
|
|
|
2020-02-26 17:31:49 +00:00
|
|
|
$item->applied_cart_rule_ids = implode(',', $appliedRuleIds);
|
2019-12-21 05:25:32 +00:00
|
|
|
|
|
|
|
|
$item->save();
|
|
|
|
|
|
2020-02-25 08:33:47 +00:00
|
|
|
return $appliedRuleIds;
|
2019-12-21 05:25:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cart shipping discount calculation process
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @param \Webkul\Checkout\Contracts\Cart $cart
|
2019-12-21 05:25:32 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function processShippingDiscount($cart)
|
|
|
|
|
{
|
2020-02-19 11:49:52 +00:00
|
|
|
if (! $selectedShipping = $cart->selected_shipping_rate) {
|
2019-12-21 05:25:32 +00:00
|
|
|
return;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
|
|
|
|
|
$selectedShipping->discount_amount = 0;
|
|
|
|
|
$selectedShipping->base_discount_amount = 0;
|
|
|
|
|
|
|
|
|
|
$appliedRuleIds = [];
|
|
|
|
|
|
|
|
|
|
foreach ($this->getCartRules() as $rule) {
|
2020-02-19 11:49:52 +00:00
|
|
|
if (! $this->canProcessRule($rule)) {
|
2019-12-21 05:25:32 +00:00
|
|
|
continue;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
|
2020-02-19 11:49:52 +00:00
|
|
|
if (! $this->validator->validate($rule, $cart)) {
|
2019-12-21 05:25:32 +00:00
|
|
|
continue;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
|
2020-02-19 11:49:52 +00:00
|
|
|
if (! $rule || ! $rule->apply_to_shipping) {
|
2019-12-21 05:25:32 +00:00
|
|
|
continue;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
|
|
|
|
|
$discountAmount = $baseDiscountAmount = 0;
|
|
|
|
|
|
|
|
|
|
switch ($rule->action_type) {
|
|
|
|
|
case 'by_percent':
|
|
|
|
|
$rulePercent = min(100, $rule->discount_amount);
|
|
|
|
|
|
|
|
|
|
$discountAmount = ($selectedShipping->price - $selectedShipping->discount_amount) * $rulePercent / 100;
|
|
|
|
|
|
|
|
|
|
$baseDiscountAmount = ($selectedShipping->base_price - $selectedShipping->base_discount_amount) * $rulePercent / 100;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'by_fixed':
|
|
|
|
|
$discountAmount = core()->convertPrice($rule->discount_amount);
|
|
|
|
|
|
|
|
|
|
$baseDiscountAmount = $rule->discount_amount;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$selectedShipping->discount_amount = min($selectedShipping->discount_amount + $discountAmount, $selectedShipping->price);
|
|
|
|
|
|
|
|
|
|
$selectedShipping->base_discount_amount = min(
|
2020-02-26 15:41:40 +00:00
|
|
|
$selectedShipping->base_discount_amount + $baseDiscountAmount,
|
|
|
|
|
$selectedShipping->base_price
|
|
|
|
|
);
|
2019-12-21 05:25:32 +00:00
|
|
|
|
|
|
|
|
$selectedShipping->save();
|
|
|
|
|
|
|
|
|
|
$appliedRuleIds[$rule->id] = $rule->id;
|
|
|
|
|
|
2020-02-19 11:49:52 +00:00
|
|
|
if ($rule->end_other_rules) {
|
2019-12-21 05:25:32 +00:00
|
|
|
break;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
}
|
|
|
|
|
|
2019-12-21 10:30:22 +00:00
|
|
|
$selectedShipping->save();
|
|
|
|
|
|
2019-12-21 05:25:32 +00:00
|
|
|
$cartAppliedCartRuleIds = array_merge(explode(',', $cart->applied_cart_rule_ids), $appliedRuleIds);
|
|
|
|
|
|
|
|
|
|
$cartAppliedCartRuleIds = array_filter($cartAppliedCartRuleIds);
|
|
|
|
|
|
|
|
|
|
$cartAppliedCartRuleIds = array_unique($cartAppliedCartRuleIds);
|
|
|
|
|
|
2020-02-26 17:31:49 +00:00
|
|
|
$cart->applied_cart_rule_ids = implode(',', $cartAppliedCartRuleIds);
|
2019-12-21 05:25:32 +00:00
|
|
|
|
|
|
|
|
$cart->save();
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Cart free shipping discount calculation process
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @param \Webkul\Checkout\Contracts\Cart $cart
|
2019-12-21 05:25:32 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function processFreeShippingDiscount($cart)
|
|
|
|
|
{
|
2020-02-19 11:49:52 +00:00
|
|
|
if (! $selectedShipping = $cart->selected_shipping_rate) {
|
2019-12-21 05:25:32 +00:00
|
|
|
return;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
|
|
|
|
|
$selectedShipping->discount_amount = 0;
|
|
|
|
|
|
|
|
|
|
$selectedShipping->base_discount_amount = 0;
|
|
|
|
|
|
|
|
|
|
$appliedRuleIds = [];
|
|
|
|
|
|
|
|
|
|
foreach ($this->getCartRules() as $rule) {
|
2020-02-19 11:49:52 +00:00
|
|
|
if (! $this->canProcessRule($rule)) {
|
2019-12-21 05:25:32 +00:00
|
|
|
continue;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
|
2020-02-19 11:49:52 +00:00
|
|
|
if (! $this->validator->validate($rule, $cart)) {
|
2019-12-21 05:25:32 +00:00
|
|
|
continue;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
|
2020-02-19 11:49:52 +00:00
|
|
|
if (! $rule || ! $rule->free_shipping) {
|
2019-12-21 05:25:32 +00:00
|
|
|
continue;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
|
|
|
|
|
$selectedShipping->price = 0;
|
|
|
|
|
|
|
|
|
|
$selectedShipping->base_price = 0;
|
|
|
|
|
|
|
|
|
|
$selectedShipping->save();
|
|
|
|
|
|
|
|
|
|
$appliedRuleIds[$rule->id] = $rule->id;
|
|
|
|
|
|
2020-02-19 11:49:52 +00:00
|
|
|
if ($rule->end_other_rules) {
|
2019-12-21 05:25:32 +00:00
|
|
|
break;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$cartAppliedCartRuleIds = array_merge(explode(',', $cart->applied_cart_rule_ids), $appliedRuleIds);
|
|
|
|
|
|
|
|
|
|
$cartAppliedCartRuleIds = array_filter($cartAppliedCartRuleIds);
|
|
|
|
|
|
|
|
|
|
$cartAppliedCartRuleIds = array_unique($cartAppliedCartRuleIds);
|
|
|
|
|
|
|
|
|
|
$cart->applied_cart_rule_ids = join(',', $cartAppliedCartRuleIds);
|
|
|
|
|
|
|
|
|
|
$cart->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculate cart item totals for each rule
|
|
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @param \Illuminate\Support\Collecton $items
|
|
|
|
|
* @return \Webkul\Rule\Helpers\Validator
|
2019-12-21 05:25:32 +00:00
|
|
|
*/
|
|
|
|
|
public function calculateCartItemTotals($items)
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->getCartRules() as $rule) {
|
|
|
|
|
if ($rule->action_type == 'cart_fixed') {
|
|
|
|
|
$totalPrice = $totalBasePrice = $validCount = 0;
|
|
|
|
|
|
|
|
|
|
foreach ($items as $item) {
|
2020-02-19 11:49:52 +00:00
|
|
|
if (! $this->canProcessRule($rule, $item)) {
|
2019-12-21 05:25:32 +00:00
|
|
|
continue;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
|
|
|
|
|
$quantity = $rule->discount_quantity ? min($item->quantity, $rule->discount_quantity) : $item->quantity;
|
|
|
|
|
|
|
|
|
|
$totalBasePrice += $item->base_price * $quantity;
|
|
|
|
|
|
|
|
|
|
$validCount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->itemTotals[$rule->id] = [
|
|
|
|
|
'base_total_price' => $totalBasePrice,
|
2020-02-19 11:49:52 +00:00
|
|
|
'total_items' => $validCount,
|
2019-12-21 05:25:32 +00:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if coupon code is valid or not, if not remove from cart
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function validateCouponCode()
|
|
|
|
|
{
|
|
|
|
|
$cart = Cart::getCart();
|
|
|
|
|
|
2020-02-19 11:49:52 +00:00
|
|
|
if (! $cart->coupon_code) {
|
2019-12-21 05:25:32 +00:00
|
|
|
return;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
|
|
|
|
|
$coupon = $this->cartRuleCouponRepository->findOneByField('code', $cart->coupon_code);
|
|
|
|
|
|
2020-02-19 11:49:52 +00:00
|
|
|
if (! $coupon || ! in_array($coupon->cart_rule_id, explode(',', $cart->applied_cart_rule_ids))) {
|
2019-12-21 05:25:32 +00:00
|
|
|
Cart::removeCouponCode();
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-21 05:25:32 +00:00
|
|
|
}
|
2019-12-25 14:27:05 +00:00
|
|
|
|
|
|
|
|
/**
|
2020-02-26 17:31:49 +00:00
|
|
|
* Divide discount amount to children
|
2019-12-25 14:27:05 +00:00
|
|
|
*
|
2020-03-05 05:34:57 +00:00
|
|
|
* @param \Webkul\Checkout\Contracts\CartItem $item
|
2019-12-25 14:27:05 +00:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2020-02-26 17:31:49 +00:00
|
|
|
protected function divideDiscount($item)
|
2019-12-25 14:27:05 +00:00
|
|
|
{
|
|
|
|
|
foreach ($item->children as $child) {
|
|
|
|
|
$ratio = $item->base_total != 0 ? $child->base_total / $item->base_total : 0;
|
|
|
|
|
|
|
|
|
|
foreach (['discount_amount', 'base_discount_amount'] as $column) {
|
2020-02-19 11:49:52 +00:00
|
|
|
if (! $item->{$column}) {
|
2019-12-25 14:27:05 +00:00
|
|
|
continue;
|
2020-02-19 11:49:52 +00:00
|
|
|
}
|
2019-12-25 14:27:05 +00:00
|
|
|
|
|
|
|
|
$child->{$column} = round(($item->{$column} * $ratio), 4);
|
|
|
|
|
|
|
|
|
|
$child->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-02-25 08:33:47 +00:00
|
|
|
}
|