hishamhadraoui / cart
dev-master
2021-05-14 15:40 UTC
This package is auto-updated.
Last update: 2024-09-29 15:18:03 UTC
README
这是一个非常简单的PHP购物车库。购物车数据可以保存在PHP会话或浏览器Cookie中。
用法
配置
$cart = new Cart([数组 $options]);
选项
// 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, ]);
添加项目
将项目添加到购物车。
布尔值 $cart->add(字符串 $id[, 整数 $quantity][, 数组 $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', ]);
更新项目
更新项目的数量。如果存在具有相同ID但具有不同属性的项,则必须提供属性。
布尔值 $cart->update(字符串 $id, 整数 $quantity[, 数组 $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', ]);
删除项目
删除项目。删除指定项目时必须提供属性,否则将删除购物车中具有相同ID的所有项目。
布尔值 $cart->remove(字符串 $id[, 数组 $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', ]);
获取项目
获取存储在购物车中的多维数组的项目。
数组 $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 />'; } }
检查购物车是否为空
检查购物车是否为空。
布尔值 $cart->isEmpty();
if ($cart->isEmpty()) { echo 'There is nothing in the basket.'; }
获取项目总数
获取购物车中项目的总数。
整数 $cart->getTotaltem();
echo 'There are '.$cart->getTotalItem().' items in the cart.';
获取总量
获取购物车中数量的总和。
整数 $cart->getTotalQuantity();
echo $cart->getTotalQuantity();
获取属性总和
获取特定属性的总额。
整数 $cart->getAttributeTotal(字符串 $attribute);
echo '<h3>Total Price: $'.number_format($cart->getAttributeTotal('price'), 2, '.', ',').'</h3>';
清除购物车
清除购物车中的所有项目。
$cart->clear();
$cart->clear();
销毁购物车
销毁整个购物车会话。
$cart->destroy();
$cart->destroy();
项目存在
检查项目中是否存在项目。
布尔值 $cart->isItemExists(字符串 $id[, 数组 $attributes]);
if ($cart->isItemExists('1001')) { echo 'This item already added to cart.'; }