tal7aouy / cart
一个简单的PHP购物车库,适用于电子商务网站应用。
Requires
- php: ^7.4|^8.0
Requires (Dev)
- phpunit/phpunit: ^9.5
README
一个简单的PHP购物车库,适用于电子商务网站应用
用法
安装
composer require tal7aouy/cart
配置
选项
//Instantiate cart $cart= new Cart(**array** $options);
require_once __DIR__."/vendor/autoload.php"; use Tal7aouy\Cart; // Initialize Cart object $cart = new Cart([ // Can add unlimited number of item to cart 'maxItem' => 0, // Set maximum quantity allowed per item to 20 'itemMaxQte' => 20, // do not use cookie ,cart data will lost when browser is closed 'cookieState' => false, ]);
添加商品
向购物车添加商品。
$cart->add(string $id, int $quantity = 1, array $attributes = []): bool;
// Add item with ID #10 $cart->add('10'); // Add 5 item with ID #12 $cart->add('12', 5); // Add item with ID #14 with price, color, and size $cart->add('14', 1, [ 'price' => '5.99', 'color' => 'yellow', 'size' => 'SM', ]); // Item with same ID but different attributes will added as separate item in cart $cart->add('14', 1, [ 'price' => '5.99', 'color' => 'Brown', 'size' => 'M', ]);
检查商品是否存在
// has item with ID #10 $isExist = $cart->has('10'); // has item exist with attributes $isExist = $cart->has('10',[ 'price' => '5.99', 'color' => 'yellow', 'size' => 'SM', ]);
更新商品
更新商品数量。如果存在具有相同ID但属性不同的商品,则必须提供属性。
$cart->update(string $id, int $quantity = 1, array $attributes = []): bool;
// Set quantity for item #10 to 5 $cart->update('10', 5); // Set quantity for item #14 to 2 $cart->update('14' [ 'price' => '5.99', 'color' => 'Red', 'size' => 'M', ]);
移除商品
移除商品。要移除指定商品,必须提供属性,否则将移除具有相同ID的所有商品。
$cart->remove(string $id, array $attributes = []): bool;
// Remove item #10 $cart->remove('10'); // Remove item #14 with color white and size XS $cart->remove('1003', [ 'price' => '5.99', 'color' => 'White', 'size' => 'XS', ]);
获取商品
获取购物车中存储的商品的多维数组。
$cart->getItems( ): array;
// 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 />'; } }
获取单个商品
获取购物车中存储的单个商品的多维数组。
$cart->getItem(string $id, string $hash): array;
// Get first one item from the cart with id 10 $theItem = $cart->getItem('10'); // Get one item from the cart with any id and hash $theItem = $cart->getItem($item['id'], $item['hash']);
检查购物车是否为空
检查购物车是否为空。
$cart->isEmpty( ):bool;
if ($cart->isEmpty()) { echo 'There is nothing in the basket.'; }
获取商品总数
获取购物车中商品的总数。
$cart->getTotalItems( ): int;
echo 'There are '.$cart->getTotalItems().' items in the cart.';
获取总数量
获取购物车中数量的总数。
$cart->getTotalQuantity( ): int;
echo $cart->getTotalQuantity();
获取属性总数
获取特定属性的总和。
$cart->getTotalAttribute( string $attribute ): float;
echo '<h3>Total Price: $'.number_format($cart->getTotalAttribute('price'), 2, '.', ',').'</h3>';
清空购物车
清空购物车中的所有商品。
$cart->clear( ):void;
$cart->clear();
销毁购物车
销毁整个购物车会话。
$cart->destroy( );
$cart->destroy();
商品是否存在
检查商品是否存在于购物车中。
bool $cart->isItemExists( string $id[, array $attributes] );
if ($cart->isItemExists('1001')) { echo 'This item already added to cart.'; }