Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in / Register
Toggle navigation
T
test-webshop
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Artem
test-webshop
Commits
30b2976e
Commit
30b2976e
authored
May 07, 2020
by
Artem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add delete items from cart methods
parent
673f2b43
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
52 additions
and
8 deletions
+52
-8
CartController.php
app/Http/Controllers/CartController.php
+11
-4
Product.php
app/Product.php
+7
-1
cart.js
public/js/cart.js
+22
-0
cart.blade.php
resources/views/cart.blade.php
+10
-3
web.php
routes/web.php
+2
-0
No files found.
app/Http/Controllers/CartController.php
View file @
30b2976e
...
@@ -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'
);
}
}
}
app/Product.php
View file @
30b2976e
...
@@ -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
);
}
}
}
public/js/cart.js
View file @
30b2976e
...
@@ -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
();
resources/views/cart.blade.php
View file @
30b2976e
...
@@ -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
...
...
routes/web.php
View file @
30b2976e
...
@@ -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'
);
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment