Commit 30b2976e authored by Artem's avatar Artem

add delete items from cart methods

parent 673f2b43
......@@ -12,10 +12,6 @@ class CartController extends Controller
{
public function index()
{
// testing
// Session::get('cart')->updateTotalPrice();
// endtest
if (!Session::has('cart')) {
return view('cart');
}
......@@ -37,4 +33,15 @@ class CartController extends Controller
$cart = Session::get('cart');
$cart->updateQty($products_qty);
}
public function removeFromCart(Product $product)
{
$product->removeItemFromCart();
}
public function clearCart()
{
Session::forget('cart');
return redirect('cart');
}
}
......@@ -13,7 +13,7 @@ class Product extends Model
public function path()
{
return route('product.show', $this); // this is $product
return route('product.show', $this);
}
public function editPath()
......@@ -32,4 +32,10 @@ class Product extends Model
Session::put('cart', $cart);
}
}
public function removeItemFromCart()
{
$cart = Session::get('cart');
$cart->removeItem($this->id);
}
}
......@@ -25,8 +25,30 @@ function setUpdateCartQtyEventListener() {
}
function setRemoveItemEventListener() {
document.querySelector('.js-products-in-cart').addEventListener('click', function(e) {
if (!e.target.classList.contains('js-remove-btn')) return;
const actionUrl = e.target.dataset.action;
axios.post(actionUrl)
.then(function (response) {
document.location.reload(true);
})
.catch(function (error) {
console.log(error);
alert('Sorry, error, we are just learning how to code');
});
});
}
function setClearCartEventListener() {
document.querySelector('.js-clear-cart-btn').addEventListener('click', function (e) {
const isSure = confirm('Are you sure you want to delete all items from cart?');
if (!isSure) {
e.preventDefault();
}
})
}
setUpdateCartQtyEventListener();
setRemoveItemEventListener();
setClearCartEventListener();
......@@ -12,11 +12,16 @@
<th style="width:10%">Price</th>
<th style="width:10%">Quantity</th>
<th style="width:15%" class="text-center">Subtotal</th>
<th style="width:15%" class="text-right"><button class="btn btn-danger btn-sm ml-auto">Clear cart</button></th>
<th style="width:15%" class="text-right">
<form action="{{ route('cart.clearCart') }}" method="POST">
@csrf
<button class="js-clear-cart-btn btn btn-danger btn-sm ml-auto">Clear cart</button>
</form>
</th>
</tr>
</thead>
<tbody class="">
<tbody class="js-products-in-cart">
@foreach($products as $product)
<tr>
<td>
......@@ -37,7 +42,9 @@
</td>
<td class="text-center">€ {{ $product['item']->price * $product['qty'] }}</td>
<td class="actions text-right">
<button class="btn btn-danger btn-sm">Remove</button>
<button class="js-remove-btn btn btn-danger btn-sm"
data-action=" {{ route('cart.removeFromCart', $product['item']) }}"
>Remove</button>
</td>
</tr>
@endforeach
......
......@@ -33,3 +33,5 @@ Route::get('/home', 'HomeController@index')->name('home');
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');
Route::post('/cart/remove/{product}', 'CartController@removeFromCart')->name('cart.removeFromCart');
Route::post('/cart/clear', 'CartController@clearCart')->name('cart.clearCart');
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