From 5a9464e64c9af1ef6ead8684d3cbda8b38a85685 Mon Sep 17 00:00:00 2001 From: Luke Policinski Date: Tue, 17 Nov 2015 15:39:22 +0000 Subject: [PATCH] Fixing coupon multiple coupons --- build/logs/clover.xml | 343 +++++++++++++++------------- src/LaraCart.php | 22 +- src/LaraCartServiceProvider.php | 4 + {config => src/config}/laracart.php | 18 +- tests/LaraCartTest.php | 9 + 5 files changed, 219 insertions(+), 177 deletions(-) rename {config => src/config}/laracart.php (81%) diff --git a/build/logs/clover.xml b/build/logs/clover.xml index bd330ee..e727f36 100644 --- a/build/logs/clover.xml +++ b/build/logs/clover.xml @@ -1,6 +1,6 @@ - - + + @@ -82,8 +82,8 @@ - - + + @@ -96,16 +96,16 @@ - - - + + + - - - - - + + + + + @@ -136,7 +136,7 @@ - + @@ -186,158 +186,167 @@ - - - - - - - + - - - - - - - - - + + + + + + + + + + - - - - - - - + + + + + + + + - - + + - - - - - - + + + + + - - - - - - - + + + + + + - - - - - - - + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - - - + + + + + + - - + + - - - + + + - - - - + + + - - + + + + + @@ -381,6 +390,26 @@ + + + + + + + + + + + + + + + + + + + + @@ -448,26 +477,14 @@ - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + diff --git a/src/LaraCart.php b/src/LaraCart.php index 4f621e6..5b7a91f 100644 --- a/src/LaraCart.php +++ b/src/LaraCart.php @@ -55,7 +55,7 @@ public function setInstance($instance = 'default') */ public function get($instance = 'default') { - if (empty($this->cart = \Session::get(config('laracart.cache_prefix', 'laracart.') . $instance))) { + if (empty($this->cart = \Session::get(config('laracart.cache_prefix', 'laracart').'.'.$instance))) { $this->cart = new Cart($instance); } @@ -67,7 +67,7 @@ public function get($instance = 'default') */ public function update() { - \Session::set(config('laracart.cache_prefix', 'laracart.') . $this->cart->instance, $this->cart); + \Session::set(config('laracart.cache_prefix', 'laracart').'.'.$this->cart->instance, $this->cart); \Event::fire('laracart.update', $this->cart); } @@ -188,8 +188,15 @@ public function getItems() * * @return CartItem */ - public function add($itemID, $name = null, $qty = 1, $price = '0.00', $options = [], $taxable = true, $lineItem = false) - { + public function add( + $itemID, + $name = null, + $qty = 1, + $price = '0.00', + $options = [], + $taxable = true, + $lineItem = false + ) { return $this->addItem( new CartItem( $itemID, @@ -365,6 +372,10 @@ public function findCoupon($code) */ public function addCoupon(CouponContract $coupon) { + if(!$this->cart->multipleCoupons) { + $this->cart->coupons = []; + } + $this->cart->coupons[$coupon->code] = $coupon; $this->update(); @@ -521,6 +532,7 @@ public function subTotal($tax = false, $format = true, $withDiscount = true) return $this->formatMoney($total); } + return number_format($total, 2); } @@ -536,7 +548,7 @@ public function total($format = true, $withDiscount = true) { $total = $this->subTotal(true, false, false) + $this->feeTotals(false); - if($withDiscount) { + if ($withDiscount) { $total -= $this->totalDiscount(false); } diff --git a/src/LaraCartServiceProvider.php b/src/LaraCartServiceProvider.php index dfc8eed..3a9c1ba 100644 --- a/src/LaraCartServiceProvider.php +++ b/src/LaraCartServiceProvider.php @@ -23,6 +23,10 @@ public function boot() __DIR__ . '/config/laracart.php' => config_path('laracart.php'), ] ); + + $this->mergeConfigFrom( + __DIR__.'/config/laracart.php', 'laracart' + ); } /** diff --git a/config/laracart.php b/src/config/laracart.php similarity index 81% rename from config/laracart.php rename to src/config/laracart.php index ee4ed79..8276b71 100644 --- a/config/laracart.php +++ b/src/config/laracart.php @@ -7,7 +7,7 @@ |-------------------------------------------------------------------------- | */ - 'cache_prefix' => 'laracart_', + 'cache_prefix' => 'laracart', /* |-------------------------------------------------------------------------- @@ -29,7 +29,7 @@ |-------------------------------------------------------------------------- | */ - 'locale' => 'EN_US.UTF-8', + 'locale' => 'en_US.UTF-8', /* |-------------------------------------------------------------------------- @@ -46,13 +46,13 @@ |-------------------------------------------------------------------------- | */ - 'tax' => '.07', + 'tax' => null, /* - |-------------------------------------------------------------------------- - | Allows you to configure if a user can apply multiple coupons - |-------------------------------------------------------------------------- - | - */ - 'multipleCoupons' => false, + |-------------------------------------------------------------------------- + | Allows you to configure if a user can apply multiple coupons + |-------------------------------------------------------------------------- + | + */ + 'multiple_coupons' => false ]; diff --git a/tests/LaraCartTest.php b/tests/LaraCartTest.php index cd08c64..c693033 100644 --- a/tests/LaraCartTest.php +++ b/tests/LaraCartTest.php @@ -341,6 +341,15 @@ public function testAddCoupon() $this->assertEquals('1', $percentCoupon->discount()); + $this->assertCount(1, $this->laracart->getCoupons()); + + $cart = $this->laracart->get()->cart; + $cart->multipleCoupons = true; + + $fixedCoupon = new LukePOLO\LaraCart\Coupons\Fixed('10OFF', 10); + $this->laracart->addCoupon($fixedCoupon); + $this->assertEquals($fixedCoupon, $this->laracart->findCoupon('10OFF')); + $this->assertCount(2, $this->laracart->getCoupons()); }