Refactored actions of cart rules
This commit is contained in:
parent
573aea9c75
commit
4a3bfd6e2c
|
|
@ -5,4 +5,34 @@ namespace Webkul\Discount\Actions;
|
|||
abstract class Action
|
||||
{
|
||||
abstract public function calculate($rule);
|
||||
|
||||
public function getProductIDs($rule)
|
||||
{
|
||||
$cart = \Cart::getCart();
|
||||
$items = $cart->items;
|
||||
|
||||
$matchedItems = collect();
|
||||
|
||||
$productIDs = $rule->product_ids;
|
||||
|
||||
$productIDs = explode(',', $productIDs);
|
||||
|
||||
$matchCriteria = $rule->uses_attribute_conditions ? $rule->product_ids : '*';
|
||||
|
||||
if ($matchCriteria == '*') {
|
||||
return $items;
|
||||
} else {
|
||||
$matchingIDs = explode(',', $matchCriteria);
|
||||
|
||||
foreach ($items as $item) {
|
||||
foreach ($matchingIDs as $matchingID) {
|
||||
if ($matchingID == $item->child ? $item->child->product_id : $item->product_id) {
|
||||
$matchedItems->push($item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $matchedItems;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -8,98 +8,48 @@ class FixedAmount extends Action
|
|||
{
|
||||
public function calculate($rule)
|
||||
{
|
||||
$cart = \Cart::getCart();
|
||||
$items = $cart->items;
|
||||
|
||||
$impact = collect();
|
||||
|
||||
$totalDiscount = 0;
|
||||
|
||||
if ($rule->uses_attribute_conditions) {
|
||||
$productIDs = $rule->product_ids;
|
||||
$eligibleItems = $this->getProductIDs($rule);
|
||||
|
||||
$productIDs = explode(',', $productIDs);
|
||||
$apply = function () use ($rule, $eligibleItems) {
|
||||
if ($rule->action_type == 'fixed_amount') {
|
||||
return true;
|
||||
} else {
|
||||
if ($rule->action_type == 'whole_cart_to_fixed' && $rule->uses_attribute_condition) {
|
||||
$matchIDs = explode(',', $rule->product_ids);
|
||||
|
||||
foreach ($productIDs as $productID) {
|
||||
foreach ($items as $item) {
|
||||
$itemPrice = $item->base_price;
|
||||
|
||||
if ($item->product->type == 'configurable') {
|
||||
$itemProductId = $item->child->product_id;
|
||||
} else {
|
||||
$itemProductId = $item->product_id;
|
||||
}
|
||||
|
||||
$itemQuantity = $item->quantity;
|
||||
|
||||
$discQuantity = $rule->disc_quantity;
|
||||
|
||||
if ($discQuantity > 1) {
|
||||
if ($itemQuantity >= $discQuantity) {
|
||||
if ($rule->disc_amount >= $itemPrice) {
|
||||
$discount = round($itemPrice * $discQuantity, 4);
|
||||
} else {
|
||||
$discount = $rule->disc_amount;
|
||||
}
|
||||
} else if ($itemQuantity < $discQuantity) {
|
||||
if ($rule->disc_amount >= $itemPrice) {
|
||||
$discount = round($itemPrice * $discQuantity, 4);
|
||||
} else {
|
||||
$discount = $rule->disc_amount;
|
||||
foreach ($matchIDs as $matchID) {
|
||||
foreach ($eligibleItems as $item) {
|
||||
if ($item->child ? $item->child->product_id : $item->product_id == $matchID) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if ($rule->disc_amount >= $itemPrice) {
|
||||
$discount = round($itemPrice * $discQuantity, 4);
|
||||
} else {
|
||||
$discount = $rule->disc_amount;
|
||||
}
|
||||
}
|
||||
|
||||
if ($itemProductId == $productID) {
|
||||
$totalDiscount = $totalDiscount + $discount;
|
||||
|
||||
$report = array();
|
||||
|
||||
$report['item_id'] = $item->id;
|
||||
$report['product_id'] = $item->child ? $item->child->product_id : $item->product_id;
|
||||
$report['discount'] = $discount;
|
||||
$report['formatted_discount'] = core()->currency($discount);
|
||||
|
||||
$impact->push($report);
|
||||
|
||||
unset($report);
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
foreach ($items as $item) {
|
||||
};
|
||||
|
||||
if ($apply()) {
|
||||
foreach ($eligibleItems as $item) {
|
||||
$itemPrice = $item->base_price;
|
||||
|
||||
$itemQuantity = $item->quantity;
|
||||
|
||||
$discQuantity = $rule->disc_quantity;
|
||||
|
||||
if ($discQuantity > 1) {
|
||||
if ($itemQuantity >= $discQuantity) {
|
||||
if ($rule->disc_amount >= $itemPrice) {
|
||||
$discount = round($itemPrice * $discQuantity, 4);
|
||||
} else {
|
||||
$discount = $rule->disc_amount;
|
||||
}
|
||||
} else if ($itemQuantity < $discQuantity) {
|
||||
if ($rule->disc_amount >= $itemPrice) {
|
||||
$discount = round($itemPrice * $discQuantity, 4);
|
||||
} else {
|
||||
$discount = $rule->disc_amount;
|
||||
}
|
||||
}
|
||||
$discQuantity = $itemQuantity <= $discQuantity ? $itemQuantity : $discQuantity;
|
||||
|
||||
if ($rule->disc_amount >= $itemPrice) {
|
||||
$discount = round($itemPrice * $discQuantity, 4);
|
||||
} else {
|
||||
if ($rule->disc_amount >= $itemPrice) {
|
||||
$discount = round($itemPrice * $discQuantity, 4);
|
||||
} else {
|
||||
$discount = $rule->disc_amount;
|
||||
}
|
||||
$discount = $rule->disc_amount;
|
||||
}
|
||||
|
||||
$totalDiscount = $totalDiscount + $discount;
|
||||
|
|
@ -123,7 +73,6 @@ class FixedAmount extends Action
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
$impact->discount = $totalDiscount;
|
||||
$impact->formatted_discount = core()->currency($impact->discount);
|
||||
|
||||
|
|
|
|||
|
|
@ -6,143 +6,77 @@ use Webkul\Discount\Actions\Action;
|
|||
|
||||
class PercentOfProduct extends Action
|
||||
{
|
||||
/**
|
||||
* To calculate impact of cart rule's action of current items of cart instance
|
||||
*
|
||||
* @param CartRule $rule
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function calculate($rule)
|
||||
{
|
||||
$cart = \Cart::getCart();
|
||||
$items = $cart->items;
|
||||
|
||||
$impact = collect();
|
||||
|
||||
$totalDiscount = 0;
|
||||
|
||||
if ($rule->discount_amount >= 100) {
|
||||
$impact->discount = $cart->base_sub_total;
|
||||
$eligibleItems = $this->getProductIDs($rule);
|
||||
|
||||
$impact->formatted_discount = core()->currency($impact->discount);
|
||||
}
|
||||
$apply = function () use($rule, $eligibleItems) {
|
||||
if ($rule->action_type == 'percent_of_product') {
|
||||
return true;
|
||||
} else {
|
||||
if ($rule->action_type == 'whole_cart_to_percent' && $rule->uses_attribute_condition) {
|
||||
$matchIDs = explode(',', $rule->product_ids);
|
||||
|
||||
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++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
foreach ($productIDs as $productID) {
|
||||
foreach ($items as $item) {
|
||||
$itemPrice = $item->base_price;
|
||||
|
||||
if ($item->product->type == 'configurable') {
|
||||
$itemProductId = $item->child->product_id;
|
||||
} else {
|
||||
$itemProductId = $item->product_id;
|
||||
}
|
||||
|
||||
$itemQuantity = $item->quantity;
|
||||
|
||||
$discQuantity = $rule->disc_quantity;
|
||||
|
||||
if ($discQuantity > 1) {
|
||||
if ($itemQuantity >= $discQuantity) {
|
||||
$discount = round(($itemPrice * $rule->disc_amount) / 100, 4) * $discQuantity;
|
||||
|
||||
if ($discount >= $itemPrice) {
|
||||
$discount = $itemPrice;
|
||||
}
|
||||
} else if ($itemQuantity < $discQuantity) {
|
||||
$discount = round(($itemPrice * $rule->disc_amount) / 100, 4) * $itemQuantity;
|
||||
|
||||
if ($discount >= $itemPrice) {
|
||||
$discount = $itemPrice;
|
||||
foreach ($matchIDs as $matchID) {
|
||||
foreach ($eligibleItems as $item) {
|
||||
if ($item->child ? $item->child->product_id : $item->product_id == $matchID) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$discount = round(($itemPrice * $rule->disc_amount) / 100, 4);
|
||||
|
||||
if ($discount >= $itemPrice) {
|
||||
$discount = $itemPrice;
|
||||
}
|
||||
}
|
||||
|
||||
if ($itemProductId == $productID) {
|
||||
$totalDiscount = $totalDiscount + $discount;
|
||||
|
||||
$report = array();
|
||||
|
||||
$report['item_id'] = $item->id;
|
||||
$report['product_id'] = $item->child ? $item->child->product_id : $item->product_id;
|
||||
$report['discount'] = $discount;
|
||||
$report['formatted_discount'] = core()->currency($discount);
|
||||
|
||||
$impact->push($report);
|
||||
|
||||
unset($report);
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
foreach ($items as $item) {
|
||||
};
|
||||
|
||||
if ($apply()) {
|
||||
foreach ($eligibleItems as $item) {
|
||||
$itemPrice = $item->base_price;
|
||||
|
||||
$itemQuantity = $item->quantity;
|
||||
|
||||
$discQuantity = $rule->disc_quantity;
|
||||
|
||||
if ($discQuantity > 1) {
|
||||
if ($itemQuantity >= $discQuantity) {
|
||||
$discount = round(($itemPrice * $rule->disc_amount) / 100, 4) * $discQuantity;
|
||||
$discQuantity = $itemQuantity <= $discQuantity ? $itemQuantity : $discQuantity;
|
||||
|
||||
if ($discount >= $itemPrice) {
|
||||
$discount = $itemPrice;
|
||||
}
|
||||
} else if ($itemQuantity < $discQuantity) {
|
||||
$discount = round(($itemPrice * $rule->disc_amount) / 100, 4) * $itemQuantity;
|
||||
|
||||
if ($discount >= $itemPrice) {
|
||||
$discount = $itemPrice;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$discount = round(($itemPrice * $rule->disc_amount) / 100, 4);
|
||||
|
||||
if ($discount >= $itemPrice) {
|
||||
$discount = $itemPrice;
|
||||
}
|
||||
}
|
||||
|
||||
$totalDiscount = $totalDiscount + $discount;
|
||||
$discount = round(($itemPrice * $rule->disc_amount) / 100, 4);
|
||||
|
||||
$report = array();
|
||||
|
||||
$report['item_id'] = $item->id;
|
||||
$report['product_id'] = $item->child ? $item->child->product_id : $item->product_id;
|
||||
|
||||
if ($discount <= $itemPrice) {
|
||||
$report['discount'] = $discount;
|
||||
} else {
|
||||
$report['discount'] = $itemPrice;
|
||||
}
|
||||
$discount = $discount <= $itemPrice ? $discount : $itemPrice;
|
||||
|
||||
$report['discount'] = $discount;
|
||||
|
||||
$report['formatted_discount'] = core()->currency($discount);
|
||||
|
||||
$impact->push($report);
|
||||
|
||||
$totalDiscount = $totalDiscount + $discount;
|
||||
|
||||
unset($report);
|
||||
}
|
||||
|
||||
$impact->discount = $totalDiscount;
|
||||
$impact->formatted_discount = core()->currency($impact->discount);
|
||||
|
||||
return $impact;
|
||||
}
|
||||
|
||||
|
||||
$impact->discount = $totalDiscount;
|
||||
$impact->formatted_discount = core()->currency($impact->discount);
|
||||
|
||||
return $impact;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,86 +2,23 @@
|
|||
|
||||
namespace Webkul\Discount\Actions\Cart;
|
||||
|
||||
use Webkul\Discount\Actions\Action;
|
||||
use Webkul\Discount\Actions\Cart\FixedAmount;
|
||||
|
||||
class WholeCartToFixed extends Action
|
||||
class WholeCartToFixed
|
||||
{
|
||||
/**
|
||||
* To calculate impact of cart rule's action of current items of cart instance
|
||||
*
|
||||
* @param CartRule $rule
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function calculate($rule)
|
||||
{
|
||||
$cart = \Cart::getCart();
|
||||
$items = $cart->items;
|
||||
$actualInstance = new FixedAmount();
|
||||
|
||||
$impact = collect();
|
||||
$result = $actualInstance->calculate($rule);
|
||||
|
||||
$impact->discount = $rule->disc_amount;
|
||||
$impact->formatted_discount = core()->currency($impact->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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($productIDs as $productID) {
|
||||
foreach ($items as $item) {
|
||||
$itemPrice = $item->base_price;
|
||||
|
||||
if ($item->product->type == 'configurable') {
|
||||
$itemProductId = $item->child->product_id;
|
||||
} else {
|
||||
$itemProductId = $item->product_id;
|
||||
}
|
||||
|
||||
$discount = round(($itemPrice * $impact->discount) / 100, 4);
|
||||
|
||||
if ($itemProductId == $productID) {
|
||||
$report = array();
|
||||
|
||||
$report['item_id'] = $item->id;
|
||||
$report['product_id'] = $item->product_id;
|
||||
$report['discount'] = $discount;
|
||||
$report['formatted_discount'] = core()->currency($discount);
|
||||
|
||||
$impact->push($report);
|
||||
|
||||
unset($report);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
foreach ($items as $item) {
|
||||
$itemPrice = $item->base_price;
|
||||
|
||||
if ($item->product->type == 'configurable') {
|
||||
$itemProductId = $item->child->product_id;
|
||||
} else {
|
||||
$itemProductId = $item->product_id;
|
||||
}
|
||||
|
||||
$discount = round(($itemPrice * $impact->discount) / 100, 4);
|
||||
|
||||
$report = array();
|
||||
|
||||
$report['item_id'] = $item->id;
|
||||
$report['product_id'] = $item->product_id;
|
||||
$report['discount'] = $discount;
|
||||
$report['formatted_discount'] = core()->currency($discount);
|
||||
|
||||
$impact->push($report);
|
||||
|
||||
unset($report);
|
||||
}
|
||||
}
|
||||
|
||||
return $impact;
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,113 +2,23 @@
|
|||
|
||||
namespace Webkul\Discount\Actions\Cart;
|
||||
|
||||
use Webkul\Discount\Actions\Action;
|
||||
use Cart;
|
||||
use Webkul\Discount\Actions\Cart\PercentOfProduct;
|
||||
|
||||
class WholeCartToPercent extends Action
|
||||
class WholeCartToPercent
|
||||
{
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
$cart = \Cart::getCart();
|
||||
$items = $cart->items;
|
||||
$actualInstance = new PercentOfProduct();
|
||||
|
||||
$impact = collect();
|
||||
$result = $actualInstance->calculate($rule);
|
||||
|
||||
$totalDiscount = 0;
|
||||
|
||||
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) {
|
||||
foreach ($productIDs as $productID) {
|
||||
foreach ($items as $item) {
|
||||
$itemPrice = $item->base_price;
|
||||
|
||||
$discount = round(($itemPrice * $rule->disc_amount) / 100, 4);
|
||||
|
||||
if ($discount >= $itemPrice) {
|
||||
$discount = $itemPrice;
|
||||
}
|
||||
|
||||
$totalDiscount = $totalDiscount + $discount;
|
||||
|
||||
if ($item->product_id == $productID) {
|
||||
$report = array();
|
||||
|
||||
$report['item_id'] = $item->id;
|
||||
$report['product_id'] = $item->child ? $item->child->product_id : $item->product_id;
|
||||
|
||||
if ($discount <= $itemPrice) {
|
||||
$report['discount'] = $discount;
|
||||
} else {
|
||||
$report['discount'] = $itemPrice;
|
||||
}
|
||||
|
||||
$report['formatted_discount'] = core()->currency(round($discount, 4));
|
||||
|
||||
$impact->push($report);
|
||||
|
||||
unset($report);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
foreach ($items as $item) {
|
||||
$itemPrice = $item->base_price;
|
||||
|
||||
$discount = round(($itemPrice * $rule->disc_amount) / 100, 4);
|
||||
|
||||
if ($discount > $itemPrice) {
|
||||
$discount = $itemPrice;
|
||||
}
|
||||
|
||||
$totalDiscount = $totalDiscount + $discount;
|
||||
|
||||
$report = array();
|
||||
|
||||
$report['item_id'] = $item->id;
|
||||
$report['product_id'] = $item->child ? $item->child->product_id : $item->product_id;
|
||||
|
||||
if ($discount <= $itemPrice) {
|
||||
$report['discount'] = $discount;
|
||||
} else {
|
||||
$report['discount'] = $itemPrice;
|
||||
}
|
||||
|
||||
$report['formatted_discount'] = core()->currency(round($discount, 4));
|
||||
|
||||
$impact->push($report);
|
||||
|
||||
unset($report);
|
||||
}
|
||||
}
|
||||
|
||||
$impact->discount = $totalDiscount;
|
||||
|
||||
$impact->fomatted_discount = core()->currency($impact->discount);
|
||||
|
||||
return $impact;
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
|
@ -5,7 +5,7 @@ return [
|
|||
'cart' => [
|
||||
'fixed_amount' => 'Webkul\Discount\Actions\Cart\FixedAmount',
|
||||
'percent_of_product' => 'Webkul\Discount\Actions\Cart\PercentOfProduct',
|
||||
// 'whole_cart_to_fixed' => 'aWebkul\Discount\Actions\Cart\WholeCartToFixed',
|
||||
'whole_cart_to_fixed' => 'Webkul\Discount\Actions\Cart\WholeCartToFixed',
|
||||
'whole_cart_to_percent' => 'Webkul\Discount\Actions\Cart\WholeCartToPercent'
|
||||
],
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ return [
|
|||
'actions' => [
|
||||
'fixed_amount' => 'Apply as fixed amount',
|
||||
'percent_of_product' => 'Percentage of product',
|
||||
// 'whole_cart_to_fixed' => 'Adjust whole cart to fixed amount',
|
||||
'whole_cart_to_fixed' => 'Adjust whole cart to fixed amount',
|
||||
'whole_cart_to_percent' => 'Adjust whole cart to percent'
|
||||
],
|
||||
|
||||
|
|
|
|||
|
|
@ -299,7 +299,7 @@ abstract class Discount
|
|||
|
||||
$this->updateCartItemAndCart($rule);
|
||||
|
||||
return;
|
||||
return $rule;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -264,7 +264,7 @@ class CartRuleController extends Controller
|
|||
$ruleCreated = $this->cartRule->create($data);
|
||||
|
||||
// can execute convertX here after when the rule is updated
|
||||
if (isset($attribute_conditions) && $attribute_conditions != "[]" && $attribute_conditions != "") {
|
||||
if (isset($attribute_conditions) && $data['uses_attribute_conditions']) {
|
||||
$this->convertX->convertX($ruleCreated->id, $attribute_conditions);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue