Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a #302

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
6 changes: 3 additions & 3 deletions .travis.yml
@@ -1,8 +1,8 @@
language: php

php:
- 7.1

- 7.2
sudo: false

cache:
Expand All @@ -17,4 +17,4 @@ before_script:
script:
- vendor/bin/phpunit --coverage-clover=coverage.xml
after_success:
- bash <(curl -s https://codecov.io/bash)
- bash <(curl -s https://codecov.io/bash)
42 changes: 42 additions & 0 deletions app/Http/Controllers/Front/CartController.php
Expand Up @@ -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();

Expand All @@ -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');
}
Expand All @@ -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');
Expand All @@ -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');
}
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Front/CheckoutController.php
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions app/Http/Controllers/Front/ProductController.php
Expand Up @@ -62,8 +62,7 @@ public function show(string $slug)
'product',
'images',
'productAttributes',
'category',
'combos'
'category'
));
}
}
5 changes: 5 additions & 0 deletions app/Shop/Products/Product.php
Expand Up @@ -144,4 +144,9 @@ public function brand()
{
return $this->belongsTo(Brand::class);
}

public function hasAvailableQuantity($quantity = 1)
{
return $this->quantity >= $quantity;
}
}
3 changes: 3 additions & 0 deletions resources/views/front/carts/cart.blade.php
Expand Up @@ -114,6 +114,9 @@
@else
<div class="row">
<div class="col-md-12">
<div class="box-body">
@include('layouts.errors-and-messages')
</div>
<p class="alert alert-warning">No products in cart yet. <a href="{{ route('home') }}">Shop now!</a></p>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/front/checkout.blade.php
Expand Up @@ -121,7 +121,7 @@
</thead>
<tbody>
@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
</tbody>
</table>
Expand Down