Merge pull request #1128 from prashant-webkul/development

Apply to shipping and free shipping now added in normal flow
This commit is contained in:
Jitendra Singh 2019-06-28 13:00:41 +05:30 committed by GitHub
commit 338fd38fdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 139 additions and 50 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
*

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();
// 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

@ -48,26 +48,6 @@ class ValidatesDiscount extends Discount
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();

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
*