asyrafhussin/laravel-shopping-cart

Laravel应用程序的购物车。

2.0.2 2019-08-29 05:26 UTC

README

Laravel应用程序的购物车。

Build Status Latest Stable Version Latest Unstable Version Scrutinizer Code Quality Code Coverage Total Downloads License

安装

$ composer require "asyrafhussin/laravel-shopping-cart:~2.0.2@dev"

或者将以下行添加到您的项目 composer.json

"require": {
    "asyrafhussin/laravel-shopping-cart": "~2.0.2@dev"
}

然后

$ composer update

完成上述步骤后,将以下行添加到 config/app.php 文件的 providers 部分

AsyrafHussin\LaravelShoppingCart\ServiceProvider::class,

并将以下行添加到 aliases 部分

'ShoppingCart'      => AsyrafHussin\LaravelShoppingCart\Facade::class,

用法

添加商品到购物车

添加一个新的商品。

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

示例

$row = ShoppingCart::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
...

更新商品

更新指定的商品。

Item ShoppingCart::update(string $rawId, int $quantity);
Item ShoppingCart::update(string $rawId, array $arrtibutes);

示例

ShoppingCart::update('8a48aa7c8e5202841ddaf767bb4d10da', ['name' => 'New item name']);
// or only update quantity
ShoppingCart::update('8a48aa7c8e5202841ddaf767bb4d10da', 5);

获取所有商品

获取所有商品。

Collection ShoppingCart::all();

示例

$items = ShoppingCart::all();

获取商品

获取指定的商品。

Item ShoppingCart::get(string $rawId);

示例

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

移除商品

通过原始ID移除指定的商品。

boolean ShoppingCart::remove(string $rawId);

示例

ShoppingCart::remove('8a48aa7c8e5202841ddaf767bb4d10da');

销毁购物车

清理购物车。

boolean ShoppingCart::destroy();
boolean ShoppingCart::clean(); // alias of destroy();

示例

ShoppingCart::destroy();// or ShoppingCart::clean();

总价

返回所有商品的总价。

int | float ShoppingCart::total(); // alias of totalPrice();
int | float ShoppingCart::totalPrice();

示例

$total = ShoppingCart::total();
// or
$total = ShoppingCart::totalPrice();

行数

返回行数。

int ShoppingCart::countRows();

示例

ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
ShoppingCart::add(37, 'Item name', 1, 100.00, ['color' => 'red', 'size' => 'M']);
ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
ShoppingCart::add(127, 'foobar', 15, 100.00, ['color' => 'green', 'size' => 'S']);
$rows = ShoppingCart::countRows(); // 2

数量

返回所有商品的数量

int ShoppingCart::count($totalItems = true);

$totalItems : 当为 false 时,将返回行数。

示例

ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
ShoppingCart::add(37, 'Item name', 1, 100.00, ['color' => 'red', 'size' => 'M']);
ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);
$count = ShoppingCart::count(); // 11 (5+1+5)

搜索商品

通过属性搜索商品。

Collection ShoppingCart::search(array $conditions);

示例

$items = ShoppingCart::search(['color' => 'red']);
$items = ShoppingCart::search(['name' => 'Item name']);
$items = ShoppingCart::search(['qty' => 10]);

检查是否为空

bool ShoppingCart::isEmpty();

指定关联模型

在将商品添加到购物车之前,指定关联模型。

Cart ShoppingCart::associate(string $modelName);

示例

ShoppingCart::associate('App\Models\Product');

ShoppingCart::add(37, 'Item name', 5, 100.00, ['color' => 'red', 'size' => 'M']);

$item = ShoppingCart::get('8a48aa7c8e5202841ddaf767bb4d10da');
$item->product->name; // $item->product is instanceof 'App\Models\Product'

集合和商品

CollectionAsyrafHussin\LaravelShoppingCart\ItemIlluminate\Support\Collection 的实例,用法参考:集合 - Laravel 文档。

AsyrafHussin\LaravelShoppingCart\Item 的属性

  • id - 您的商品项目ID。
  • name - 商品名称。
  • qty - 商品数量。
  • price - 单价。
  • total - 商品总价。
  • __raw_id - 行的唯一ID。
  • __model - 关联模型的名称。
  • ... 自定义属性。

方法

  • rawId() - 返回商品的原始ID。

事件

您可以轻松处理这些事件,例如

Event::listen('shopping_cart.adding', function($attributes, $cart){
    // code
});

PHP 扩展包开发

想知道如何从零开始构建 PHP 扩展包吗?

请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— 《PHP 扩展包实战教程 - 从入门到发布》

许可协议

MIT