Simple Cart for Laravel 4
Installation
Open your composer.json and add the next code, (*1)
{
"require": {
"laravel/framework": "4.0.*",
"unodepiera/simplecart": "dev-master"
},
"minimum-stability": "dev"
}
Update your packages with composer update or install with composer install., (*2)
Usage
Find the providers key in app/config/app.php and register the Simplecart Service Provider., (*3)
'providers' => array(
//...
'Unodepiera\Simplecart\SimplecartServiceProvider'
)
Find the aliases key in app/config/app.php., (*4)
'aliases' => array(
//...
'Simplecart' => 'Unodepiera\Simplecart\Facades\Simplecart',
)
Example Usage Simplecart
Insert simple row
$id = 19;
$qty = 20;
$price = 550;
$item = array(
'id' => 5,
'qty' => $qty,
'price' => $price,
'name' => "hair",
'medida' => "xl"
);
//add options to row
$item["options"] = array("color" => "blue", "avaliable" => "si");
//add row to cart
Simplecart::insert($item);
, (*5)
Update a product
$update = array(
'id' => 5,
'rowid' => "e4da3b7fbbce2345d7772b0674a318d5",
'qty' => 25,
'price' => $price,
'name' => "shirt",
'medida' => "xl"
);
Simplecart::update($update);
, (*6)
Remove a product by rowid
You just need to pass a rowid that there, (*7)
Simplecart::remove_item("8e296a067a37563370ded05f5a3bf3ec");
, (*8)
Get cart content
Simplecart::get_content();
, (*9)
Get total cost
Simplecart::total_cart();
, (*10)
Get total items
Simplecart::total_articles();
, (*11)
Destroy simplecart
Simplecart::destroy();
Final example usage
First visit route insert, (*12)
Route::get("insert", function(){
$id = 9;
$qty = 5;
$price = 1500;
$item = array(
'id' => $id,
'qty' => $qty,
'price' => $price,
'name' => "shirt",
'medida' => "xxl"
);
//add options to row
$item["options"] = array("color" => "orange", "avaliable" => "yes");
//add row to cart
Simplecart::insert($item);
});
, (*13)
Then create the next view and visit the route show
Route::get("show", function()
{
$cart = Simplecart::get_content();
$totalcart = Simplecart::total_cart();
$totalitems = Simplecart::total_articles();
return View::make("cart", array("cart" => $cart, "total_cart" => $totalcart, "total_items" => $totalitems));
});
, (*14)
Loop the cart and check if has options
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Simplecart for Laravel 4</title>
</head>
<body>
@if($cart)
|
Id
|
Name
|
Options
|
Price
|
Qty
|
Total price
|
@foreach($cart as $items)
{{ $items['id'] }}
|
{{ $items['name'] }}
|
@if(isset($items['options']))
@foreach($items['options'] as $key => $val)
{{ $key }}: {{ $val }}
@endforeach
@else
----
@endif
|
{{ $items['price'] }}
|
{{ $items['qty'] }}
|
{{ $items['total'] }}
|
@endforeach
| Total: {{ $total_cart }} |
Items: {{ $total_items }} |
@endif
</body>
</html>
Visit me