咱們秉承得益開源社區,也奉獻開源社區的原則,咱們會陸續將正在線上使用的穩定包提交到 github 上,同時在後續的開源產品中,也會用到,你們能夠放心使用。php
購物車在電商場景中基本是必須的一個模塊,咱們基於 overtrue/laravel-shopping-cart 進行擴展開發。laravel
BTW: github 上已經有很是多和優秀的輪子,可是在實際應用場景中,會遇到不知足需求的狀況,這個時候就須要改造下。另外貌似又見 overtrue 大神的輪子,主要 overtrue 的輪子很優秀,並且符合國人使用習慣。
主要實現瞭如下擴展:git
包地址: laravel-shopping-cart
composer require ibrand/laravel-shopping-cart:~1.0 -vvv
php artisan vendor:publish --provider="iBrand\Shoppingcart\ServiceProvider"
低於 Laravel5.5 版本github
config/app.php
文件中 'providers' 添加app
iBrand\Shoppingcart\ServiceProvider::class
config/app.php
文件中 'aliases' 添加composer
'Cart'=> iBrand\Shoppingcart\Facade::class
You can change data Storage in config/ibrand/cart.php
file.ide
'storage' => \iBrand\Shoppingcart\Storage\DatabaseStorage::class, 'storage' => \iBrand\Shoppingcart\Storage\SessionStorage::class,
If you use Database Storage, you need to execute php artisan migrate
ui
Add a new item.spa
Item | null Cart::add( string | int $id, string $name, int $quantity, int | float $price [, array $attributes = []] );
example:code
$row = Cart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']); // Item: // id => 37 // name => 'Item name' // qty => 5 // price => 100.00 // color => 'red' // size => 'M' // total => 500.00 // __raw_id => '8a48aa7c8e5202841ddaf767bb4d10da' $rawId = $row->rawId();// get __raw_id $row->qty; // 5 ...
Update the specified item.
Item Cart::update(string $rawId, int $quantity); Item Cart::update(string $rawId, array $arrtibutes);
example:
Cart::update('8a48aa7c8e5202841ddaf767bb4d10da', ['name' => 'New item name'); // or only update quantity Cart::update('8a48aa7c8e5202841ddaf767bb4d10da', 5);
Get all the items.
Collection Cart::all();
example:
$items = Cart::all();
Get the specified item.
Item Cart::get(string $rawId);
example:
$item = Cart::get('8a48aa7c8e5202841ddaf767bb4d10da');
Remove the specified item by raw ID.
boolean Cart::remove(string $rawId);
example:
Cart::remove('8a48aa7c8e5202841ddaf767bb4d10da');
Clean Shopping Cart.
boolean Cart::destroy(); boolean Cart::clean(); // alias of destroy();
example:
Cart::destroy();// or Cart::clean();
READ MORE: ibrandcc/laravel-shopping-cart
歡迎你們 star 和提交 issue :)