美文网首页
Laravel shopping cart : 电商购物车包,线

Laravel shopping cart : 电商购物车包,线

作者: iBrand_shjchen | 来源:发表于2018-01-18 16:38 被阅读146次

我们秉承得益开源社区,也奉献开源社区的原则,我们会陆续将正在线上使用的稳定包提交到 github 上,同时在后续的开源产品中,也会用到,大家可以放心使用。

购物车在电商场景中基本是必须的一个模块,我们基于 overtrue/laravel-shopping-cart 进行扩展开发。

BTW: github 上已经有非常多和优秀的轮子,但是在实际应用场景中,会遇到不满足需求的情况,这个时候就需要改造下。另外貌似又见 overtrue 大神的轮子,主要 overtrue 的轮子很优秀,而且符合国人使用习惯。

主要实现了以下扩展:

  1. 购物车数据支持 Cache 和 Database 存储
  2. Item 增加 Model 属性返回。因为购物车可能是SPU或者SKU,因此直接通过 model 属性直接返回相关对象。
  3. 支持把 Session 中的数据直接同步到 Cache 或 Database 中。
  4. 支持多 Guard. 因为在 iBrand 产品有商城购物车和导购购物车。

Installation

composer require ibrand/laravel-shopping-cart:~1.0 -vvv
php artisan vendor:publish --provider="iBrand\Shoppingcart\ServiceProvider"

低于 Laravel5.5 版本

config/app.php 文件中 'providers' 添加

iBrand\Shoppingcart\ServiceProvider::class

config/app.php 文件中 'aliases' 添加

'Cart'=> iBrand\Shoppingcart\Facade::class

Usage

Select Storage

You can change data Storage in config/ibrand/cart.php file.

'storage' => \iBrand\Shoppingcart\Storage\CacheStorage::class,
 
'storage' => \iBrand\Shoppingcart\Storage\DatabaseStorage::class,
  
'storage' => \iBrand\Shoppingcart\Storage\SessionStorage::class,

If you use Database Storage, you need to execute php artisan migrate

Add item to cart

Add a new item.

Item | null Cart::add(
                    string | int $id,
                    string $name,
                    int $quantity,
                    int | float $price
                    [, array $attributes = []]
                 );

example:

$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 item

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 items

Get all the items.

Collection Cart::all();

example:

$items = Cart::all();

Get item

Get the specified item.

Item Cart::get(string $rawId);

example:

$item = Cart::get('8a48aa7c8e5202841ddaf767bb4d10da');

Remove item

Remove the specified item by raw ID.

boolean Cart::remove(string $rawId);

example:

Cart::remove('8a48aa7c8e5202841ddaf767bb4d10da');

Destroy cart

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 :)

讨论交流

qrcode.jpg

相关文章

网友评论

      本文标题:Laravel shopping cart : 电商购物车包,线

      本文链接:https://www.haomeiwen.com/subject/dupwoxtx.html