shortcutmediaro/yii2-cart

此包最新版本(1.0)没有提供许可证信息。

Yii2 购物车

安装: 10

依赖: 0

建议: 0

安全: 0

星标: 0

关注者: 1

分支: 47

类型:yii2-extension

1.0 2015-05-06 07:35 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:49:10 UTC


README

@todo: 升级README

使用购物车

当使用实现了两种购物车接口之一的模型时,购物车的操作非常直接。购物车对象可以在 \Yii::$app->cart 下访问,如果需要自定义,可以在配置中覆盖。

// access the cart from "cart" subcomponent
$cart = \Yii::$app->cart;

// Product is an AR model implementing CartProductInterface
$product = Product::find(1);

// add an item to the cart
$cart->add($product);

// returns the sum of all 'vat' attributes (or return values of getVat()) from all models in the cart.
$totalVat = $cart->getAttributeTotal('vat');

// clear the cart
$cart->clear();

// render the contents of the cart with default parameters
echo \yii2mod\cart\widgets\CartGrid::widget();

购物车中的项目

添加到购物车的产品/项目在保存和从购物车存储加载数据时会被序列化/反序列化。如果您使用 Active Record 模型作为产品/折扣,请确保您省略了任何不必要的引用以使序列化数据紧凑。

// get all items from the cart
$items = $cart->getItems();

// get only products
$items = $cart->getItems(Cart::ITEM_PRODUCT);

// loop through cart items
foreach ($items as $item) {
	// access any attribute/method from the model
	var_dump($item->getAttributes());

	// remove an item from the cart by its ID
	$cart->remove($item->uniqueId)
}