Commit 2b18faab authored by Artem's avatar Artem

add update qty in cart method

parent 37b318fd
......@@ -28,5 +28,10 @@ class Cart /*extends Model*/
$this->total_price += $product->price;
}
public function updateQty($products_qty)
{
foreach ($products_qty as $id => $qty) {
$this->items[$id]['qty'] = $qty;
}
}
}
......@@ -23,8 +23,15 @@ class CartController extends Controller
return view('cart', compact('cart', 'products'));
}
public function addToCart(Request $request, Product $product)
public function addToCart(Product $product)
{
$product->addItemToCart();
}
public function updateQtyInCart(Request $request)
{
$products_qty = json_decode($request->productsQty, true);
$cart = Session::get('cart');
$cart->updateQty($products_qty);
}
}
function setUpdateCartQtyEventListener() {
document.querySelector('.js-update-qty-btn').addEventListener('click', function(e) {
const actionUrl = this.dataset.action;
const qtyInputs = document.querySelectorAll('.js-qty-input');
const productsQty = {};
qtyInputs.forEach(item => {
const productId = item.dataset.product_id;
productsQty[productId] = item.value;
});
axios.post(actionUrl, {
productsQty: JSON.stringify(productsQty)
})
.then(function (response) {
document.location.reload(true);;
})
.catch(function (error) {
console.log(error);
});
});
}
setUpdateCartQtyEventListener();
function setAddToCardEventListener() {
document.querySelector('.products').addEventListener('click', function(e) {
if (!e.target.classList.contains('js-btn-add-to-basket')) return;
e.preventDefault();
const actionUrl = e.target.dataset.action;
axios.post(actionUrl, {
productId: e.target.dataset.product_id
})
axios.post(actionUrl)
.then(function (response) {
const cartAmountElem = document.querySelector('.amount-in-cart');
cartAmountElem.textContent++;
})
.catch(function (error) {
console.log(error);
alert('Sorry, error, we are just learning how to code');
});
});
}
......
......@@ -30,7 +30,10 @@
</td>
<td>€ {{ $product['item']->price }}</td>
<td>
<input type="number" class="form-control text-center" value="{{ $product['qty'] }}">
<input type="number"
class="js-qty-input form-control text-center"
data-product_id="{{ $product['item']->id }}"
value="{{ $product['qty'] }}">
</td>
<td class="text-center">€ {{ $product['item']->price * $product['qty'] }}</td>
<td class="actions text-right">
......@@ -46,7 +49,9 @@
<a href="{{ route('homepage') }}" class="btn btn-primary"> Continue Shopping</a>
</td>
<td colspan="2" class="text-right">
<button class="btn btn-warning">Update quantity</button>
<button class="js-update-qty-btn btn btn-warning"
data-action="{{ route('cart.updateQtyInCart') }}"
>Update quantity</button>
</td>
<td class="hidden-xs text-center"><strong>Total € {{ $cart->total_price }}</strong></td>
<td><button class="btn btn-success btn-block">Send order</button></td>
......@@ -61,3 +66,7 @@
</div>
</section>
@endsection
@section('script')
<script src="js/cart.js"></script>
@endsection
......@@ -21,8 +21,7 @@
<p class="product__price">{{$product->price}} EUR</p>
<button class="js-btn-add-to-basket btn btn-info"
data-product_id="{{ $product->id }}"
data-action="{{ route('product.addToCart', $product) }}"
data-action="{{ route('cart.addToCart', $product) }}"
>Add to basket</button>
</div>
</li>
......
......@@ -30,6 +30,6 @@ Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::post('/add-to-cart/{product}', 'CartController@addToCart')->name('product.addToCart');
Route::get('/cart', 'CartController@index')->name('cart.index');
Route::post('/cart/add/{product}', 'CartController@addToCart')->name('cart.addToCart');
Route::post('/cart/update', 'CartController@updateQtyInCart')->name('cart.updateQtyInCart');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment