This commit is contained in:
Prashant Singh 2019-06-28 16:33:08 +05:30
commit ba8fb649e7
11 changed files with 147 additions and 132 deletions

View File

@ -68,7 +68,7 @@
<div class="control-group" :class="[errors.has('ends_till') ? 'has-error' : '']">
<label for="ends_till">{{ __('admin::app.promotion.general-info.ends-till') }}</label>
<input type="text" class="control" v-model="ends_till" v-validate="'after:starts_from'" name="ends_till" data-vv-as="&quot;{{ __('admin::app.promotion.general-info.ends-till') }}&quot;">
<input type="text" class="control" v-model="ends_till" name="ends_till" data-vv-as="&quot;{{ __('admin::app.promotion.general-info.ends-till') }}&quot;">
<span class="control-error" v-if="errors.has('ends_till')">@{{ errors.first('ends_till') }}</span>
</div>

View File

@ -1116,7 +1116,8 @@ class Cart {
*
* Move a wishlist item to cart
*/
public function moveToCart($wishlistItem) {
public function moveToCart($wishlistItem)
{
$product = $wishlistItem->product;
if ($product->type == 'simple') {
@ -1140,7 +1141,8 @@ class Cart {
*
* @param instance cartItem $id
*/
public function moveToWishlist($itemId) {
public function moveToWishlist($itemId)
{
$cart = $this->getCart();
$items = $cart->items;
$wishlist = [];
@ -1187,7 +1189,8 @@ class Cart {
*
* @return response mixed
*/
public function proceedToBuyNow($id, $quantity) {
public function proceedToBuyNow($id, $quantity)
{
$product = $this->product->findOneByField('id', $id);
if ($product->type == 'configurable') {

View File

@ -27,7 +27,7 @@ class BuyAGetB extends Action
$amountDiscounted = $amountDiscounted * $realQty;
}
if ($amountDiscounted > $item['base_price']) {
if ($amountDiscounted > $item['base_price'] && $realQty == 1) {
$amountDiscounted = $item['base_price'];
}
}

View File

@ -27,7 +27,7 @@ class FixedAmount extends Action
$amountDiscounted = $amountDiscounted * $realQty;
}
if ($amountDiscounted > $item['base_price']) {
if ($amountDiscounted > $item['base_price'] && $realQty == 1) {
$amountDiscounted = $item['base_price'];
}
}
@ -43,10 +43,10 @@ class FixedAmount extends Action
*/
public function calculateOnShipping($cart)
{
$cart = \Cart::getCart();
$percentOfDiscount = ($cart->base_discount_amount * 100) / $cart->base_grand_total;
$discountOnShipping = ($percentOfDiscount / 100) * $cart->selected_shipping_rate->base_price;
return $discountOnShipping;
return $percentOfDiscount;
}
}

View File

@ -24,7 +24,7 @@ class PercentOfProduct extends Action
$amountDiscounted = $amountDiscounted * $realQty;
}
if ($amountDiscounted > $item['base_price']) {
if ($amountDiscounted > $item['base_price'] && $realQty == 1) {
$amountDiscounted = $item['base_price'];
}
}
@ -40,10 +40,10 @@ class PercentOfProduct extends Action
*/
public function calculateOnShipping($cart)
{
$cart = \Cart::getCart();
$percentOfDiscount = ($cart->base_discount_amount * 100) / $cart->base_grand_total;
$discountOnShipping = ($percentOfDiscount / 100) * $cart->selected_shipping_rate->base_price;
return $discountOnShipping;
return $percentOfDiscount;
}
}

View File

@ -34,7 +34,7 @@ return [
'actions' => [
'percent_of_product' => 'Percentage of product',
'fixed_amount' => 'Apply as fixed amount',
'buy_a_get_b' => 'Buy A get B'
// 'buy_a_get_b' => 'Buy A get B'
],
'validation' => [

View File

@ -136,6 +136,8 @@ class CouponAbleRule extends Discount
if ($existingRule->count()) {
$existingRule->first()->delete();
$this->resetShipping($cart);
foreach ($cart->items as $item) {
if ($item->discount_amount > 0) {
$item->update([

View File

@ -132,11 +132,15 @@ abstract class Discount
{
$cart = \Cart::getCart();
// create or update
// Create or update
$existingRule = $this->cartRuleCart->findWhere([
'cart_id' => $cart->id
]);
if ($rule->use_coupon) {
$this->resetShipping($cart);
}
if (count($existingRule)) {
if ($existingRule->first()->cart_rule_id != $rule->id) {
$existingRule->first()->update([
@ -147,10 +151,15 @@ abstract class Discount
$this->updateCartItemAndCart($rule);
if ($rule->use_coupon) {
$this->checkOnShipping($cart);
}
return true;
} else {
// $this->checkOnShipping($cart);
}
} else {
$this->cartRuleCart->create([
'cart_id' => $cart->id,
'cart_rule_id' => $rule->id
@ -160,12 +169,116 @@ abstract class Discount
$this->updateCartItemAndCart($rule);
if ($rule->use_coupon) {
$this->checkOnShipping($cart);
}
return true;
}
return false;
}
/**
* Checks whether rule is getting applied on shipping or not
*/
public function checkOnShipping($cart)
{
if (! isset($cart->selected_shipping_rate)) {
return false;
}
$shippingRate = config('carriers')[$cart->selected_shipping_rate->carrier]['class'];
$actualShippingRate = new $shippingRate;
$actualShippingRate = $actualShippingRate->calculate();
$actualShippingPrice = $actualShippingRate->price;
$actualShippingBasePrice = $actualShippingRate->base_price;
$alreadyAppliedCartRuleCart = $this->cartRuleCart->findWhere([
'cart_id' => $cart->id
]);
if (count($alreadyAppliedCartRuleCart)) {
$this->resetShipping($cart);
$alreadyAppliedRule = $alreadyAppliedCartRuleCart->first()->cart_rule;
$cartShippingRate = $cart->selected_shipping_rate;
if (isset($cartShippingRate)) {
if ($cartShippingRate->base_price < $actualShippingBasePrice) {
return false;
} else {
$this->applyOnShipping($alreadyAppliedRule, $cart);
}
} else {
$this->applyOnShipping($alreadyAppliedRule, $cart);
}
} else {
$this->resetShipping($cart);
}
}
/**
* Apply on shipping
*
* @return void
*/
public function applyOnShipping($appliedRule, $cart)
{
$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
]);
}
}
}
}
/**
* Resets the shipping for the current items in the cart
*
* @return void
*/
public function resetShipping($cart)
{
$cart = \Cart::getCart();
if (isset($cart->selected_shipping_rate->carrier)) {
$shippingRate = config('carriers')[$cart->selected_shipping_rate->carrier]['class'];
$actualShippingRate = new $shippingRate;
$actualShippingRate = $actualShippingRate->calculate();
$actualShippingPrice = $actualShippingRate->price;
$actualShippingBasePrice = $actualShippingRate->base_price;
$cartShippingRate = $cart->selected_shipping_rate;
$cartShippingRate->update([
'price' => $actualShippingPrice,
'base_price' => $actualShippingBasePrice
]);
}
}
/**
* Removes any cart rule from the current cart instance
*
@ -570,7 +683,7 @@ abstract class Discount
break;
}
} else if ($test_condition == '{}') {
if (str_contains($actual_value, $test_value)) {
if (! str_contains($actual_value, $test_value)) {
$result = true;
break;

View File

@ -37,6 +37,8 @@ class NonCouponAbleRule extends Discount
// if the validation fails then the cart rule gets deleted from cart rule cart
$alreadyAppliedCartRuleCart->first()->delete();
$this->resetShipping($cart);
// all discount is cleared fro mthe cart and cart items table
$this->clearDiscount();
@ -117,7 +119,7 @@ class NonCouponAbleRule extends Discount
if (count($endRules) == 1) {
$this->save(array_first($endRules)['rule']);
return $endRules;
return array_first($endRules)['impact'];
}
$maxImpact = 0;

View File

@ -2,9 +2,10 @@
namespace Webkul\Discount\Helpers;
use Webkul\Discount\Helpers\Discount;
use Webkul\Discount\Repositories\CartRuleCartRepository as CartRuleCart;
class ValidatesDiscount
class ValidatesDiscount extends Discount
{
/**
* CartRuleCartRepository instance
@ -21,6 +22,11 @@ class ValidatesDiscount
$this->cartRuleCart = $cartRuleCart;
}
public function apply($code)
{
return ;
}
/**
* Validates the currently applied cart rule on the current cart
*
@ -42,26 +48,6 @@ class ValidatesDiscount
if (! $applicability) {
return $this->remove();
} else {
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);
$cart->selected_shipping_rate->update([
'price' => $cart->selected_shipping_rate->base_price - $discountOnShipping,
'base_price' => $cart->selected_shipping_rate->price - core()->convertPrice($discountOnShipping, $cart->cart_currency_code)
]);
}
}
}
} else {
return $this->remove();
@ -71,86 +57,6 @@ class ValidatesDiscount
return false;
}
/**
* Checks whether coupon is getting applied on current cart instance or not
*
* @return boolean
*/
public function checkApplicability($rule)
{
$cart = \Cart::getCart();
$timeBased = false;
// 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;
}
} else {
$timeBased = true;
}
$channelBased = false;
// channel based constraints
foreach ($rule->channels as $channel) {
if ($channel->channel_id == core()->getCurrentChannel()->id) {
$channelBased = true;
}
}
$customerGroupBased = false;
// customer groups based constraints
if (auth()->guard('customer')->check()) {
foreach ($rule->customer_groups as $customer_group) {
if (auth()->guard('customer')->user()->group->exists()) {
if ($customer_group->customer_group_id == auth()->guard('customer')->user()->group->id) {
$customerGroupBased = true;
}
}
}
} else {
foreach ($rule->customer_groups as $customer_group) {
if ($customer_group->customer_group->code == 'guest') {
$customerGroupBased = true;
}
}
}
$conditionsBased = true;
//check conditions
if ($rule->conditions != null) {
$conditions = json_decode(json_decode($rule->conditions));
$test_mode = array_last($conditions);
if ($test_mode->criteria == 'any_is_true') {
$conditionsBased = $this->testIfAnyConditionIsTrue($conditions, $cart);
}
if ($test_mode->criteria == 'all_are_true') {
$conditionsBased = $this->testIfAllConditionAreTrue($conditions, $cart);
}
}
if ($timeBased && $channelBased && $customerGroupBased && $conditionsBased) {
return true;
} else {
return false;
}
}
/**
* Removes the already applied coupon on the current cart instance
*

View File

@ -166,6 +166,8 @@ class OnepageController extends Controller
$this->nonCoupon->apply();
$this->nonCoupon->checkOnShipping(Cart::getCart());
Cart::collectTotals();
$cart = Cart::getCart();
@ -285,19 +287,6 @@ class OnepageController extends Controller
return $result;
}
/**
* Applies non couponable rule if present
*
* @return Void
*/
public function applyNonCouponAbleRule()
{
$cart = Cart::getCart();
$nonCouponAbleRules = Cart::applyNonCoupon();
return $nonCouponAbleRules;
}
/**
* Initiates the removal of couponable cart rule
*