hassansin/dbcart
Composer 安装命令:
composer require hassansin/dbcart
包简介
Shopping Cart library for Laravel 5 that uses Database instead of Sessions
README 文档
README
Shopping Cart library for Laravel 5 that uses database instead of sessions to store carts.
Features
- Cart for guest users
- Cart for logged in users
- Guest Cart is merged with User Cart when logged in
- Singleton Cart instance to avoid unnecessary database queries. But also possible to avoid signleton cart if needed.
- Built on top of Eloquent Model, so easily extendable and all eloquent methods can be used.
- Multiple instances of cart
- Schedule expired carts for deletion
Installation
-
Edit your project's composer.json file to require hassansin/DBCart.
"require": { "hassansin/dbcart": "dev-master" }
-
Then install dependecies with composer command:
composer update -
Next, add a new provider to the providers array in config/app.php:
'providers' => [ //... Hassansin\DBCart\CartServiceProvider::class, //... ],
-
Then, publish database migrations and run migration:
php artisan vendor:publish --provider="Hassansin\DBCart\CartServiceProvider" --tag=migrations php artisan migrate
Configuration
Optionally, you can publish package config file:
php artisan vendor:publish --provider="Hassansin\DBCart\CartServiceProvider" --tag=config
Now, update config/cart.php if required
Usage
Get Cart Instance:
Get the current cart instance. It returns a singleton cart instance:
$cart = app('cart'); //using app() helper
or,
$cart = App::make('cart');
alternatively, you can avoid singleton instance and use the model class to load the cart instance from database everytime:
use Hassansin\DBCart\Models\Cart; //... $cart = Cart::current();
The idea of using singleton cart is that the cart object will be available globally throughout your app (e.g. controllers/models/views/view composers etc) for a single request. Also as you manipulate cart items, $cart->item_count and $cart->total_price would get updated.
Add an Item: $cart->addItem($attributes)
$cart->addItem([ 'product_id' => 1, 'unit_price' => 10.5, 'quantity' => 1 ]);
which is equivalent to $cart->items()->create($attributes)
Get Items: $cart->items
Since $cart is eloquent model instance, you can use any of the eloquent methods to get items
$items = $cart->items // by dynamic property access $items = $cart->items()->get() $items = $cart->items()->where('quantity', '>=', 2)->get()
Update an Item: $cart->updateItem($where, $attributes)
$cart->updateItem([ 'id' => 2 ], [ 'product_id' => 1, 'unit_price' => 10.5, 'quantity' => 1 ]);
which is equivalent to $cart->items()->where($where)->first()->update($attributes)
Remove an Item: $cart->removeItem($where)
$cart->removeItem([ 'id' => 2 ]);
which is equivalent to $cart->items()->where($where)->first()->delete()
Clear Cart Items: $cart->clear()
Remove all items from the cart
$cart->clear();
Checkout cart: $cart->checkout()
This method only updates status and placed_at column values. status is set to pending
$cart->checkout();
Move item(s) between carts:
To move all items from one cart to another cart instance:
$cart = app('cart'); $wishlist = app('cart', ['name' => 'wishlist']); //move all wishlist items to cart $wishlist->moveItemsTo($cart);
To move a single item between carts:
$cart = app('cart'); $wishlist = app('cart', ['name' => 'wishlist']); //move an wishlist item to cart $item = $wishlist->items()->where(['product_id' => 1])->first(); $item->moveTo($cart);
Get Cart Attributes
$total_price = $cart->total_price; // cart total price $item_count = $cart->item_count; // cart items count $date_placed = $cart->placed_at; // returns Carbon instance
Cart Statuses
Supports several cart statuses:
active: currently adding items to the cartexpired: cart is expired, meaningful for session cartspending: checked out cartscompleted: completed carts
use Hassansin\DBCart\Models\Cart; // get carts based on their status: active/expired/pending/complete $active_carts = Cart::active()->get(); $expired_carts = Cart::expired()->get(); $pending_carts = Cart::pending()->get(); $completed_carts = Cart::completed()->get();
Working with Multiple Cart Instances
By default, cart instances are named as default. You can load other instances by providing a name:
$cart = app('cart'); // default cart, same as: app('cart', [ 'name' => 'default']; $sales_cart = app('cart', [ 'name' => 'sales']); $wishlist = app('cart', [ 'name' => 'wishlist']);
or, without singleton carts:
use Hassansin\DBCart\Models\Cart; //... $cart = Cart::current(); $sales_cart = Cart::current('sales'); $wishlist = Cart::current('wishlist');
To get carts other than default:
$pending_sales_carts = Cart::instance('sales')->pending()->get();
Delete Expired Carts:
The guest carts depend on the session lifetime. They are valid as long as the session is not expired. You can increase session lifetime in config/session.php to increase cart lifetime. When a session expires, a new cart instance will be created in database and the old one will no longer be used. Over time, these expired carts could pile-up in database.
Laravel Task Scheduler comes to the rescue. To enable scheduler just add following to the crontab in your server:
* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
That's it. The module will now check for expired carts in every hour and delete them. This won't affect the carts for loggedin users.
Other features:
Get Cart User: $cart->user
Get Item Product: $item->product
Is Cart Empty: $cart->isEmpty()
If an item exists in cart: $cart->hasItem(['id' => 10])
Expire the cart: cart->expire();
Set to completed status: $cart->complete();
Extending Cart Model
It's easy to extend DBCart. You can extend base DBCart model and add your own methods or columns. Follow these steps to extend the cart model:
-
Create a model by extending
Hassansin\DBCart\Models\Cart:namespace App; use Hassansin\DBCart\Models\Cart as BaseCart; class Cart extends BaseCart { //override or add your methods here ... public function getSubTotalAttribute(){ return $this->attributes['total_price']; } public function getGrandTotalAttribute(){ //taking discount, tax etc. into account return $this->sub_total - $this->discount; } }
-
Update
cart_modelinconfig/cart.phpwith the fully qualified class name of the extended model.'cart_model' => App\Cart::class,
-
That's it, you can now load the cart as usual:
$cart = App::make('cart');
You can also follow the above steps and create your own CartLine model by extending Hassansin\DBCart\Models\CartLine. Be sure to update config/cart.php to reflect your changes.
hassansin/dbcart 适用场景与选型建议
hassansin/dbcart 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.46k 次下载、GitHub Stars 达 25, 最近一次更新时间为 2015 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「ecommerce」 「cart」 「laravel」 「shopping-cart」 「laravel5」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hassansin/dbcart 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hassansin/dbcart 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hassansin/dbcart 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
Nevogate Payment Gateway SDK
Laravel 11 and above Shopping cart
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Admin Hub for GetCandy. A modern headless e-commerce solution for Laravel PHP framework.
统计信息
- 总下载量: 7.46k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-12-01