An easy to use more advanced shopping cart for Laravel applications.
$ composer require lachezargrigorov/laravel-shopping-cart
Laravel 5.5 and above uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider and Facade to the array in config/app.php
If you don't use auto-discovery, add the CartServiceProvider to the providers array in config/app.php
\Lachezargrigorov\Cart\CartServiceProvider::class,
If you want to use the Cart facade, add this to the aliases array in app.php:
'Cart' => \Lachezargrigorov\Cart\Facades\Cart::class,
Implement the Item interface in your product model. The Cart and Item uses getCartPrice method to calculate the totals.
use Illuminate\Database\Eloquent\Model;
use Lachezargrigorov\Cart\Iterfaces\Item;
class Product extends Model implements Item {}
Publish the package config to your local config with the publish command and configure them:
php artisan vendor:publish --provider="Lachezargrigorov\Cart\CartServiceProvider" --tag="config"
Set the item_class in local package config (cart.php)
"item_class" => \App\Product::class,
- Cart : class Lachezargrigorov\Cart\Cart
- Item : class Lachezargrigorov\Cart\Item
- Condition : abstract class Lachezargrigorov\Cart\Condition;
- ItemCondition : class Lachezargrigorov\Cart\ItemCondition extends Condition
- CartCondition : class Lachezargrigorov\Cart\CartCondition extends Condition
- Item : interface Lachezargrigorov\Cart\Interfaces\Item
- LaravelCollection : class Illuminate\Support\Collection
- Collection : class Lachezargrigorov\Cart\Collections\Collection extends LaravelCollection
- ItemAttributesCollection : class Lachezargrigorov\Cart\Collections\ItemAttributesCollection extends Collection
- ConditionAttributesCollection : class Lachezargrigorov\Cart\Collections\ConditionAttributesCollection extends Collection
- CartException : class Lachezargrigorov\Cart\Exceptions\CartException
This method add or get an Item if exist.
- $id : int - the id of the item (product) model
- return : Item
Cart::item($id); //quantity = 0 on create
For better performance models are lazy associated to the items on first '$item->model' call after init or item addition in single DB request so you don't need to add any extra data like name, price, etc.
Cart::item(1);
Cart::item(2)->addQuantity(1);
//models are not loaded yet
//models are lazy loaded here
Cart::item(1)->model;
//if item not exist already, add a new one and mark that models need to be loaded again on next "$item->model" call
Cart::item(3);
Cart::item(4);
//models are not loaded again
//models are lazy loaded here again
Cart::item(4)->model;
-return : removed Item
Cart::item($id)->remove();
- return : LaravelCollection with Items
Cart::items();
- id : int - the id of the item (product) model
- return : bool
Cart::has($id);
- return : int
Cart::count();
- ids : [] - array with ids
- return : Cart
Cart::remove($ids);
- return : Cart
Cart::empty();
- return : bool
Cart::isEmpty();
- return : LaravelCollection with keys
Cart::keys();
This will case the cart to reload the models on next model call.
- return : Cart
Cart::emptyModels();
This method add or get an CartCondition if exist.
- name : condition name
- return : CartCondition
Cart::condition($name);
This will rewrite the existing conditions.
- return : Cart
Cart::setConditionAsArray([
"name" => "all sale1",
"type" => "all sale",
"value" => "-10%",
]);
//or as multidimensional array
Cart::setConditionAsArray([
[
"name" => "all sale1",
"type" => "all sale",
"value" => "-10%",
],
[
"name" => "all sale2",
"type" => "all sale",
"value" => "+1",
],
]);
- return : LaravelCollection with CartConditions
Cart::conditions();
- name : condition name
- return : bool
Cart::hasCondition($name);
- return : int
Cart::countConditions();
- names : array - array with names
- return : Cart
Cart::removeConditions($names);
- return : Cart
Cart::emptyConditions();
- return : bool
Cart::isEmptyConditions();
- return : LaravelCollection with keys
Cart::keysOfConditions();
- return : int
Cart::totalQuantity();
- return : double
Cart::subtotalWithoutConditions();
- return : double
Cart::subtotal();
- return : double
Cart::total();
- id : int
- quantity : int
- attributes : ItemAttributesCollection
- conditions : LaravelCollection with ItemConditions
- model : Illuminate\Database\Eloquent\Model
- return : Item
Cart::item($id)->quantity(1);
- return : Item
Cart::item($id)->addQuantity(1);
- return : int
Cart::item($id)->quantity;
- return : Item
Cart::item($id)->attributes(["size" => "L", "color" => "blue"]);
- return : ItemAttributesCollection
$attributesCollection = Cart::item($id)->attributes;
$itemSize = $attributesCollection->size;
$itemColor = $attributesCollection->color;
//or
Cart::item($id)->attributes->size;
Cart::item($id)->attributes->color;
//or using LaravelCollection methods
Cart::item($id)->attributes->has("size");
Cart::item($id)->attributes->get("size");
Cart::item($id)->attributes->each(function($value, $key){
...
});
- return : Item
Cart::item($id)->emptyAttributes();
- name : string - condition name
- return : ItemCondition
Cart::item($id)->condition($name);
This will rewrite the existing conditions.
- return : Item
Cart::item(1)->setConditionAsArray([
"name" => "item sale1",
"type" => "item sale",
"value" => "-10%",
]);
//or as multidimensional array
Cart::item(1)->setConditionAsArray([
[
"name" => "item sale1",
"type" => "item sale",
"value" => "-10%",
],
[
"name" => "item sale2",
"type" => "item sale",
"value" => "+1",
],
]);
- return : LaravelCollection with ItemConditions
Cart::item($id)->conditions();
return : removed ItemCondition
Cart::item($id)->condition($name)->remove();
- name : string - condition name
- return : bool
Cart::item($id)->hasCondition($name);
- return : bool
Cart::item($id)->isEmptyConditions();
- return : Item
Cart::item($id)->emptyConditions();
- return : Illuminate\Database\Eloquent\Model
Cart::item($id)->model;
-return : double
Cart::item($id)->priceWithoutConditions();
-return : double
Cart::item($id)->priceSumWithoutConditions();
-return : double
Cart::item($id)->price();
-return : double
Cart::item($id)->priceSum();
- [] : array - quantity : int (not required), add_quantity : int (not required), attributes : array (not required), conditions : array (not required)
- return : Item
Cart::item(1)->set([
"quantity" => 1,
//"add_quantity" => 2,
"attributes" => [
"size" => "S",
],
"conditions" => [
[
"name" => "whole sale",
"type" => "all sale",
"value" => "10%",
"attributes" => ["some" => "attribute"]
], [
"name" => "item sale",
"type" => "item sale",
"value" => "-1",
]
]
]);
// equel to
Cart::item(1)->quantity(1)/*->addQuantity(2)*/->attributes(["size" => "S"])->condition('whole sale')->type("all sale")->value("10%")->attributes(["some" => "attribute"]);
Cart::item(1)->condition("item sale")->type("item sale")->value("-1");
- name : string - condition name and it's collection key are always the same
- type : string
- value : string - [+-]?[0-9]+(\.[0-9]+)?[%]? examples: +10%, 10%, 10.22% -10%, +10.11, -10.80
- attributes : ConditionAttributesCollection
This will change the key in collection too.
- name : string
- return : CartCondition | ItemCondition
// CartCondition
//this will create a new condition with name and key = "all sale"
Cart::condition("all sale");
//this will change the name and the key in collection too
Cart::condition("all sale")->name("friday sale");
//now this condition is accessible with the new key (name)
Cart::condition("friday sale");
// ItemCondition
//this will create a new item condition with name and key = "all sale"
Cart::item(1)->condition("all sale");
//this will change the name and the key in collection too
Cart::item(1)->condition("all sale")->name("friday sale");
//now this item condition is accessible with the new key
Cart::item(1)->condition("friday sale");
- return : string
// CartCondition
Cart::condition("all sale")->name;
// ItemCondition
Cart::item(1)->condition("item sale")->name;
- type : string
- return : CartCondition | ItemCondition
// CartCondition
Cart::condition("all sale")->type($type);
// ItemCondition
Cart::item(1)->condition("item sale")->type($type);
- return : string
// CartCondition
Cart::condition("all sale")->type;
// ItemCondition
Cart::item(1)->condition("item sale")->type;
- value : string
- return : CartCondition | ItemCondition
// CartCondition
Cart::condition("all sale")->value($value);
// ItemCondition
Cart::item(1)->condition("item sale")->value($value);
- return : string
// CartCondition
Cart::condition("all sale")->value;
// ItemCondition
Cart::item(1)->condition("item sale")->value;
Merge existing attributes.
- attributes : array
- return : CartCondition | ItemCondition
// CartCondition
$attributes = ["some_attribute" => "attribute"];
Cart::condition("all sale")->attributes($attributes);
// ItemCondition
Cart::item(1)->condition("item sale")->attributes($attributes);
- return : ConditionAttributesCollection
// CartCondition
Cart::condition("all sale")->attributes;
Cart::condition("all sale")->attributes->some_attribute;
// ItemCondition
Cart::item(1)->condition("item sale")->attributes;
Cart::item(1)->condition("item sale")->attributes->some_attribute;
//or useing any LaravelCollection method
Cart::item(1)->condition("item sale")->attributes->get("some_attribute");
- return : CartCondition | ItemCondition
// CartCondition
Cart::condition("all sale")->emptyAttributes();
// ItemCondition
Cart::item(1)->condition("item sale")->emptyAttributes();
- [] : array - name : string (not required), type : string (not required), value : string (not required), attributes : array (not required)
- return : CartCondition | ItemCondition
// CartCondition
Cart::condition("all sale")->set([
"name" => "sale",
"type" => "sale",
"value" => "-10%",
"attributes" => [
"size" => "M"
]
]);
// ItemCondition
Cart::item(1)->condition("all sale")->set([
"name" => "sale",
"type" => "sale",
"value" => "-10%",
"attributes" => [
"size" => "M"
]
]);
$ composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING, ISSUE_TEMPLATE, PULL_REQUEST_TEMPLATE and CODE_OF_CONDUCT for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.