darthsoup/shoppingcart
Composer 安装命令:
composer require darthsoup/shoppingcart
包简介
Laravel 5 Shoppingcart
关键字:
README 文档
README
An easy shoppingcart implementation for Laravel > 6. Based on the work of Gloudemans\Shoppingcart.
Features
This package for shopping carts provides these features:
- Cart items can have subitems
- Custom hash algorithms for item identifiers
- Individual taxing for single items
- Database Support
Installation
To get the latest version, simply require the library using Composer.
$ composer require darthsoup/shoppingcart
Once installed, you'll need to publish the vendor assets
$ php artisan vendor:publish --provider="DarthSoup\Cart\CartServiceProvider" --tag="config"
This will create cart.php config file.
Usage
The cart package provides you the following methods to use:
Add Item
/** * Add a Item to the cart. * * @param string|array $id Unique ID of the item|Item formatted as array|Array of items * @param string $name Name of the item * @param int $quantity Item quantity to add to the cart * @param float $price Price of one item * @param array $options Array of additional options, such as 'size' or 'color' */ // Basic form Cart::add('1', 'Product One', 1, 9.99, ['option_key' => 'option_value']); // Array form Cart::add(['id' => 'mail1000', 'name' => 'Mail Package One', 'quantity' => 5, 'price' => 4.99, 'options' => []]); // Batch method Cart::add([ ['id' => '15', 'name' => 'Hamburger', 'quantity' => 1, 'price' => 1.99], ['id' => '16', 'name' => 'Cheeseburger', 'quantity' => 1, 'price' => 2.49, 'options' => ['onion' => false]] ]);
you also can make Items by make them manually
$item = new \DarthSoup\Cart\Item('15', 'Hamburger', 1.99, ['onion' => false]); Cart::add($item);
Update one Item
Cart::update('rowId', [ 'options' => ['foo' => 'bar'] ]);
Get One Cart Item
Cart::get('rowId');
Show Cart Content
Show the content of the Cart by returning the CartCollection
Cart::content();
Empty the cart
Cart::destroy();
Remove one Item or Subitem
Cart::remove('rowId');
Total Price of all Items
Cart::total();
Item Count
Cart::count();
SubItems
This package also includes the functionality to add Subitems by adding them to an additional collection in the Item
Add SubItem
The addSubItem function works basically like add but it accepts a parent row Id at the end to add an SubItem
to the item.
$hamburger = Cart::add('15', 'Hamburger', 1, 1.99, ['onion' => false]); Cart::addSubItem('99', 'Extra Bacon', 1, 0.99, [], $hamburger->getRowId());
Remove SubItem
Just like removing normal ones, just include your subItem rowId and it will be removed from the parent
Models
A new feature is associating a model with the items in the cart. Let's say you have a Product model in your application. With the associate() method, you can tell the cart that an item in the cart, is associated to the Product model.
That way you can access your model right from the CartCollection!
Here is an example:
<?php Cart::associate(\App\Product::class)->add('15', 'Hamburger', 1, 9.99, ['extra_sauce' => true]); $content = Cart::content(); foreach ($content as $row) { echo 'You have ' . $row->quantity . ' items of ' . $row->model->name . ' with description: "' . $row->model->description . '" in your cart.'; }
Using the key model to access the model that you associated.
Exceptions
The Cart package will throw exceptions if something goes wrong. This way it's easier to debug your code using the Cart package or to handle the error based on the type of exceptions. The Cart packages can throw the following exceptions:
| Exception | Reason |
|---|---|
| InstanceException | When no instance is passed to the instance() method |
| InvalidRowIdException | When the $rowId that got passed doesn't exists in the current cart |
| InvalidQuantityException | When the quantity is outside the set limits |
| ClassNotFoundException | When an class cannot found while association |
Events
The cart also has events build in. There are five events available for you to listen for.
| Event | Fired |
|---|---|
| cart.added($item) | When a item is added |
| cart.subitem.added($item) | When a sub item is added |
| cart.updated($rowId) | When an item in the cart is updated |
| cart.removed($rowId) | When an item is removed from the cart |
| cart.destroyed() | When the cart is destroyed |
Contributions
Please use Github for reporting bugs, and making comments or suggestions. See CONTRIBUTING.md for how to contribute changes.
darthsoup/shoppingcart 适用场景与选型建议
darthsoup/shoppingcart 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.4k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「shoppingcart」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 darthsoup/shoppingcart 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 darthsoup/shoppingcart 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 darthsoup/shoppingcart 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 5.4k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-04-04