New cart rule actions

This commit is contained in:
Prashant Singh 2019-07-24 19:32:37 +05:30
parent 45ce843772
commit 1a3038ae72
6 changed files with 117 additions and 57 deletions

View File

@ -40,13 +40,17 @@ class FixedAmount extends Action
$totalDiscount = $totalDiscount + $amountDiscounted;
$itemReport['item_id'] = $item->id;
$itemReport['product_id'] = $item->product_id;
$itemReport['discount'] = $amountDiscounted;
$itemReport['formatted_discount'] = core()->currency($amountDiscounted);
$report->push($itemReport);
unset($itemReport);
}
$report->discount = $totalDiscount;
$report->formatted_discount = core()->currency($totalDiscount);
return $report;
}

View File

@ -38,13 +38,17 @@ class PercentOfProduct extends Action
$totalDiscount = $totalDiscount + $amountDiscounted;
$itemReport['item_id'] = $item->id;
$itemReport['product_id'] = $item->product_id;
$itemReport['discount'] = $amountDiscounted;
$itemReport['formatted_discount'] = core()->currency($amountDiscounted);
$report->push($itemReport);
unset($itemReport);
}
$report->discount = $totalDiscount;
$report->formatted_discount = core()->currency($totalDiscount);
return $report;
}

View File

@ -13,38 +13,21 @@ class WholeCartToFixed extends Action
$totalDiscount = 0;
foreach ($items as $item) {
$amountDiscounted = 0;
$itemReport = array();
$disc_threshold = $rule->disc_threshold;
$disc_amount = $rule->disc_amount;
$disc_quantity = $rule->disc_quantity;
$realQty = $item['quantity'];
if ($cart->items_qty >= $disc_threshold) {
$amountDiscounted = $item['base_price'] * ($disc_amount / 100);
if ($realQty > $disc_quantity) {
$amountDiscounted = $amountDiscounted * $disc_quantity;
} else {
$amountDiscounted = $amountDiscounted * $realQty;
}
if ($amountDiscounted > $item['base_price'] && $realQty == 1) {
$amountDiscounted = $item['base_price'];
}
}
$totalDiscount = $totalDiscount + $amountDiscounted;
$itemReport['item_id'] = $item->id;
$itemReport['discount'] = $amountDiscounted;
$itemReport['formatted_discount'] = core()->currency($amountDiscounted);
$itemReport['product_id'] = $item->product_id;
$itemReport['discount'] = 0;
$itemReport['formatted_discount'] = core()->currency(0);
$report->push($itemReport);
unset($itemReport);
}
$report->discount = $cart->base_grand_total - $rule->discount_amount;
$report->formatted_discount = core()->currency($report->discount);
return $report;
}

View File

@ -6,22 +6,79 @@ use Webkul\Discount\Actions\Action;
class WholeCartToPercent extends Action
{
/**
* To calculate impact of cart rule's action of current items of cart instance
*
* @param CartRule $rule
* @param CartItem $items
* @param Cart $cart
*
* @return boolean
*/
public function calculate($rule, $items, $cart)
{
$report = collect();
$totalDiscount = 0;
$disc_percent = $rule->discount_amount;
$itemReport = collect();
if ($disc_percent <= 100) {
$totalDiscount = $cart->base_grand_total - $rule->discount_amount;
$report->discount = $totalDiscount;
if ($rule->discount_amount >= 100) {
$report->discount = $cart->base_grand_total;
} else {
$report->discount = 0;
$report->discount = ($rule->disc_amount / 100) * $cart->base_grand_total;
}
$report->formatted_discount = core()->currency($report->discount);
if ($rule->uses_attribute_conditions) {
$productIDs = $rule->product_ids;
$productIDs = explode(',', $productIDs);
$matchCount = 0;
foreach ($productIDs as $productID) {
foreach ($items as $item) {
if ($item->product_id == $productID) {
$matchCount++;
}
}
}
if ($matchCount > 0) {
$discountPerItem = $report->discount / $matchCount;
}
foreach ($productIDs as $productID) {
foreach ($items as $item) {
if ($item->product_id == $productID) {
$itemReport = array();
$itemReport['item_id'] = $item->id;
$itemReport['product_id'] = $item->product_id;
$itemReport['discount'] = $discountPerItem;
$itemReport['formatted_discount'] = core()->currency(0);
$report->push($itemReport);
unset($itemReport);
}
}
}
} else {
$discountPerItem = $report->discount / $cart->items_qty;
foreach ($items as $item) {
$itemReport = array();
$itemReport['item_id'] = $item->id;
$itemReport['product_id'] = $item->product_id;
$itemReport['discount'] = $discountPerItem;
$itemReport['formatted_discount'] = core()->currency(0);
$report->push($itemReport);
unset($itemReport);
}
}
return $report;

View File

@ -366,39 +366,50 @@ abstract class Discount
$actionInstance = new $this->rules['cart'][$rule->action_type];
$impact = $actionInstance->calculate($rule, $cartItems, $cart);
if ($impact);
$totalItemDiscount = 0;
foreach ($cart->items as $item) {
foreach ($impact as $itemDiscount) {
if ($item->id == $itemDiscount['item_id']) {
if ($rule->action_type == 'percent_of_product') {
$item->update([
'discount_percent' => $rule->discount_amount,
'discount_amount' => core()->convertPrice($itemDiscount['discount'], $cart->cart_currency_code),
'base_discount_amount' => $itemDiscount['discount']
]);
} else {
$item->update([
'discount_amount' => core()->convertPrice($itemDiscount['discount'], $cart->cart_currency_code),
'base_discount_amount' => $itemDiscount['discount']
]);
}
$totalItemDiscount = $totalItemDiscount + $itemDiscount['discount'];
// save coupon if rule use it
if ($rule->use_coupon) {
$coupon = $rule->coupons->code;
if ($itemDiscount['discount'] > 0) {
$item->update([
'discount_amount' => core()->convertPrice($itemDiscount['discount'], $cart->cart_currency_code),
'base_discount_amount' => $itemDiscount['discount']
]);
if ($item->id == $itemDiscount['item_id'] && $itemDiscount['discount'] > 0) {
$item->update([
'coupon_code' => $coupon
]);
$cart->update([
'coupon_code' => $coupon
'discount_percent' => $rule->discount_amount
]);
}
}
// save coupon if rule use it
if ($rule->use_coupon) {
$coupon = $rule->coupons->code;
$item->update([
'coupon_code' => $coupon
]);
$cart->update([
'coupon_code' => $coupon
]);
}
}
}
if ($totalItemDiscount < $impact->discount) {
$cart->update([
'base_discount_amount' => $impact->discount,
'discount_amount' => core()->convertPrice($impact->discount, $cart->cart_currency_code),
'base_sub_total' => $cart->base_sub_total - $impact->discount,
'sub_total' => $cart->sub_total - core()->convertPrice($impact->discount, $cart->cart_currency_code)
]);
}
Cart::collectTotals();
return true;

View File

@ -76,7 +76,7 @@ class NonCouponAbleRule extends Discount
$actionInstance = new $this->rules['cart'][$rule->action_type];
$impact = $actionInstance->calculate($rule, $items, $cart);
dd($impact);
if ($impact->discount > 0) {
array_push($applicableRules, [
'rule' => $rule,
@ -209,6 +209,7 @@ class NonCouponAbleRule extends Discount
return $prioritySorted;
}
} else if (count($applicableRules) == 1) {
$this->save(array_first($applicableRules)['rule']);
return array_first($applicableRules)['impact'];