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
fe9f5e90
Commit
fe9f5e90
authored
May 12, 2020
by
Artem
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve cart model and controller
parent
f0d39ab7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
18 additions
and
26 deletions
+18
-26
Cart.php
app/Cart.php
+1
-6
CartController.php
app/Http/Controllers/CartController.php
+17
-3
Product.php
app/Product.php
+0
-17
No files found.
app/Cart.php
View file @
fe9f5e90
...
@@ -3,7 +3,6 @@
...
@@ -3,7 +3,6 @@
namespace
App
;
namespace
App
;
//use Illuminate\Database\Eloquent\Model;
//use Illuminate\Database\Eloquent\Model;
use
App\Product
;
class
Cart
/*extends Model*/
class
Cart
/*extends Model*/
{
{
...
@@ -34,19 +33,17 @@ class Cart /*extends Model*/
...
@@ -34,19 +33,17 @@ class Cart /*extends Model*/
$this
->
total_price
=
$result
;
$this
->
total_price
=
$result
;
}
}
public
function
addItem
(
$
product
,
$
id
)
public
function
addItem
(
$id
)
{
{
if
(
array_key_exists
(
$id
,
$this
->
items
))
{
if
(
array_key_exists
(
$id
,
$this
->
items
))
{
$this
->
items
[
$id
][
'qty'
]
++
;
$this
->
items
[
$id
][
'qty'
]
++
;
}
else
{
}
else
{
$this
->
items
[
$id
]
=
[
$this
->
items
[
$id
]
=
[
'item'
=>
$product
,
'qty'
=>
1
'qty'
=>
1
];
];
}
}
$this
->
total_qty
++
;
$this
->
total_qty
++
;
$this
->
total_price
+=
$product
->
price
;
}
}
public
function
updateQty
(
$products_qty
)
public
function
updateQty
(
$products_qty
)
...
@@ -57,7 +54,6 @@ class Cart /*extends Model*/
...
@@ -57,7 +54,6 @@ class Cart /*extends Model*/
}
else
{
}
else
{
$this
->
items
[
$id
][
'qty'
]
=
(
int
)
$qty
;
$this
->
items
[
$id
][
'qty'
]
=
(
int
)
$qty
;
$this
->
updateTotalQty
();
$this
->
updateTotalQty
();
$this
->
updateTotalPrice
();
}
}
}
}
}
}
...
@@ -66,7 +62,6 @@ class Cart /*extends Model*/
...
@@ -66,7 +62,6 @@ class Cart /*extends Model*/
{
{
unset
(
$this
->
items
[
$id
]);
unset
(
$this
->
items
[
$id
]);
$this
->
updateTotalQty
();
$this
->
updateTotalQty
();
$this
->
updateTotalPrice
();
}
}
public
function
updateCurrentItems
()
public
function
updateCurrentItems
()
...
...
app/Http/Controllers/CartController.php
View file @
fe9f5e90
...
@@ -26,9 +26,17 @@ class CartController extends Controller
...
@@ -26,9 +26,17 @@ class CartController extends Controller
public
function
addToCart
(
Product
$product
)
public
function
addToCart
(
Product
$product
)
{
{
$product
->
addItemToCart
();
if
(
Session
::
has
(
'cart'
))
{
$cart
=
Session
::
get
(
'cart'
);
$cart
=
Session
::
get
(
'cart'
);
$cart
->
addItem
(
$product
->
id
);
}
else
{
$cart
=
new
Cart
();
$cart
->
addItem
(
$product
->
id
);
Session
::
put
(
'cart'
,
$cart
);
}
return
$cart
->
total_qty
;
return
$cart
->
total_qty
;
}
}
public
function
updateQtyInCart
(
Request
$request
)
public
function
updateQtyInCart
(
Request
$request
)
...
@@ -44,7 +52,13 @@ class CartController extends Controller
...
@@ -44,7 +52,13 @@ class CartController extends Controller
public
function
removeFromCart
(
Product
$product
)
public
function
removeFromCart
(
Product
$product
)
{
{
$product
->
removeItemFromCart
();
$cart
=
Session
::
get
(
'cart'
);
$cart
->
removeItem
(
$product
->
id
);
if
(
!
$cart
->
total_qty
)
{
Session
::
forget
(
'cart'
);
}
}
}
public
function
clearCart
()
public
function
clearCart
()
...
...
app/Product.php
View file @
fe9f5e90
...
@@ -21,21 +21,4 @@ class Product extends Model
...
@@ -21,21 +21,4 @@ class Product extends Model
return
route
(
'product.edit'
,
$this
);
return
route
(
'product.edit'
,
$this
);
}
}
public
function
addItemToCart
()
{
if
(
Session
::
has
(
'cart'
))
{
$cart
=
Session
::
get
(
'cart'
);
$cart
->
addItem
(
$this
,
$this
->
id
);
}
else
{
$cart
=
new
Cart
();
$cart
->
addItem
(
$this
,
$this
->
id
);
Session
::
put
(
'cart'
,
$cart
);
}
}
public
function
removeItemFromCart
()
{
$cart
=
Session
::
get
(
'cart'
);
$cart
->
removeItem
(
$this
->
id
);
}
}
}
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