Building a shopping cart in laravel

E-commerce is a huge business on the web. An integral part of most e-commerce sites is the
use of a shopping cart system. This recipe will walk through how to use Laravel sessions to
store items for sales and build a shopping cart.

Getting ready

For this recipe, we need a standard installation of Laravel, as well as a properly set up and
configured MySQL database.

How to do it…

To complete this recipe, follow these given steps:
1. In our database, create a table and add some data with this SQL code:
CREATE TABLE items (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
name varchar(255) DEFAULT NULL,
description text,
price int(11) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB;
INSERT INTO items VALUES (‘1’, ‘Lamp’, ‘This is a Lamp.’,’14’);
INSERT INTO items VALUES (‘2’, ‘Desk’, ‘This is a Desk.’,’75’);
INSERT INTO items VALUES (‘3’, ‘Chair’, ‘This is a
Chair.’, ’22’);
INSERT INTO items VALUES (‘4’, ‘Sofa’, ‘This is a
Sofa/Couch.’, ‘144’);
INSERT INTO items VALUES (‘5’, ‘TV’, ‘This is a
Television.’, ’89’);

2. In the routes.php file, create the routes for our cart with the following code:
Route::get(‘items’, function()
{
$items = DB::table(‘items’)->get();
return View::make(‘items’)->with(‘items’, $items)
>nest(‘cart’, ‘cart’, array(‘cart_items’ =>
Session::get(‘cart’)));
});
Route::get(‘item-detail/{id}’, function($id)
{
$item = DB::table(‘items’)->find($id);
return View::make(‘item-detail’)->with(‘item’, $item)
>nest(‘cart’, ‘cart’, array(‘cart_items’ =>
Session::get(‘cart’)));
});
Route::get(‘add-item/{id}’, function($id)
{
$item = DB::table(‘items’)->find($id);
$cart = Session::get(‘cart’);
$cart[uniqid()] = array (‘id’ => $item->id, ‘name’ =>
$item >name, ‘price’ => $item->price);
Session::put(‘cart’, $cart);
return Redirect::to(‘items’);
});
Route::get(‘remove-item/{key}’, function($key)
{
$cart = Session::get(‘cart’);
unset($cart[$key]);
Session::put(‘cart’, $cart);
return Redirect::to(‘items’);
});
Route::get(’empty-cart’, function()
{
Session::forget(‘cart’);
return Redirect::to(‘items’);
});
3. In the views directory, create a file named items.php with the following code:
<!doctype html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>Item List</title>

</head>
<body>
<div>
<?php foreach ($items as $item): ?>
<p>
<a href=”item-detail/<?= $item->id ?>”>
<?= $item->name ?>
</a> —
<a href=”add-item/<?= $item->id ?>”>Add to Cart</a>
</p>
<?php endforeach; ?>
</div>
<?php $cart_session = Session::get(‘cart’) ?>
<?php if ($cart_session): ?>
<?= $cart ?>
<?php endif; ?>
</body>
</html>
4. In the views directory, create a file named item-detail.php by the given code:
<!doctype html>
<html lang=”en”>
<head>
<meta charset=”utf-8″>
<title>Item: <?= $item->name ?></title>
</head>
<body>
<div>
<h2><?= $item->name ?></h2>
<p>Price: <?= $item->price ?></p>
<p>Description: <?= $item->description ?></p>
<p>
<a href=”../add-item/<?= $item->id ?>”>Add to Cart</a>

</p>
<p><a href=”../items”>Item list</a></p>
</div>
<? if (Session::has(‘cart’)): ?>
<?= $cart ?>
<? endif; ?>
</body>
</html>
5. In the views directory, create a file named cart.php with the following code:
<div class=”cart” style=”border: 1px solid #555″>
<?php if ($cart_items): ?>
<?php $price = 0 ?>
<ul>

<?php foreach ($cart_items as $cart_item_key =>
$cart_item_value): ?>
<?php $price += $cart_item_value[‘price’]?>
<li>
<?= $cart_item_value[‘name’] ?>:
<?= $cart_item_value[‘price’] ?> (<a href=”remove-item/<?=
$cart_item_key ?>”>remove</a>)
</li>
<?php endforeach; ?>
</ul>
<p><strong>Total: </strong> <?= $price ?></p>
<?php endif; ?>
</div>
6. Now, we can go in our browser to http://{your-server}/items to view the list
of items from our database, links to their detail pages, and an option to add them to
a cart. When added to the cart, they will show at the bottom of the page.

How it works…

To begin this recipe, we need to set up a database table that will hold items that we want to
add to the cart. We’ll also add in a few test items, so we have some data to work with.
In our first route, we get all of the existing items in our table and display them. We’re also
nesting in a cart view that will show the items we already added. In that nested view, we also
send in our cart session, so the list can populate.
Our next route does something similar but it accepts only one item and displays the full
information.
The next route actually adds the items. First, we get the item from the database based on its
ID. Then we save the existing cart session to a variable, so we can manipulate it. We add the
item to the array, using php’s uniqid() function as our key. Then we put the cart array back
into the Session and redirect it.
If we want to remove an item, we first make a way to get the item’s ID and remove it from the
cart array. The other way is to just delete all the session and start over.
In our view, we’ll also notice that we are only allowing the cart list to show if there actually is
anything in the cart.

Leave a Reply

Your email address will not be published. Required fields are marked *