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
2b18faab
Commit
2b18faab
authored
May 07, 2020
by
Artem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add update qty in cart method
parent
37b318fd
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
57 additions
and
12 deletions
+57
-12
Cart.php
app/Cart.php
+6
-1
CartController.php
app/Http/Controllers/CartController.php
+8
-1
cart.js
public/js/cart.js
+27
-0
welcome.js
public/js/welcome.js
+2
-4
cart.blade.php
resources/views/cart.blade.php
+11
-2
welcome.blade.php
resources/views/welcome.blade.php
+1
-2
web.php
routes/web.php
+2
-2
No files found.
app/Cart.php
View file @
2b18faab
...
...
@@ -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
;
}
}
}
app/Http/Controllers/CartController.php
View file @
2b18faab
...
...
@@ -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
);
}
}
public/js/cart.js
0 → 100644
View file @
2b18faab
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
();
public/js/welcome.js
View file @
2b18faab
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'
);
});
});
}
...
...
resources/views/cart.blade.php
View file @
2b18faab
...
...
@@ -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
resources/views/welcome.blade.php
View file @
2b18faab
...
...
@@ -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>
...
...
routes/web.php
View file @
2b18faab
...
...
@@ -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'
);
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