bobkingstone / pedlar-cart
A simple drop in shopping cart for laravel ecommerce sites
Requires
- php: >=5.4.0
- illuminate/support: 4.2.*
Requires (Dev)
- phpspec/phpspec: ~2.0
This package is not auto-updated.
Last update: 2022-02-01 12:38:29 UTC
README
Laravel e-commerce shopping cart package. It uses Laravel's Session class by default.
Installation
Add bobkingstone/pedlar-cart to your composer.json
"require": {
"bobkingstone/pedlar-cart": "dev-master"
}
Run composer update
composer update
Once this has completed add the service provider to the array of providers in app/config/app.php
'Bobkingstone\PedlarCart\PedlarCartServiceProvider'
Usage
The package generates a 'Cart' facade for the package automatically so there is no need to add it to the alias array.
To add an item to the cart:
$item = array(
'id => '1', //your cart id
'qty' => 2,
'price' => 200.00,
);
$CartItemIdentifier = Cart::insert($item);
To get the total number of all items in cart:
Cart::countItems();
To get an array of CartItems from cart:
Cart::all();
To access cart items values:
foreach (Cart::all() as $item)
{
echo $item->id;
echo $item->price;
echo $item->qty;
};
To get the total value of all items in cart:
Cart::totalValue();
To get a count of unique items in cart:
Cart::totalUniqueItems();
To empty the cart:
Cart::clear();
To set tax rate, you can either add it to each item:
$item = array(
'id => '1',
'qty' => 2,
'tax' => 20,
'price' => 200.00,
);
Cart::totalWithTax();
Or pass in the percentage with the cart total calculation (this will override each items predefined tax rate):
Cart::totalWithTax(20);
To update an item
$item = Cart::find('91936a0df88d531b5a770b614cd3c1ea');
$item->update('qty','3');
This will replace the existing value, to add to the quantity add the same item:
$item = array(
'id => '1',
'qty' => 2,
'price' => 200.00,
);
Cart::insert($item);
The item quantity will automatically be added.
To remove a single item from cart:
Cart::remove('91936a0df88d531b5a770b614cd3c1ea');
Exceptions
The package will throw InvalidNumberOfValuesException if one of the following required params is missing:
$requiredParams = array (
'id',
'qty',
'price'
);
It will also throw InvalidItemKeyException if an invalid update is passed to a cart item.
Notes
Using dd() to view cart contents will interrupt Laravels Session storage functions and can prevent the cart from being updated.