Cart rule frontend normalization

This commit is contained in:
Prashant Singh 2019-06-14 13:21:17 +05:30
parent 7f9409bd8e
commit c52238e45e
7 changed files with 82 additions and 60 deletions

View File

@ -79,24 +79,6 @@ return [
'type' => 'boolean'
]
],
], [
'key' => 'customer.settings.credit-max',
'name' => 'admin::app.admin.system.credit-max',
'sort' => 4,
'fields' => [
[
'name' => 'Use Credit Max',
'title' => 'admin::app.admin.system.use-credit-max',
'type' => 'boolean',
'channel_based' => true
], [
'name' => 'Credit Max Value',
'title' => 'admin::app.admin.system.credit-max-value',
'type' => 'text',
'validation' => 'decimal',
'channel_based' => true
]
],
], [
'key' => 'general',
'name' => 'admin::app.admin.system.general',

View File

@ -1305,13 +1305,13 @@ class Cart {
*
* @return void
*/
public function removeDiscount()
public function clearDiscount()
{
$cartItems = $this->getCart()->items;
foreach($cartItems as $item) {
$item->update([
'coupon_code' => null,
'coupon_code' => NULL,
'discount_percent' => 0,
'discount_amount' => 0,
'base_discount_amount' => 0
@ -1319,7 +1319,7 @@ class Cart {
}
$this->getCart()->update([
'coupon_code' => null,
'coupon_code' => NULL,
'discount_amount' => 0,
'base_discount_amount' => 0
]);
@ -1327,12 +1327,12 @@ class Cart {
return true;
}
public function removeCoupon()
{
$result = $this->discount->removeCoupon();
// public function removeCoupon()
// {
// $result = $this->discount->removeCoupon();
return $result;
}
// return $result;
// }
public function leastWorthItem()
{

View File

@ -38,6 +38,23 @@ class Discount
public function applyNonCouponAbleRule()
{
$cart = \Cart::getCart();
$previousRule = $this->cartRuleCart->findWhere([
'cart_id' => $cart->id
]);
if ($previousRule->count()) {
$previousRule = $previousRule->first()->cart_rule;
if ($previousRule->use_coupon) {
return 'false';
}
} else {
\Cart::clearDiscount();
}
if (auth()->guard('customer')->check()) {
$nonCouponAbleRules = $this->cartRule->findWhere([
'use_coupon' => 0,
@ -104,6 +121,17 @@ class Discount
if ($rule['rule']->id == $leastId) {
$rule['used_coupon'] = false;
foreach (\Cart::getCart()->items as $item) {
if ($item->id == $itemId['item_id']) {
$item->update([
'discount_amount' => array_first($canBeApplied)['discount'],
'base_discount_amount' => array_first($canBeApplied)['discount']
]);
break;
}
}
$this->save($rule['rule']);
return $rule;
@ -113,8 +141,6 @@ class Discount
}
if ($canBeApplied->count()) {
$this->save(array_first($canBeApplied)['rule']);
$itemId = array_first($canBeApplied);
foreach (\Cart::getCart()->items as $item) {
@ -128,14 +154,18 @@ class Discount
}
}
$this->save(array_first($canBeApplied)['rule']);
return array_first($canBeApplied);
} else {
return false;
return 'false';
}
}
public function applyCouponAbleRule($code)
{
$cart = \Cart::getCart();
if (auth()->guard('customer')->check()) {
$couponAbleRules = $this->cartRule->findWhere([
'use_coupon' => 1,
@ -256,13 +286,20 @@ class Discount
$itemId = array_first($canBeApplied);
foreach (\Cart::getCart()->items as $item) {
foreach ($cart->items as $item) {
if ($item->id == $itemId['item_id']) {
$item->update([
'discount_amount' => array_first($canBeApplied)['discount'],
'base_discount_amount' => array_first($canBeApplied)['discount']
'base_discount_amount' => array_first($canBeApplied)['discount'],
'coupon_code' => $rule->coupons->code
]);
$cart->update([
'coupon_code' => $rule->coupons->code
]);
\Cart::collectTotals();
break;
}
}
@ -385,6 +422,26 @@ class Discount
return false;
}
/**
* Removes the cart rule from the cart
*/
public function removeRule()
{
$cart = Cart::getCart();
$appliedRule = $this->cartRuleCart->findWhere([
'cart_id' => $cart->id
]);
if ($appliedRule->count() == 0) {
Cart::clearDiscount();
Cart::collectTotals();
}
return true;
}
public function removeCoupon()
{
$cart = \Cart::getCart();

View File

@ -82,21 +82,7 @@ class CartController extends Controller
*/
public function index()
{
$cart = Cart::getCart();
$appliedRule = $this->cartRuleCart->findWhere([
'cart_id' => $cart->id
]);
if ($appliedRule->count() == 0) {
Cart::removeDiscount();
Cart::collectTotals();
}
Cart::applyNonCoupon();
return view($this->_config['view'])->with('cart', $cart);
return view($this->_config['view'])->with('cart', Cart::getCart());
}
/**

View File

@ -67,18 +67,6 @@ class OnepageController extends Controller
if (Cart::hasError())
return redirect()->route('shop.checkout.cart.index');
$cart = Cart::getCart();
$appliedRule = $this->cartRuleCart->findWhere([
'cart_id' => $cart->id
]);
if ($appliedRule->count() == 0) {
Cart::removeDiscount();
Cart::collectTotals();
}
Cart::applyNonCoupon();
return view($this->_config['view'])->with('cart', Cart::getCart());
@ -133,7 +121,7 @@ class OnepageController extends Controller
if (Cart::hasError() || !$shippingMethod || !Cart::saveShippingMethod($shippingMethod))
return response()->json(['redirect_url' => route('shop.checkout.cart.index')], 403);
Cart::applyNonCoupon();
Cart::applyNonCoupon();
Cart::collectTotals();

View File

@ -378,7 +378,9 @@
axios.post('{{ route('shop.checkout.check.coupons') }}', {
code: this_this.code
}).then(function(response) {
console.log(response.data);
}).catch(function(error) {
console.log(error.data);
});
},
}

View File

@ -36,7 +36,7 @@
<label class="right">{{ core()->currency($cart->base_grand_total) }}</label>
</div>
@if (! request()->is('checkout/cart'))
@if (! request()->is('checkout/cart') && ! $cart->coupon_code)
<div class="discount">
<div class="discount-group">
<form class="coupon-form" method="post" @submit.prevent="onSubmit">
@ -48,5 +48,12 @@
</form>
</div>
</div>
@else
<div class="discount-details-group">
<div class="item-detail">
<label>{{ __('shop::app.checkout.total.coupon-applied') }}</label>
<label class="right" style="display: inline-flex; align-items: center;">{{ $cart->coupon_code }} <span class="icon cross-icon" title="{{ __('shop::app.checkout.total.remove-coupon') }}"></span></label>
</div>
</div>
@endif
</div>