diff --git a/.travis.yml b/.travis.yml index 39cc473f1..41641e4d2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: php php: - - 7.1 - + - 7.2 + sudo: false cache: @@ -17,4 +17,4 @@ before_script: script: - vendor/bin/phpunit --coverage-clover=coverage.xml after_success: - - bash <(curl -s https://codecov.io/bash) \ No newline at end of file + - bash <(curl -s https://codecov.io/bash) diff --git a/app/Http/Controllers/Front/CartController.php b/app/Http/Controllers/Front/CartController.php index 56ef3d4fe..53d8c5247 100644 --- a/app/Http/Controllers/Front/CartController.php +++ b/app/Http/Controllers/Front/CartController.php @@ -86,6 +86,11 @@ public function store(AddToCartRequest $request) { $product = $this->productRepo->findProductById($request->input('product')); + if (!$product->hasAvailableQuantity()) { + return redirect()->route('cart.index') + ->with('error', 'Item not available'); + } + if ($product->attributes()->count() > 0) { $productAttr = $product->attributes()->where('default', 1)->first(); @@ -110,6 +115,11 @@ public function store(AddToCartRequest $request) $this->cartRepo->addToCart($product, $request->input('quantity'), $options); + $productRepository = new ProductRepository($product); + $productRepository->updateProduct([ + 'quantity' => $product->quantity - 1 + ]); + return redirect()->route('cart.index') ->with('message', 'Add to cart successful'); } @@ -123,6 +133,30 @@ public function store(AddToCartRequest $request) */ public function update(Request $request, $id) { + if ($request->input('quantity') <= 0) { + return $this->destroy($id); + } + + $item = $this->cartRepo->findItem($id); + $productRepository = new ProductRepository($item->product); + + if ($item->qty > $request->input('quantity')) { + $productRepository->updateProduct([ + 'quantity' => $item->qty - $request->input('quantity') + ]); + } + else { + if (!$item->product->hasAvailableQuantity($request->input('quantity') - $item->product->quantity)) { + return redirect()->route('cart.index') + ->with('error', 'Quantity not available'); + } + + $productRepository->updateProduct([ + 'quantity' => $item->product->quantity - ($request->input('quantity') - $item->product->quantity) + ]); + + } + $this->cartRepo->updateQuantityInCart($id, $request->input('quantity')); request()->session()->flash('message', 'Update cart successful'); @@ -137,8 +171,16 @@ public function update(Request $request, $id) */ public function destroy($id) { + $item = $this->cartRepo->findItem($id); $this->cartRepo->removeToCart($id); + $productRepository = new ProductRepository($item->product); + $productRepository->updateProduct([ + 'quantity' => $item->product->quantity + $item->qty + ]); + + + request()->session()->flash('message', 'Removed to cart successful'); return redirect()->route('cart.index'); } diff --git a/app/Http/Controllers/Front/CheckoutController.php b/app/Http/Controllers/Front/CheckoutController.php index 404b88bde..d67af5060 100644 --- a/app/Http/Controllers/Front/CheckoutController.php +++ b/app/Http/Controllers/Front/CheckoutController.php @@ -100,6 +100,7 @@ public function __construct( public function index(Request $request) { $products = $this->cartRepo->getCartItems(); + $customer = $request->user(); $rates = null; $shipment_object_id = null; diff --git a/app/Http/Controllers/Front/ProductController.php b/app/Http/Controllers/Front/ProductController.php index 8aec05fc3..e86b865d3 100644 --- a/app/Http/Controllers/Front/ProductController.php +++ b/app/Http/Controllers/Front/ProductController.php @@ -62,8 +62,7 @@ public function show(string $slug) 'product', 'images', 'productAttributes', - 'category', - 'combos' + 'category' )); } } diff --git a/app/Shop/Products/Product.php b/app/Shop/Products/Product.php index 46d17fcc4..92ec9992e 100644 --- a/app/Shop/Products/Product.php +++ b/app/Shop/Products/Product.php @@ -144,4 +144,9 @@ public function brand() { return $this->belongsTo(Brand::class); } + + public function hasAvailableQuantity($quantity = 1) + { + return $this->quantity >= $quantity; + } } diff --git a/resources/views/front/carts/cart.blade.php b/resources/views/front/carts/cart.blade.php index eb41d139c..19d3c4091 100644 --- a/resources/views/front/carts/cart.blade.php +++ b/resources/views/front/carts/cart.blade.php @@ -114,6 +114,9 @@ @else
+
+ @include('layouts.errors-and-messages') +

No products in cart yet. Shop now!

diff --git a/resources/views/front/checkout.blade.php b/resources/views/front/checkout.blade.php index 88e48adaa..7139ca57b 100644 --- a/resources/views/front/checkout.blade.php +++ b/resources/views/front/checkout.blade.php @@ -121,7 +121,7 @@ @foreach($payments as $payment) - @include('layouts.front.payment-options', compact('payment', 'total', 'shipment')) + @include('layouts.front.payment-options', compact('payment', 'total', 'shipment_object_id')) @endforeach