pinga/cart
Composer 安装命令:
composer require pinga/cart
包简介
A simple PHP shopping cart package
README 文档
README
This is a very simple PHP cart library. Cart data can either be saved in PHP session or browser cookie. Maintained and expanded by Taras Kondratyuk and based on seikan/Cart.
Usage
Configuration
- Install the package:
composer require pinga/cart
- Include in your main PHP script:
require_once '../vendor/autoload.php';
- Use in your script:
use Pinga\Cart\Cart; $cart= new Cart( [array $options] );
Options
| Parameter | Type | Description |
|---|---|---|
| cartMaxItem | int | The maximum item can be added to cart. 0 = Unlimited |
| itemMaxQuantity | int | The maximum quantity per item can be added to cart. 0 = Unlimited |
| useCookie | bool | Use cookie to keep cart data when browser is closed. |
// Include core Cart library require_once 'class.Cart.php'; // Initialize Cart object $cart = new Cart([ // Can add unlimited number of item to cart 'cartMaxItem' => 0, // Set maximum quantity allowed per item to 99 'itemMaxQuantity' => 99, // Do not use cookie, cart data will lost when browser is closed 'useCookie' => false, ]);
Add Item
Adds an item to cart.
bool $cart->add( string $id[, int $quantity][, array $attributes] );
// Add item with ID #1001 $cart->add('1001'); // Add 5 item with ID #1002 $cart->add('1002', 5); // Add item with ID #1003 with price, color, and size $cart->add('1003', 1, [ 'price' => '5.99', 'color' => 'White', 'size' => 'XS', ]); // Item with same ID but different attributes will added as separate item in cart $cart->add('1003', 1, [ 'price' => '5.99', 'color' => 'Red', 'size' => 'M', ]);
Update Item
Updates quantity of an item. Attributes must be provides if item with same ID exists with different attributes.
bool $cart->update( string $id, int $quantity[, array $attributes] );
// Set quantity for item #1001 to 5 $cart->update('1001', 5); // Set quantity for item #1003 to 2 $cart->update('1003', 2, [ 'price' => '5.99', 'color' => 'Red', 'size' => 'M', ]);
Remove Item
Removes an item. Attributes must be provided to remove specified item, or all items with same ID will be removed from cart.
bool $cart->remove( string $id[, array $attributes] );
// Remove item #1001 $cart->remove('1001'); // Remove item #1003 with color White and size XS $cart->remove('1003', [ 'price' => '5.99', 'color' => 'White', 'size' => 'XS', ]);
Get Items
Gets a multi-dimensional array of items stored in cart.
array $cart->getItems( );
// Get all items in the cart $allItems = $cart->getItems(); foreach ($allItems as $items) { foreach ($items as $item) { echo 'ID: '.$item['id'].'<br />'; echo 'Qty: '.$item['quantity'].'<br />'; echo 'Price: '.$item['attributes']['price'].'<br />'; echo 'Size: '.$item['attributes']['size'].'<br />'; echo 'Color: '.$item['attributes']['color'].'<br />'; } }
Get Item
Gets a multi-dimensional array of one item stored in cart.
array $cart->getItem( string $id[, string $hash] );
// Get first one item from the cart with id 1001 $theItem = $cart->getItem('1001'); // Get one item from the cart with any id and hash $theItem = $cart->getItem($item['id'], $item['hash']);
Check Cart Empty
Checks if the cart is empty.
bool $cart->isEmpty( );
if ($cart->isEmpty()) { echo 'There is nothing in the basket.'; }
Get Total Item
Gets the total of items in the cart.
int $cart->getTotaltem( );
echo 'There are '.$cart->getTotalItem().' items in the cart.';
Get Total Quantity
Gets the total of quantity in the cart.
int $cart->getTotalQuantity( );
echo $cart->getTotalQuantity();
Get Attribute Total
Gets the sum of a specific attribute.
int $cart->getAttributeTotal( string $attribute );
echo '<h3>Total Price: $'.number_format($cart->getAttributeTotal('price'), 2, '.', ',').'</h3>';
Clear Cart
Clears all items in the cart.
$cart->clear( );
$cart->clear();
Destroy Cart
Destroys the entire cart session.
$cart->destroy( );
$cart->destroy();
Item Exists
Checks if an item exists in cart.
bool $cart->isItemExists( string $id[, array $attributes] );
if ($cart->isItemExists('1001')) { echo 'This item already added to cart.'; }
pinga/cart 适用场景与选型建议
pinga/cart 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「cart」 「Simple」 「shopping」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 pinga/cart 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 pinga/cart 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 pinga/cart 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel 11 and above Shopping cart
A simple library for make Lexer and Parsers to build a language
A simple library that allows transform any kind of data to native php data or whatever
A simple library that uses the hydration pattern to fill objects given different sources of data.
Admin Hub for GetCandy. A modern headless e-commerce solution for Laravel PHP framework.
Micro Active Record library in PHP, support chain calls, events, and relations.
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-09