Commit 30b2976e authored by Artem's avatar Artem

add delete items from cart methods

parent 673f2b43
...@@ -12,10 +12,6 @@ class CartController extends Controller ...@@ -12,10 +12,6 @@ class CartController extends Controller
{ {
public function index() public function index()
{ {
// testing
// Session::get('cart')->updateTotalPrice();
// endtest
if (!Session::has('cart')) { if (!Session::has('cart')) {
return view('cart'); return view('cart');
} }
...@@ -37,4 +33,15 @@ class CartController extends Controller ...@@ -37,4 +33,15 @@ class CartController extends Controller
$cart = Session::get('cart'); $cart = Session::get('cart');
$cart->updateQty($products_qty); $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 ...@@ -13,7 +13,7 @@ class Product extends Model
public function path() public function path()
{ {
return route('product.show', $this); // this is $product return route('product.show', $this);
} }
public function editPath() public function editPath()
...@@ -32,4 +32,10 @@ class Product extends Model ...@@ -32,4 +32,10 @@ class Product extends Model
Session::put('cart', $cart); Session::put('cart', $cart);
} }
} }
public function removeItemFromCart()
{
$cart = Session::get('cart');
$cart->removeItem($this->id);
}
} }
...@@ -25,8 +25,30 @@ function setUpdateCartQtyEventListener() { ...@@ -25,8 +25,30 @@ function setUpdateCartQtyEventListener() {
} }
function setRemoveItemEventListener() { 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(); setUpdateCartQtyEventListener();
setRemoveItemEventListener(); setRemoveItemEventListener();
setClearCartEventListener();
...@@ -12,11 +12,16 @@ ...@@ -12,11 +12,16 @@
<th style="width:10%">Price</th> <th style="width:10%">Price</th>
<th style="width:10%">Quantity</th> <th style="width:10%">Quantity</th>
<th style="width:15%" class="text-center">Subtotal</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> </tr>
</thead> </thead>
<tbody class=""> <tbody class="js-products-in-cart">
@foreach($products as $product) @foreach($products as $product)
<tr> <tr>
<td> <td>
...@@ -37,7 +42,9 @@ ...@@ -37,7 +42,9 @@
</td> </td>
<td class="text-center">€ {{ $product['item']->price * $product['qty'] }}</td> <td class="text-center">€ {{ $product['item']->price * $product['qty'] }}</td>
<td class="actions text-right"> <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> </td>
</tr> </tr>
@endforeach @endforeach
......
...@@ -33,3 +33,5 @@ Route::get('/home', 'HomeController@index')->name('home'); ...@@ -33,3 +33,5 @@ Route::get('/home', 'HomeController@index')->name('home');
Route::get('/cart', 'CartController@index')->name('cart.index'); Route::get('/cart', 'CartController@index')->name('cart.index');
Route::post('/cart/add/{product}', 'CartController@addToCart')->name('cart.addToCart'); Route::post('/cart/add/{product}', 'CartController@addToCart')->name('cart.addToCart');
Route::post('/cart/update', 'CartController@updateQtyInCart')->name('cart.updateQtyInCart'); 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