sarga/packages/Webkul/Discount/src/Actions/Cart/WholeCartToPercent.php

100 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace Webkul\Discount\Actions\Cart;
use Webkul\Discount\Actions\Action;
class WholeCartToPercent extends Action
{
2019-07-24 14:02:37 +00:00
/**
* 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
*/
2019-08-03 14:24:02 +00:00
public function calculate($rule)
{
2019-08-03 14:24:02 +00:00
$cart = \Cart::getCart();
$items = $cart->items;
2019-08-03 14:24:02 +00:00
$impact = collect();
2019-07-23 12:53:49 +00:00
$totalDiscount = 0;
2019-07-24 14:02:37 +00:00
if ($rule->discount_amount >= 100) {
2019-08-03 14:24:02 +00:00
$impact->discount = $cart->base_grand_total;
2019-07-24 14:02:37 +00:00
} else {
2019-08-03 14:24:02 +00:00
$impact->discount = ($rule->disc_amount / 100) * $cart->base_grand_total;
2019-07-24 14:02:37 +00:00
}
2019-08-03 14:24:02 +00:00
$impact->formatted_discount = core()->currency($impact->discount);
2019-07-24 14:02:37 +00:00
if ($rule->uses_attribute_conditions) {
$productIDs = $rule->product_ids;
$productIDs = explode(',', $productIDs);
$matchCount = 0;
2019-07-24 14:02:37 +00:00
foreach ($productIDs as $productID) {
foreach ($items as $item) {
if ($item->product_id == $productID) {
$matchCount++;
}
}
}
2019-07-24 14:02:37 +00:00
if ($matchCount > 0) {
2019-08-03 14:24:02 +00:00
$discountPerItem = $impact->discount / $matchCount;
2019-07-24 14:02:37 +00:00
}
2019-07-24 14:02:37 +00:00
foreach ($productIDs as $productID) {
foreach ($items as $item) {
if ($item->product_id == $productID) {
2019-08-03 14:24:02 +00:00
$report = array();
2019-07-24 14:02:37 +00:00
2019-08-03 14:24:02 +00:00
$report['item_id'] = $item->id;
$report['product_id'] = $item->product_id;
$report['discount'] = $discountPerItem;
$report['formatted_discount'] = core()->currency(0);
2019-07-24 14:02:37 +00:00
2019-08-03 14:24:02 +00:00
$impact->push($report);
2019-07-24 14:02:37 +00:00
2019-08-03 14:24:02 +00:00
unset($report);
2019-07-24 14:02:37 +00:00
}
}
}
2019-07-23 12:53:49 +00:00
} else {
2019-07-24 14:02:37 +00:00
$discountPerItem = $report->discount / $cart->items_qty;
foreach ($items as $item) {
2019-08-03 14:24:02 +00:00
$report = array();
2019-07-24 14:02:37 +00:00
2019-08-03 14:24:02 +00:00
$report['item_id'] = $item->id;
$report['product_id'] = $item->product_id;
$report['discount'] = $discountPerItem;
$report['formatted_discount'] = core()->currency(0);
2019-07-24 14:02:37 +00:00
2019-08-03 14:24:02 +00:00
$impact->push($report);
2019-07-24 14:02:37 +00:00
2019-08-03 14:24:02 +00:00
unset($report);
2019-07-24 14:02:37 +00:00
}
}
2019-08-03 14:24:02 +00:00
return $impact;
}
/**
* Calculates the impact on the shipping amount if the rule is apply_to_shipping enabled
*/
2019-08-03 14:24:02 +00:00
public function calculateOnShipping()
{
$percentOfDiscount = ($cart->base_discount_amount * 100) / $cart->base_grand_total;
$discountOnShipping = ($percentOfDiscount / 100) * $cart->selected_shipping_rate->base_price;
return $discountOnShipping;
}
}