diff --git a/packages/Webkul/Checkout/src/Cart.php b/packages/Webkul/Checkout/src/Cart.php index 6bdbbffad..fc42a4823 100755 --- a/packages/Webkul/Checkout/src/Cart.php +++ b/packages/Webkul/Checkout/src/Cart.php @@ -1297,7 +1297,8 @@ class Cart { 'id' => $item->id, 'total' => $item->total, 'base_total' => $leastValue, - 'quantity' => $item->quantity + 'quantity' => $item->quantity, + 'price' => $item->price ]; } } diff --git a/packages/Webkul/Discount/src/Database/Migrations/2019_05_13_024326_create_cart_rule_table.php b/packages/Webkul/Discount/src/Database/Migrations/2019_05_13_024326_create_cart_rule_table.php index 03f6273ec..41859725a 100644 --- a/packages/Webkul/Discount/src/Database/Migrations/2019_05_13_024326_create_cart_rule_table.php +++ b/packages/Webkul/Discount/src/Database/Migrations/2019_05_13_024326_create_cart_rule_table.php @@ -39,8 +39,8 @@ class CreateCartRuleTable extends Migration $table->integer('sort_order')->unsigned()->default(0); $table->string('action_type')->nullable(); $table->decimal('disc_amount', 12, 4)->default(0); - $table->decimal('disc_quantity', 12, 4)->default(0); - $table->string('disc_threshold')->default(0); + $table->decimal('disc_quantity', 12, 4)->default(1); + $table->string('disc_threshold')->default(1); $table->integer('coupon_type')->default(1); $table->boolean('auto_generation')->default(0); $table->boolean('apply_to_shipping')->default(0); diff --git a/packages/Webkul/Discount/src/Helpers/Discount.php b/packages/Webkul/Discount/src/Helpers/Discount.php index cee384b68..f48e14796 100644 --- a/packages/Webkul/Discount/src/Helpers/Discount.php +++ b/packages/Webkul/Discount/src/Helpers/Discount.php @@ -310,6 +310,8 @@ class Discount \Cart::collectTotals(); + $report['grand_total'] = core()->currency(\Cart::getCart()->grand_total); + break; } } @@ -376,24 +378,36 @@ class Discount $leastWorthItem = \Cart::leastWorthItem(); $realQty = $leastWorthItem['quantity']; - if ($cart->items_qty >= $disc_threshold && $realQty >= $disc_quantity) { + if ($cart->items_qty >= $disc_threshold) { if ($action_type == config('pricerules.cart.validation.0')) { - $amountDiscounted = $leastWorthItem['total'] * ($disc_amount / 100); + $amountDiscounted = $leastWorthItem['price'] * ($disc_amount / 100); - if ($amountDiscounted > $leastWorthItem['total']) { - $amountDiscounted = $leastWorthItem['total']; + if ($realQty > $disc_quantity) { + $amountDiscounted = $amountDiscounted * $disc_quantity; + } + + if ($amountDiscounted > $leastWorthItem['price']) { + $amountDiscounted = $leastWorthItem['price']; } } else if ($action_type == config('pricerules.cart.validation.1')) { $amountDiscounted = $disc_amount; - if ($amountDiscounted > $leastWorthItem['total']) { - $amountDiscounted = $leastWorthItem['total']; + if ($realQty > $disc_quantity) { + $amountDiscounted = $amountDiscounted * $disc_quantity; + } + + if ($amountDiscounted > $leastWorthItem['price']) { + $amountDiscounted = $leastWorthItem['price']; } } else if ($action_type == config('pricerules.cart.validation.2')) { $amountDiscounted = $disc_amount; - if ($amountDiscounted > $leastWorthItem['total']) { - $amountDiscounted = $leastWorthItem['total']; + if ($realQty > $disc_quantity) { + $amountDiscounted = $amountDiscounted * $disc_quantity; + } + + if ($amountDiscounted > $leastWorthItem['price']) { + $amountDiscounted = $leastWorthItem['price']; } } } @@ -459,7 +473,6 @@ class Discount if ($existingRule->count()) { if ($existingRule->first()->delete()) { - foreach ($cart->items as $item) { if ($item->discount_amount > 0) { $item->update([ @@ -477,6 +490,8 @@ class Discount 'base_discount_amount' => 0 ]); + \Cart::collectTotals(); + return true; } else { return false; diff --git a/packages/Webkul/Shop/src/Http/Controllers/CartController.php b/packages/Webkul/Shop/src/Http/Controllers/CartController.php index 274e1939c..48ffb82bf 100755 --- a/packages/Webkul/Shop/src/Http/Controllers/CartController.php +++ b/packages/Webkul/Shop/src/Http/Controllers/CartController.php @@ -305,12 +305,16 @@ class CartController extends Controller if ($result) { return response()->json([ 'success' => true, - 'message' => trans('admin::app.promotion.status.coupon-removed') + 'message' => trans('admin::app.promotion.status.coupon-removed'), + 'data' => [ + 'grand_total' => core()->currency(Cart::getCart()->grand_total) + ] ]); } else { return response()->json([ 'success' => false, - 'message' => trans('admin::app.promotion.status.coupon-remove-failed') + 'message' => trans('admin::app.promotion.status.coupon-remove-failed'), + 'data' => null ]); } } diff --git a/packages/Webkul/Shop/src/Resources/views/checkout/onepage.blade.php b/packages/Webkul/Shop/src/Resources/views/checkout/onepage.blade.php index 071dbf16b..607d84292 100755 --- a/packages/Webkul/Shop/src/Resources/views/checkout/onepage.blade.php +++ b/packages/Webkul/Shop/src/Resources/views/checkout/onepage.blade.php @@ -538,7 +538,7 @@ document.getElementById("discount-detail").style.display = "block"; document.getElementById("discount-detail-discount-amount").innerHTML = response.data.result.formatted_discount; - document.getElementById("discount-detail-discount-amount").innerHTML = response.data.result.formatted_discount; + document.getElementById("grand-total-amount-detail").innerHTML = response.data.result.grand_total; }).catch(function(error) { console.log(error.data); }); @@ -550,8 +550,14 @@ axios.post('{{ route('shop.checkout.remove.coupon') }}') .then(function(response) { this_this.coupon_used = false; + this_this.code = ''; + + console.log(response.data.data.grand_total); + document.getElementById("discount-detail").style.display = "none"; + + document.getElementById("grand-total-amount-detail").innerHTML = response.data.data.grand_total; }).catch(function(error) { console.log(error.data);