pinga/cart

一个简单的PHP购物车包

v0.5.1 2023-03-09 15:33 UTC

This package is auto-updated.

Last update: 2024-09-09 19:02:49 UTC


README

这是一个非常简单的PHP购物车库。购物车数据可以保存在PHP会话或浏览器cookie中。由Taras Kondratyuk维护和扩展,基于seikan/Cart。

使用方法

配置

  1. 安装包
composer require pinga/cart
  1. 在主PHP脚本中包含
require_once '../vendor/autoload.php';
  1. 在脚本中使用
use Pinga\Cart\Cart;
$cart= new Cart( [array $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,
]);

添加项目

向购物车添加项目。

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',
]);

更新项目

更新项目的数量。如果存在具有相同ID但不同属性的项目,则必须提供属性。

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',
]);

移除项目

移除项目。要移除指定的项目,必须提供属性,否则将移除购物车中所有具有相同ID的项目。

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',
]);

获取项目

获取存储在购物车中的多维数组的项目。

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 />';
  }
}

获取单个项目

获取存储在购物车中的一个项目的多维数组。

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']);

检查购物车是否为空

检查购物车是否为空。

bool $cart->isEmpty( );

if ($cart->isEmpty()) {
  echo 'There is nothing in the basket.';
}

获取项目总数

获取购物车中项目的总数。

int $cart->getTotaltem( );

echo 'There are '.$cart->getTotalItem().' items in the cart.';

获取总数量

获取购物车中数量的总数。

int $cart->getTotalQuantity( );

echo $cart->getTotalQuantity();

获取属性总额

获取特定属性的总和。

int $cart->getAttributeTotal( string $attribute );

echo '<h3>Total Price: $'.number_format($cart->getAttributeTotal('price'), 2, '.', ',').'</h3>';

清空购物车

清空购物车中的所有项目。

$cart->clear( );

$cart->clear();

销毁购物车

销毁整个购物车会话。

$cart->destroy( );

$cart->destroy();

项目存在

检查项目是否在购物车中存在。

bool $cart->isItemExists( string $id[, array $attributes] );

if ($cart->isItemExists('1001')) {
  echo 'This item already added to cart.';
}