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
673f2b43
Commit
673f2b43
authored
May 07, 2020
by
Artem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix bugs in update qty in cart method
parent
2b18faab
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
45 additions
and
4 deletions
+45
-4
Cart.php
app/Cart.php
+34
-1
CartController.php
app/Http/Controllers/CartController.php
+3
-0
cart.js
public/js/cart.js
+6
-1
cart.blade.php
resources/views/cart.blade.php
+1
-1
welcome.blade.php
resources/views/welcome.blade.php
+1
-1
No files found.
app/Cart.php
View file @
673f2b43
...
@@ -13,6 +13,26 @@ class Cart /*extends Model*/
...
@@ -13,6 +13,26 @@ class Cart /*extends Model*/
$this
->
total_price
=
0
;
$this
->
total_price
=
0
;
}
}
public
function
updateTotalQty
()
{
$result
=
0
;
foreach
(
$this
->
items
as
$item
)
{
$result
+=
$item
[
'qty'
];
}
$this
->
total_qty
=
$result
;
}
public
function
updateTotalPrice
()
{
$result
=
0
;
foreach
(
$this
->
items
as
$item
)
{
$result
+=
$item
[
'qty'
]
*
$item
[
'item'
]
->
price
;
}
$this
->
total_price
=
$result
;
}
public
function
addItem
(
$product
,
$id
)
public
function
addItem
(
$product
,
$id
)
{
{
if
(
array_key_exists
(
$id
,
$this
->
items
))
{
if
(
array_key_exists
(
$id
,
$this
->
items
))
{
...
@@ -31,7 +51,20 @@ class Cart /*extends Model*/
...
@@ -31,7 +51,20 @@ class Cart /*extends Model*/
public
function
updateQty
(
$products_qty
)
public
function
updateQty
(
$products_qty
)
{
{
foreach
(
$products_qty
as
$id
=>
$qty
)
{
foreach
(
$products_qty
as
$id
=>
$qty
)
{
$this
->
items
[
$id
][
'qty'
]
=
$qty
;
if
(
!
$qty
)
{
$this
->
removeItem
(
$id
);
}
else
{
$this
->
items
[
$id
][
'qty'
]
=
(
int
)
$qty
;
$this
->
updateTotalQty
();
$this
->
updateTotalPrice
();
}
}
}
}
}
public
function
removeItem
(
$id
)
{
unset
(
$this
->
items
[
$id
]);
$this
->
updateTotalQty
();
$this
->
updateTotalPrice
();
}
}
}
app/Http/Controllers/CartController.php
View file @
673f2b43
...
@@ -12,6 +12,9 @@ class CartController extends Controller
...
@@ -12,6 +12,9 @@ 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'
);
...
...
public/js/cart.js
View file @
673f2b43
...
@@ -16,7 +16,7 @@ function setUpdateCartQtyEventListener() {
...
@@ -16,7 +16,7 @@ function setUpdateCartQtyEventListener() {
productsQty
:
JSON
.
stringify
(
productsQty
)
productsQty
:
JSON
.
stringify
(
productsQty
)
})
})
.
then
(
function
(
response
)
{
.
then
(
function
(
response
)
{
document
.
location
.
reload
(
true
);
;
document
.
location
.
reload
(
true
);
})
})
.
catch
(
function
(
error
)
{
.
catch
(
function
(
error
)
{
console
.
log
(
error
);
console
.
log
(
error
);
...
@@ -24,4 +24,9 @@ function setUpdateCartQtyEventListener() {
...
@@ -24,4 +24,9 @@ function setUpdateCartQtyEventListener() {
});
});
}
}
function
setRemoveItemEventListener
()
{
}
setUpdateCartQtyEventListener
();
setUpdateCartQtyEventListener
();
setRemoveItemEventListener
();
resources/views/cart.blade.php
View file @
673f2b43
...
@@ -16,7 +16,7 @@
...
@@ -16,7 +16,7 @@
</tr>
</tr>
</thead>
</thead>
<tbody>
<tbody
class=""
>
@foreach(
$products
as
$product
)
@foreach(
$products
as
$product
)
<tr>
<tr>
<td>
<td>
...
...
resources/views/welcome.blade.php
View file @
673f2b43
...
@@ -20,7 +20,7 @@
...
@@ -20,7 +20,7 @@
<p class="
product__short
-
text
">
{
{$product->description_short}
}
</p>
<p class="
product__short
-
text
">
{
{$product->description_short}
}
</p>
<p class="
product__price
">
{
{$product->price}
}
EUR</p>
<p class="
product__price
">
{
{$product->price}
}
EUR</p>
<button class="
js
-
btn
-
add
-
to
-
basket
btn
btn
-
info
"
<button class="
js
-
btn
-
add
-
to
-
basket
btn
btn
-
info
text
-
white
"
data-action="
{{
route
(
'cart.addToCart'
,
$product
)
}}
"
data-action="
{{
route
(
'cart.addToCart'
,
$product
)
}}
"
>Add to basket</button>
>Add to basket</button>
</div>
</div>
...
...
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