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
e323d301
Commit
e323d301
authored
May 17, 2020
by
Artem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve cart methods
parent
1495ab98
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
32 additions
and
28 deletions
+32
-28
CartController.php
app/Http/Controllers/CartController.php
+4
-6
cart.js
public/js/cart.js
+21
-20
cart.blade.php
resources/views/cart.blade.php
+6
-1
web.php
routes/web.php
+1
-1
No files found.
app/Http/Controllers/CartController.php
View file @
e323d301
...
...
@@ -60,14 +60,14 @@ class CartController extends Controller
public
function
getItems
()
{
if
(
!
Session
::
has
(
'cart'
))
{
return
[
'redirectLink
WhenEmpty
'
=>
route
(
'homepage'
)];
return
[
'redirectLink'
=>
route
(
'homepage'
)];
}
$cart
=
Session
::
get
(
'cart'
);
if
(
!
$cart
->
total_qty
)
{
Session
::
forget
(
'cart'
);
return
[
'redirectLink
WhenEmpty
'
=>
route
(
'homepage'
)];
return
[
'redirectLink'
=>
route
(
'homepage'
)];
}
$cart
->
updateCurrentItems
();
...
...
@@ -79,11 +79,9 @@ class CartController extends Controller
}
$response
=
[
'cart'
=>
$cart
,
'positionsAmount'
=>
$cart
->
total_qty
,
'totalPrice'
=>
$cart
->
total_price
,
'products'
=>
$cart_products
,
'update_item_url'
=>
route
(
'cart.updateQtyInCart'
),
'remove_item_url'
=>
route
(
'cart.removeFromCart'
),
'item_url'
=>
'/product/'
];
return
json_encode
(
$response
);
...
...
public/js/cart.js
View file @
e323d301
...
...
@@ -9,9 +9,9 @@ class Cart {
this
.
products
;
this
.
positionsAmount
;
this
.
totalPrice
;
this
.
updateItemUrl
;
this
.
removeItemUrl
;
this
.
itemUrl
;
this
.
updateItemUrl
=
cartElem
.
dataset
.
updateItemUrl
;
this
.
removeItemUrl
=
cartElem
.
dataset
.
removeItemUrl
;
this
.
itemUrl
=
cartElem
.
dataset
.
itemUrl
;
this
.
hasEvents
;
this
.
updateItemQty
=
this
.
updateItemQty
.
bind
(
this
);
this
.
removeItemFromCart
=
this
.
removeItemFromCart
.
bind
(
this
);
...
...
@@ -68,7 +68,7 @@ class Cart {
return
;
}
this
.
loaderElem
.
classList
.
toggle
(
'loader-show'
);
this
.
toggleLoager
(
);
axios
.
post
(
actionUrl
,
{
productId
,
...
...
@@ -80,7 +80,7 @@ class Cart {
})
.
catch
(
(
error
)
=>
{
console
.
log
(
error
);
this
.
loaderElem
.
classList
.
toggle
(
'loader-show'
);
this
.
toggleLoager
(
);
toastr
.
error
(
'Error, something went wrong'
);
});
}
...
...
@@ -89,12 +89,12 @@ class Cart {
if
(
!
event
.
target
.
classList
.
contains
(
'js-remove-btn'
))
{
return
;
}
const
actionUrl
=
event
.
target
.
dataset
.
action
;
const
productId
=
event
.
target
.
dataset
.
productId
;
this
.
loaderElem
.
classList
.
toggle
(
'loader-show'
)
;
const
{
action
,
productId
}
=
event
.
target
.
dataset
;
axios
.
post
(
actionUrl
,
{
this
.
toggleLoager
();
axios
.
post
(
action
,
{
productId
,
})
.
then
(
(
response
)
=>
{
...
...
@@ -103,7 +103,7 @@ class Cart {
})
.
catch
(
(
error
)
=>
{
console
.
log
(
error
);
this
.
loaderElem
.
classList
.
toggle
(
'loader-show'
);
this
.
toggleLoager
(
);
toastr
.
error
(
'Error, something went wrong'
);
});
}
...
...
@@ -114,6 +114,10 @@ class Cart {
this
.
hasEvents
=
true
;
}
toggleLoager
()
{
this
.
loaderElem
.
classList
.
toggle
(
'loader-show'
);
}
show
()
{
if
(
!
this
.
loaderElem
.
classList
.
contains
(
'loader-show'
))
{
this
.
loaderElem
.
classList
.
toggle
(
'loader-show'
);
...
...
@@ -121,21 +125,18 @@ class Cart {
axios
.
get
(
this
.
getItemsUrl
)
.
then
(
(
response
)
=>
{
const
redirectLink
WhenEmpty
=
response
.
data
.
redirectLinkWhenEmpty
;
if
(
redirectLink
WhenEmpty
)
{
window
.
location
.
replace
(
redirectLink
WhenEmpty
);
const
redirectLink
=
response
.
data
.
redirectLink
;
if
(
redirectLink
)
{
window
.
location
.
replace
(
redirectLink
);
return
;
}
this
.
products
=
response
.
data
.
products
;
this
.
positionsAmount
=
response
.
data
.
cart
.
total_qty
;
this
.
totalPrice
=
response
.
data
.
cart
.
total_price
;
this
.
updateItemUrl
=
response
.
data
.
update_item_url
;
this
.
removeItemUrl
=
response
.
data
.
remove_item_url
;
this
.
itemUrl
=
response
.
data
.
item_url
;
this
.
positionsAmount
=
response
.
data
.
positionsAmount
;
this
.
totalPrice
=
response
.
data
.
totalPrice
;
this
.
showProducts
();
this
.
loaderElem
.
classList
.
toggle
(
'loader-show'
);
this
.
toggleLoager
(
);
if
(
!
this
.
hasEvents
)
{
this
.
events
();
...
...
@@ -143,7 +144,7 @@ class Cart {
})
.
catch
(
(
error
)
=>
{
console
.
log
(
error
);
this
.
loaderElem
.
classList
.
toggle
(
'loader-show'
);
this
.
toggleLoager
(
);
toastr
.
error
(
'Error, something went wrong'
);
});
}
...
...
resources/views/cart.blade.php
View file @
e323d301
...
...
@@ -61,7 +61,12 @@
</
tr
>
</
thead
>
<
tbody
class
="
js
-
cart
" data-get-items-url="
{{
route
(
'cart.getItems'
)
}}
">
<
tbody
class
="
js
-
cart
"
data-get-items-url="
{{
route
(
'cart.getItems'
)
}}
"
data-update-item-url="
{{
route
(
'cart.updateQtyInCart'
)
}}
"
data-remove-item-url="
{{
route
(
'cart.removeFromCart'
)
}}
"
data-item-url="
{{
route
(
'productPath'
)
}}
"
>
{{-- @foreach(
$products
as
$product
)
<tr>
<td>
...
...
routes/web.php
View file @
e323d301
...
...
@@ -18,7 +18,7 @@ Auth::routes();
Route
::
get
(
'/'
,
'ProductController@index'
)
->
name
(
'homepage'
);
Route
::
prefix
(
'product'
)
->
group
(
function
()
{
Route
::
post
(
'/'
,
'ProductController@store'
);
Route
::
post
(
'/'
,
'ProductController@store'
)
->
name
(
'productPath'
)
;
Route
::
get
(
'/create'
,
'ProductController@create'
);
Route
::
get
(
'/{product}'
,
'ProductController@show'
)
->
name
(
'product.show'
);
Route
::
get
(
'/{product}/edit'
,
'ProductController@edit'
)
->
name
(
'product.edit'
);
...
...
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