2019-06-10 07:49:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Discount\Helpers;
|
|
|
|
|
|
|
|
|
|
use Webkul\Discount\Repositories\CartRuleRepository as CartRule;
|
|
|
|
|
use Webkul\Discount\Repositories\CartRuleCartRepository as CartRuleCart;
|
|
|
|
|
use Carbon\Carbon;
|
2019-06-17 03:26:30 +00:00
|
|
|
use Cart;
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
abstract class Discount
|
2019-06-10 07:49:05 +00:00
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* To hold the cart rule repository instance
|
|
|
|
|
*/
|
|
|
|
|
protected $cartRule;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* To hold the cart rule cart repository instance
|
|
|
|
|
*/
|
|
|
|
|
protected $cartRuleCart;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* To hold if end rules are present or not.
|
|
|
|
|
*/
|
|
|
|
|
protected $endRuleActive = false;
|
|
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
/**
|
|
|
|
|
* To hold the rule classes
|
|
|
|
|
*/
|
|
|
|
|
protected $rules;
|
|
|
|
|
|
2019-06-10 07:49:05 +00:00
|
|
|
public function __construct(CartRule $cartRule, CartRuleCart $cartRuleCart)
|
|
|
|
|
{
|
|
|
|
|
$this->cartRule = $cartRule;
|
2019-06-17 03:26:30 +00:00
|
|
|
|
2019-06-10 07:49:05 +00:00
|
|
|
$this->cartRuleCart = $cartRuleCart;
|
2019-06-17 03:26:30 +00:00
|
|
|
|
|
|
|
|
$this->rules = config('discount-rules');
|
2019-06-10 07:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 14:34:32 +00:00
|
|
|
/**
|
2019-06-17 03:26:30 +00:00
|
|
|
* Abstract method apply
|
|
|
|
|
*/
|
|
|
|
|
abstract public function apply($code);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks whether coupon is getting applied on current cart instance or not
|
2019-06-14 14:34:32 +00:00
|
|
|
*
|
2019-06-17 03:26:30 +00:00
|
|
|
* @return boolean
|
2019-06-14 14:34:32 +00:00
|
|
|
*/
|
2019-06-17 03:26:30 +00:00
|
|
|
public function checkApplicability($rule)
|
2019-06-10 07:49:05 +00:00
|
|
|
{
|
2019-06-14 07:51:17 +00:00
|
|
|
$cart = \Cart::getCart();
|
|
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
$timeBased = false;
|
2019-06-14 07:51:17 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
// time based constraints
|
|
|
|
|
if ($rule->starts_from != null && $rule->ends_till == null) {
|
|
|
|
|
if (Carbon::parse($rule->starts_from) < now()) {
|
|
|
|
|
$timeBased = true;
|
|
|
|
|
}
|
|
|
|
|
} else if ($rule->starts_from == null && $rule->ends_till != null) {
|
|
|
|
|
if (Carbon::parse($rule->ends_till) > now()) {
|
|
|
|
|
$timeBased = true;
|
|
|
|
|
}
|
|
|
|
|
} else if ($rule->starts_from != null && $rule->ends_till != null) {
|
|
|
|
|
if (Carbon::parse($rule->starts_from) < now() && now() < Carbon::parse($rule->ends_till)) {
|
|
|
|
|
$timeBased = true;
|
2019-06-14 07:51:17 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2019-06-17 03:26:30 +00:00
|
|
|
$timeBased = true;
|
2019-06-14 07:51:17 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
$channelBased = false;
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
// channel based constraints
|
|
|
|
|
foreach ($rule->channels as $channel) {
|
|
|
|
|
if ($channel->channel_id == core()->getCurrentChannel()->id) {
|
|
|
|
|
$channelBased = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
$customerGroupBased = false;
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
// customer groups based constraints
|
|
|
|
|
if (auth()->guard('customer')->check()) {
|
|
|
|
|
foreach ($rule->customer_groups as $customer_group) {
|
2019-06-18 13:30:24 +00:00
|
|
|
if (auth()->guard('customer')->user()->group->exists()) {
|
|
|
|
|
if ($customer_group->customer_group_id == auth()->guard('customer')->user()->group->id) {
|
|
|
|
|
$customerGroupBased = true;
|
|
|
|
|
}
|
2019-06-10 07:49:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-06-17 03:26:30 +00:00
|
|
|
} else {
|
2019-06-25 06:57:13 +00:00
|
|
|
foreach ($rule->customer_groups as $customer_group) {
|
|
|
|
|
if ($customer_group->customer_group->code == 'guest') {
|
|
|
|
|
$customerGroupBased = true;
|
|
|
|
|
}
|
2019-06-10 07:49:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
$conditionsBased = true;
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
//check conditions
|
|
|
|
|
if ($rule->conditions != null) {
|
|
|
|
|
$conditions = json_decode(json_decode($rule->conditions));
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
$test_mode = array_last($conditions);
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
if ($test_mode->criteria == 'any_is_true') {
|
|
|
|
|
$conditionsBased = $this->testIfAnyConditionIsTrue($conditions, $cart);
|
|
|
|
|
}
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
if ($test_mode->criteria == 'all_are_true') {
|
|
|
|
|
$conditionsBased = $this->testIfAllConditionAreTrue($conditions, $cart);
|
2019-06-10 07:49:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-22 06:48:59 +00:00
|
|
|
$partialMatch = 0;
|
|
|
|
|
|
|
|
|
|
if ($rule->uses_attribute_conditions) {
|
|
|
|
|
$productIDs = explode(',', $rule->product_ids);
|
|
|
|
|
|
|
|
|
|
foreach ($productIDs as $productID) {
|
|
|
|
|
foreach ($cart->items as $item) {
|
|
|
|
|
if ($item->product_id == $productID) {
|
|
|
|
|
$partialMatch = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
if ($timeBased && $channelBased && $customerGroupBased && $conditionsBased) {
|
2019-07-22 06:48:59 +00:00
|
|
|
|
|
|
|
|
if ($rule->uses_attribute_conditions == 1 && $partialMatch) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-06-10 12:56:12 +00:00
|
|
|
} else {
|
2019-06-17 03:26:30 +00:00
|
|
|
return false;
|
2019-06-10 12:56:12 +00:00
|
|
|
}
|
2019-06-10 07:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-14 14:34:32 +00:00
|
|
|
/**
|
2019-06-17 03:26:30 +00:00
|
|
|
* Save the rule in the CartRule for current cart instance
|
2019-06-14 14:34:32 +00:00
|
|
|
*
|
2019-07-19 13:56:33 +00:00
|
|
|
* @param CartRule $rule
|
|
|
|
|
*
|
2019-06-17 03:26:30 +00:00
|
|
|
* @return boolean
|
2019-06-14 14:34:32 +00:00
|
|
|
*/
|
2019-06-17 03:26:30 +00:00
|
|
|
public function save($rule)
|
2019-06-10 07:49:05 +00:00
|
|
|
{
|
2019-06-14 07:51:17 +00:00
|
|
|
$cart = \Cart::getCart();
|
|
|
|
|
|
2019-07-19 13:56:33 +00:00
|
|
|
// create or update
|
2019-06-17 03:26:30 +00:00
|
|
|
$existingRule = $this->cartRuleCart->findWhere([
|
|
|
|
|
'cart_id' => $cart->id
|
|
|
|
|
]);
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-06-28 06:00:01 +00:00
|
|
|
if ($rule->use_coupon) {
|
|
|
|
|
$this->resetShipping($cart);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
if (count($existingRule)) {
|
|
|
|
|
if ($existingRule->first()->cart_rule_id != $rule->id) {
|
|
|
|
|
$existingRule->first()->update([
|
|
|
|
|
'cart_rule_id' => $rule->id
|
|
|
|
|
]);
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-06-20 07:33:01 +00:00
|
|
|
$this->clearDiscount();
|
|
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
$this->updateCartItemAndCart($rule);
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-06-28 07:27:19 +00:00
|
|
|
if ($rule->use_coupon) {
|
|
|
|
|
$this->checkOnShipping($cart);
|
|
|
|
|
}
|
2019-06-28 05:26:44 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
return true;
|
2019-06-27 14:25:36 +00:00
|
|
|
} else {
|
2019-06-28 07:27:19 +00:00
|
|
|
// $this->checkOnShipping($cart);
|
2019-06-10 07:49:05 +00:00
|
|
|
}
|
2019-06-17 03:26:30 +00:00
|
|
|
} else {
|
|
|
|
|
$this->cartRuleCart->create([
|
|
|
|
|
'cart_id' => $cart->id,
|
|
|
|
|
'cart_rule_id' => $rule->id
|
|
|
|
|
]);
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-06-20 07:33:01 +00:00
|
|
|
$this->clearDiscount();
|
|
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
$this->updateCartItemAndCart($rule);
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-06-28 07:27:19 +00:00
|
|
|
if ($rule->use_coupon) {
|
|
|
|
|
$this->checkOnShipping($cart);
|
|
|
|
|
}
|
2019-06-28 05:26:44 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
return true;
|
2019-06-10 07:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2019-06-14 06:40:03 +00:00
|
|
|
|
2019-06-27 14:21:17 +00:00
|
|
|
/**
|
|
|
|
|
* Checks whether rule is getting applied on shipping or not
|
|
|
|
|
*/
|
2019-06-28 05:26:44 +00:00
|
|
|
public function checkOnShipping($cart)
|
2019-06-27 14:21:17 +00:00
|
|
|
{
|
|
|
|
|
if (! isset($cart->selected_shipping_rate)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$shippingRate = config('carriers')[$cart->selected_shipping_rate->carrier]['class'];
|
|
|
|
|
|
|
|
|
|
$actualShippingRate = new $shippingRate;
|
|
|
|
|
$actualShippingRate = $actualShippingRate->calculate();
|
2019-07-19 13:56:33 +00:00
|
|
|
|
2019-07-15 10:34:25 +00:00
|
|
|
if (is_array($actualShippingRate)) {
|
|
|
|
|
foreach($actualShippingRate as $actualRate) {
|
|
|
|
|
if ($actualRate->method == $cart->selected_shipping_rate->method) {
|
|
|
|
|
$actualShippingRate = $actualRate;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-27 14:21:17 +00:00
|
|
|
$actualShippingPrice = $actualShippingRate->price;
|
|
|
|
|
$actualShippingBasePrice = $actualShippingRate->base_price;
|
|
|
|
|
|
|
|
|
|
$alreadyAppliedCartRuleCart = $this->cartRuleCart->findWhere([
|
|
|
|
|
'cart_id' => $cart->id
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if (count($alreadyAppliedCartRuleCart)) {
|
2019-06-28 05:26:44 +00:00
|
|
|
$this->resetShipping($cart);
|
|
|
|
|
|
2019-06-27 14:21:17 +00:00
|
|
|
$alreadyAppliedRule = $alreadyAppliedCartRuleCart->first()->cart_rule;
|
|
|
|
|
|
|
|
|
|
$cartShippingRate = $cart->selected_shipping_rate;
|
|
|
|
|
|
|
|
|
|
if (isset($cartShippingRate)) {
|
2019-06-28 05:26:44 +00:00
|
|
|
if ($cartShippingRate->base_price < $actualShippingBasePrice) {
|
2019-06-27 14:21:17 +00:00
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
$this->applyOnShipping($alreadyAppliedRule, $cart);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$this->applyOnShipping($alreadyAppliedRule, $cart);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-06-28 05:26:44 +00:00
|
|
|
$this->resetShipping($cart);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Apply on shipping
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function applyOnShipping($appliedRule, $cart)
|
|
|
|
|
{
|
2019-07-19 13:56:33 +00:00
|
|
|
// $cart = \Cart::getCart();
|
|
|
|
|
|
|
|
|
|
// if (isset($cart->selected_shipping_rate)) {
|
|
|
|
|
// if ($appliedRule->free_shipping && $cart->selected_shipping_rate->base_price > 0) {
|
|
|
|
|
// $cart->selected_shipping_rate->update([
|
|
|
|
|
// 'price' => 0,
|
|
|
|
|
// 'base_price' => 0
|
|
|
|
|
// ]);
|
|
|
|
|
// } else if ($appliedRule->free_shipping == 0 && $appliedRule->apply_to_shipping && $cart->selected_shipping_rate->base_price > 0) {
|
|
|
|
|
// $actionType = config('discount-rules')[$appliedRule->action_type];
|
|
|
|
|
|
|
|
|
|
// if ($appliedRule->apply_to_shipping) {
|
|
|
|
|
// $actionInstance = new $actionType;
|
|
|
|
|
|
|
|
|
|
// $discountOnShipping = $actionInstance->calculateOnShipping($cart);
|
|
|
|
|
|
|
|
|
|
// $discountOnShipping = ($discountOnShipping / 100) * $cart->selected_shipping_rate->base_price;
|
|
|
|
|
|
|
|
|
|
// $cart->selected_shipping_rate->update([
|
|
|
|
|
// 'price' => $cart->selected_shipping_rate->price - core()->convertPrice($discountOnShipping, $cart->cart_currency_code),
|
|
|
|
|
// 'base_price' => $cart->selected_shipping_rate->base_price - $discountOnShipping
|
|
|
|
|
// ]);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2019-06-27 14:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-28 05:26:44 +00:00
|
|
|
/**
|
|
|
|
|
* Resets the shipping for the current items in the cart
|
2019-06-28 06:00:01 +00:00
|
|
|
*
|
|
|
|
|
* @return void
|
2019-06-28 05:26:44 +00:00
|
|
|
*/
|
|
|
|
|
public function resetShipping($cart)
|
|
|
|
|
{
|
2019-06-28 06:00:01 +00:00
|
|
|
$cart = \Cart::getCart();
|
|
|
|
|
|
2019-06-28 07:29:40 +00:00
|
|
|
if (isset($cart->selected_shipping_rate->carrier)) {
|
|
|
|
|
$shippingRate = config('carriers')[$cart->selected_shipping_rate->carrier]['class'];
|
2019-06-28 05:26:44 +00:00
|
|
|
|
2019-06-28 07:29:40 +00:00
|
|
|
$actualShippingRate = new $shippingRate;
|
|
|
|
|
$actualShippingRate = $actualShippingRate->calculate();
|
2019-07-15 10:34:25 +00:00
|
|
|
|
|
|
|
|
if (is_array($actualShippingRate)) {
|
|
|
|
|
foreach($actualShippingRate as $actualRate) {
|
|
|
|
|
if ($actualRate->method == $cart->selected_shipping_rate->method) {
|
|
|
|
|
$actualShippingRate = $actualRate;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-28 07:29:40 +00:00
|
|
|
$actualShippingPrice = $actualShippingRate->price;
|
|
|
|
|
$actualShippingBasePrice = $actualShippingRate->base_price;
|
|
|
|
|
$cartShippingRate = $cart->selected_shipping_rate;
|
2019-06-28 05:26:44 +00:00
|
|
|
|
2019-06-28 07:29:40 +00:00
|
|
|
$cartShippingRate->update([
|
|
|
|
|
'price' => $actualShippingPrice,
|
|
|
|
|
'base_price' => $actualShippingBasePrice
|
|
|
|
|
]);
|
|
|
|
|
}
|
2019-06-28 05:26:44 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
/**
|
|
|
|
|
* Removes any cart rule from the current cart instance
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function clearDiscount()
|
|
|
|
|
{
|
|
|
|
|
$cart = Cart::getCart();
|
2019-06-14 07:51:17 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
$cartItems = $cart->items;
|
2019-06-15 07:43:07 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
foreach($cartItems as $item) {
|
|
|
|
|
$item->update([
|
|
|
|
|
'coupon_code' => NULL,
|
|
|
|
|
'discount_percent' => 0,
|
|
|
|
|
'discount_amount' => 0,
|
|
|
|
|
'base_discount_amount' => 0
|
|
|
|
|
]);
|
|
|
|
|
}
|
2019-06-14 06:40:03 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
$cart->update([
|
|
|
|
|
'coupon_code' => NULL,
|
|
|
|
|
'discount_amount' => 0,
|
|
|
|
|
'base_discount_amount' => 0
|
|
|
|
|
]);
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
return true;
|
2019-06-10 07:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
/**
|
|
|
|
|
* Update discount for least worth item
|
|
|
|
|
*/
|
|
|
|
|
public function updateCartItemAndCart($rule)
|
|
|
|
|
{
|
|
|
|
|
$cart = Cart::getCart();
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-07-19 13:56:33 +00:00
|
|
|
$cartItems = $cart->items;
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-07-19 13:56:33 +00:00
|
|
|
$actionInstance = new $this->rules['cart'][$rule->action_type];
|
2019-06-14 13:54:15 +00:00
|
|
|
|
2019-07-22 06:48:59 +00:00
|
|
|
$impact = $actionInstance->calculate($rule, $cartItems, $cart);
|
|
|
|
|
|
|
|
|
|
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']
|
|
|
|
|
]);
|
2019-07-19 13:56:33 +00:00
|
|
|
}
|
2019-06-14 13:54:15 +00:00
|
|
|
|
2019-07-22 06:48:59 +00:00
|
|
|
// save coupon if rule use it
|
|
|
|
|
if ($rule->use_coupon) {
|
|
|
|
|
$coupon = $rule->coupons->code;
|
2019-06-15 06:57:54 +00:00
|
|
|
|
2019-07-22 06:48:59 +00:00
|
|
|
$item->update([
|
|
|
|
|
'coupon_code' => $coupon
|
|
|
|
|
]);
|
2019-06-14 13:54:15 +00:00
|
|
|
|
2019-07-22 06:48:59 +00:00
|
|
|
$cart->update([
|
|
|
|
|
'coupon_code' => $coupon
|
|
|
|
|
]);
|
|
|
|
|
}
|
2019-06-15 06:57:54 +00:00
|
|
|
}
|
2019-06-10 07:49:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 07:03:34 +00:00
|
|
|
Cart::collectTotals();
|
|
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
return true;
|
2019-06-10 07:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
2019-06-20 07:33:01 +00:00
|
|
|
/**
|
|
|
|
|
* Validate the currently applied cart rule
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function validateRule($rule)
|
|
|
|
|
{
|
|
|
|
|
$applicability = $this->checkApplicability($rule);
|
|
|
|
|
|
|
|
|
|
if ($applicability) {
|
|
|
|
|
if ($rule->status) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 06:57:13 +00:00
|
|
|
/**
|
|
|
|
|
* Retreives all the payment methods from application config
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getPaymentMethods()
|
|
|
|
|
{
|
|
|
|
|
$paymentMethods = config('paymentmethods');
|
|
|
|
|
|
|
|
|
|
return $paymentMethods;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Retreives all the shippin methods from the application config
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getShippingMethods()
|
|
|
|
|
{
|
|
|
|
|
$shippingMethods = config('carriers');
|
|
|
|
|
|
|
|
|
|
return $shippingMethods;
|
|
|
|
|
}
|
2019-06-20 07:33:01 +00:00
|
|
|
|
2019-06-14 07:51:17 +00:00
|
|
|
/**
|
2019-06-17 03:26:30 +00:00
|
|
|
* Checks the rule against the current cart instance whether rule conditions are applicable
|
|
|
|
|
* or not
|
2019-06-14 14:22:58 +00:00
|
|
|
*
|
|
|
|
|
* @return boolean
|
2019-06-14 07:51:17 +00:00
|
|
|
*/
|
2019-06-17 06:54:19 +00:00
|
|
|
protected function testIfAllConditionAreTrue($conditions, $cart)
|
|
|
|
|
{
|
2019-06-25 06:57:13 +00:00
|
|
|
$paymentMethods = $this->getPaymentMethods();
|
|
|
|
|
|
|
|
|
|
$shippingMethods = $this->getShippingMethods();
|
|
|
|
|
|
2019-06-16 11:56:06 +00:00
|
|
|
array_pop($conditions);
|
|
|
|
|
|
2019-06-25 07:44:12 +00:00
|
|
|
$shipping_address = $cart->getShippingAddressAttribute() ?? null;
|
|
|
|
|
|
2019-06-25 09:14:14 +00:00
|
|
|
$shipping_method = $cart->selected_shipping_rate->method_title ?? null;
|
|
|
|
|
|
2019-06-25 07:44:12 +00:00
|
|
|
$shipping_country = $shipping_address->country ?? null;
|
2019-06-25 09:14:14 +00:00
|
|
|
|
2019-06-25 07:44:12 +00:00
|
|
|
$shipping_state = $shipping_address->state ?? null;
|
2019-06-25 09:14:14 +00:00
|
|
|
|
2019-06-25 07:44:12 +00:00
|
|
|
$shipping_postcode = $shipping_address->postcode ?? null;
|
2019-06-25 09:14:14 +00:00
|
|
|
|
2019-06-25 07:44:12 +00:00
|
|
|
$shipping_city = $shipping_address->city ?? null;
|
2019-06-25 09:14:14 +00:00
|
|
|
|
2019-06-26 09:00:32 +00:00
|
|
|
if (isset($cart->payment)) {
|
|
|
|
|
$payment_method = $paymentMethods[$cart->payment->method]['title'];
|
|
|
|
|
} else {
|
|
|
|
|
$payment_method = null;
|
|
|
|
|
}
|
2019-06-25 09:14:14 +00:00
|
|
|
|
2019-06-10 07:49:05 +00:00
|
|
|
$sub_total = $cart->base_sub_total;
|
|
|
|
|
|
|
|
|
|
$total_items = $cart->items_qty;
|
2019-06-25 09:14:14 +00:00
|
|
|
|
2019-06-10 07:49:05 +00:00
|
|
|
$total_weight = 0;
|
|
|
|
|
|
2019-06-25 09:14:14 +00:00
|
|
|
foreach ($cart->items as $item) {
|
2019-06-10 07:49:05 +00:00
|
|
|
$total_weight = $total_weight + $item->base_total_weight;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 06:54:19 +00:00
|
|
|
$result = true;
|
2019-06-16 11:56:06 +00:00
|
|
|
|
2019-06-10 07:49:05 +00:00
|
|
|
foreach ($conditions as $condition) {
|
2019-06-25 09:14:14 +00:00
|
|
|
if (isset($condition->attribute)) {
|
2019-06-19 15:48:16 +00:00
|
|
|
$actual_value = ${$condition->attribute};
|
2019-06-25 09:14:14 +00:00
|
|
|
|
|
|
|
|
} else {
|
2019-06-25 07:49:58 +00:00
|
|
|
$result = false;
|
2019-06-25 09:14:14 +00:00
|
|
|
}
|
2019-06-10 07:49:05 +00:00
|
|
|
|
2019-06-25 09:14:14 +00:00
|
|
|
if (isset($condition->value)) {
|
2019-06-19 15:48:16 +00:00
|
|
|
$test_value = $condition->value;
|
2019-06-25 09:14:14 +00:00
|
|
|
|
|
|
|
|
} else {
|
2019-06-25 07:49:58 +00:00
|
|
|
$result = false;
|
2019-06-25 09:14:14 +00:00
|
|
|
}
|
2019-06-19 15:48:16 +00:00
|
|
|
|
2019-06-25 09:14:14 +00:00
|
|
|
if (isset($condition->condition)) {
|
2019-06-19 15:48:16 +00:00
|
|
|
$test_condition = $condition->condition;
|
2019-06-25 09:14:14 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2019-06-25 07:49:58 +00:00
|
|
|
$result = false;
|
2019-06-25 09:14:14 +00:00
|
|
|
}
|
2019-06-19 15:48:16 +00:00
|
|
|
|
|
|
|
|
if (isset($condition->type) && ($condition->type == 'numeric' || $condition->type == 'string' || $condition->type == 'text')) {
|
2019-06-25 09:14:14 +00:00
|
|
|
if ($condition->type == 'string') {
|
|
|
|
|
$actual_value = strtolower($actual_value);
|
|
|
|
|
|
|
|
|
|
$test_value = strtolower($test_value);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-10 07:49:05 +00:00
|
|
|
if ($test_condition == '=') {
|
|
|
|
|
if ($actual_value != $test_value) {
|
2019-06-17 06:54:19 +00:00
|
|
|
$result = false;
|
2019-06-10 07:49:05 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if ($test_condition == '>=') {
|
|
|
|
|
if (! ($actual_value >= $test_value)) {
|
2019-06-17 06:54:19 +00:00
|
|
|
$result = false;
|
2019-06-10 07:49:05 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if ($test_condition == '<=') {
|
|
|
|
|
if (! ($actual_value <= $test_value)) {
|
2019-06-17 06:54:19 +00:00
|
|
|
$result = false;
|
2019-06-10 07:49:05 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if ($test_condition == '>') {
|
|
|
|
|
if (! ($actual_value > $test_value)) {
|
2019-06-17 06:54:19 +00:00
|
|
|
$result = false;
|
2019-06-10 07:49:05 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if ($test_condition == '<') {
|
|
|
|
|
if (! ($actual_value < $test_value)) {
|
2019-06-17 06:54:19 +00:00
|
|
|
$result = false;
|
2019-06-10 07:49:05 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if ($test_condition == '{}') {
|
2019-06-28 09:43:57 +00:00
|
|
|
if (! str_contains($actual_value, $test_value)) {
|
2019-06-17 06:54:19 +00:00
|
|
|
$result = false;
|
2019-06-10 07:49:05 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if ($test_condition == '!{}') {
|
2019-06-28 09:43:57 +00:00
|
|
|
if (str_contains($actual_value, $test_value)) {
|
2019-06-17 06:54:19 +00:00
|
|
|
$result = false;
|
2019-06-10 07:49:05 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2019-06-17 03:26:30 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Checks the rule against the current cart instance whether rule conditions are applicable
|
|
|
|
|
* or not
|
|
|
|
|
*
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
2019-06-25 05:40:21 +00:00
|
|
|
protected function testIfAnyConditionIsTrue($conditions, $cart)
|
|
|
|
|
{
|
2019-06-25 09:14:14 +00:00
|
|
|
$paymentMethods = $this->getPaymentMethods();
|
|
|
|
|
|
|
|
|
|
$shippingMethods = $this->getShippingMethods();
|
|
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
array_pop($conditions);
|
|
|
|
|
|
2019-06-17 06:54:19 +00:00
|
|
|
$result = false;
|
2019-06-17 03:26:30 +00:00
|
|
|
|
2019-06-25 07:44:12 +00:00
|
|
|
$shipping_address = $cart->getShippingAddressAttribute() ?? null;
|
2019-06-25 09:14:14 +00:00
|
|
|
|
|
|
|
|
$shipping_method = $cart->selected_shipping_rate->method_title ?? null;
|
|
|
|
|
|
2019-06-25 07:44:12 +00:00
|
|
|
$shipping_country = $shipping_address->country ?? null;
|
2019-06-25 09:14:14 +00:00
|
|
|
|
2019-06-25 07:44:12 +00:00
|
|
|
$shipping_state = $shipping_address->state ?? null;
|
2019-06-25 09:14:14 +00:00
|
|
|
|
2019-06-25 07:44:12 +00:00
|
|
|
$shipping_postcode = $shipping_address->postcode ?? null;
|
2019-06-25 09:14:14 +00:00
|
|
|
|
2019-06-25 07:44:12 +00:00
|
|
|
$shipping_city = $shipping_address->city ?? null;
|
2019-06-25 09:14:14 +00:00
|
|
|
|
2019-06-26 09:00:32 +00:00
|
|
|
if (isset($cart->payment)) {
|
|
|
|
|
$payment_method = $paymentMethods[$cart->payment->method]['title'];
|
|
|
|
|
} else {
|
|
|
|
|
$payment_method = null;
|
|
|
|
|
}
|
2019-06-25 09:14:14 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
$sub_total = $cart->base_sub_total;
|
2019-06-25 09:14:14 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
$total_items = $cart->items_qty;
|
2019-06-25 09:14:14 +00:00
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
$total_weight = 0;
|
|
|
|
|
|
|
|
|
|
foreach($cart->items as $item) {
|
|
|
|
|
$total_weight = $total_weight + $item->base_total_weight;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ($conditions as $condition) {
|
2019-06-25 09:14:14 +00:00
|
|
|
if (isset($condition->attribute)) {
|
2019-06-25 07:49:58 +00:00
|
|
|
$actual_value = ${$condition->attribute};
|
2019-06-25 09:14:14 +00:00
|
|
|
|
|
|
|
|
} else {
|
2019-06-25 07:49:58 +00:00
|
|
|
$result = false;
|
2019-06-25 09:14:14 +00:00
|
|
|
}
|
2019-06-25 07:49:58 +00:00
|
|
|
|
2019-06-25 09:14:14 +00:00
|
|
|
if (isset($condition->value)) {
|
2019-06-25 07:49:58 +00:00
|
|
|
$test_value = $condition->value;
|
2019-06-25 09:14:14 +00:00
|
|
|
|
|
|
|
|
} else {
|
2019-06-25 07:49:58 +00:00
|
|
|
$result = false;
|
2019-06-25 09:14:14 +00:00
|
|
|
}
|
2019-06-25 07:49:58 +00:00
|
|
|
|
2019-06-25 09:14:14 +00:00
|
|
|
if (isset($condition->condition)) {
|
2019-06-25 07:49:58 +00:00
|
|
|
$test_condition = $condition->condition;
|
2019-06-25 09:14:14 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2019-06-25 07:49:58 +00:00
|
|
|
$result = false;
|
2019-06-25 09:14:14 +00:00
|
|
|
}
|
2019-06-17 03:26:30 +00:00
|
|
|
|
|
|
|
|
if ($condition->type == 'numeric' || $condition->type == 'string' || $condition->type == 'text') {
|
2019-06-25 09:14:14 +00:00
|
|
|
if ($condition->type == 'string') {
|
|
|
|
|
$actual_value = strtolower($actual_value);
|
|
|
|
|
|
|
|
|
|
$test_value = strtolower($test_value);
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-17 03:26:30 +00:00
|
|
|
if ($test_condition == '=') {
|
2019-06-17 06:54:19 +00:00
|
|
|
if ($actual_value == $test_value) {
|
|
|
|
|
$result = true;
|
2019-06-17 03:26:30 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if ($test_condition == '>=') {
|
2019-06-17 06:54:19 +00:00
|
|
|
if ($actual_value >= $test_value) {
|
|
|
|
|
$result = true;
|
2019-06-17 03:26:30 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if ($test_condition == '<=') {
|
2019-06-17 06:54:19 +00:00
|
|
|
if ($actual_value <= $test_value) {
|
|
|
|
|
$result = true;
|
2019-06-17 03:26:30 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if ($test_condition == '>') {
|
2019-06-17 06:54:19 +00:00
|
|
|
if ($actual_value > $test_value) {
|
|
|
|
|
$result = true;
|
2019-06-17 03:26:30 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if ($test_condition == '<') {
|
2019-06-17 06:54:19 +00:00
|
|
|
if ($actual_value < $test_value) {
|
|
|
|
|
$result = true;
|
2019-06-17 03:26:30 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if ($test_condition == '{}') {
|
2019-06-28 09:43:57 +00:00
|
|
|
if (! str_contains($actual_value, $test_value)) {
|
2019-06-17 06:54:19 +00:00
|
|
|
$result = true;
|
2019-06-17 03:26:30 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
} else if ($test_condition == '!{}') {
|
2019-06-28 09:43:57 +00:00
|
|
|
if (str_contains($actual_value, $test_value)) {
|
2019-06-17 06:54:19 +00:00
|
|
|
$result = true;
|
2019-06-17 03:26:30 +00:00
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2019-06-10 07:49:05 +00:00
|
|
|
}
|