Downloads
Stars
Version
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,
This method add or get an Item if exist.
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();
Cart::items();
Cart::has($id);
Cart::count();
Cart::remove($ids);
Cart::empty();
Cart::isEmpty();
Cart::keys();
This will case the cart to reload the models on next model call.
Cart::emptyModels();
This method add or get an CartCondition if exist.
Cart::condition($name);
This will rewrite the existing conditions.
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",
],
]);
Cart::conditions();
Cart::hasCondition($name);
Cart::countConditions();
Cart::removeConditions($names);
Cart::emptyConditions();
Cart::isEmptyConditions();
Cart::keysOfConditions();
Cart::totalQuantity();
Cart::subtotalWithoutConditions();
Cart::subtotal();
Cart::total();
Cart::item($id)->quantity(1);
Cart::item($id)->addQuantity(1);
Cart::item($id)->quantity;
Cart::item($id)->attributes(["size" => "L", "color" => "blue"]);
$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){
...
});
Cart::item($id)->emptyAttributes();
Cart::item($id)->condition($name);
This will rewrite the existing conditions.
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",
],
]);
Cart::item($id)->conditions();
return : removed ItemCondition
Cart::item($id)->condition($name)->remove();
Cart::item($id)->hasCondition($name);
Cart::item($id)->isEmptyConditions();
Cart::item($id)->emptyConditions();
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();
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");
This will change the key in collection too.
// 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");
// CartCondition
Cart::condition("all sale")->name;
// ItemCondition
Cart::item(1)->condition("item sale")->name;
// CartCondition
Cart::condition("all sale")->type($type);
// ItemCondition
Cart::item(1)->condition("item sale")->type($type);
// CartCondition
Cart::condition("all sale")->type;
// ItemCondition
Cart::item(1)->condition("item sale")->type;
// CartCondition
Cart::condition("all sale")->value($value);
// ItemCondition
Cart::item(1)->condition("item sale")->value($value);
// CartCondition
Cart::condition("all sale")->value;
// ItemCondition
Cart::item(1)->condition("item sale")->value;
Merge existing attributes.
// CartCondition
$attributes = ["some_attribute" => "attribute"];
Cart::condition("all sale")->attributes($attributes);
// ItemCondition
Cart::item(1)->condition("item sale")->attributes($attributes);
// 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");
// CartCondition
Cart::condition("all sale")->emptyAttributes();
// ItemCondition
Cart::item(1)->condition("item sale")->emptyAttributes();
// 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.