关于如何判断zencart购物车是否为空的问题,如果对shopping_cart类比较熟悉的话,那就好理解多了。
购物车类在includes\classes\shopping_cart.php 文件中,有兴趣的话可以去看看这个类的方法说明,里面都有详细的英文注释。而如果要判断当前的购物车是否为空的话,可以调用函数
/**
* Method to count total number of items in cart //
*
* Note this is not just the number of distinct items in the cart,
* but the number of items adjusted for the quantity of each item
* in the cart, So we have had 2 items in the cart, one with a quantity
* of 3 and the other with a quantity of 4 our total number of items
* would be 7
*
* @return total number of items in cart
*/
function count_contents() {
$this->notify('NOTIFIER_CART_COUNT_CONTENTS_START');
$total_items = 0;
if (is_array($this->contents)) {
reset($this->contents);
while (list($products_id, ) = each($this->contents)) {
$total_items += $this->get_quantity($products_id);
}
}
$this->notify('NOTIFIER_CART_COUNT_CONTENTS_END');
return $total_items;
}
count_contents() 这个函数就是获取当前购物车中所有商品的数量,同一种商品多个数量也都算到总数中。如果购物车没有商品,则该函数返回0个商品,否则就不是空的。所以可以这样来判断是否为空
if($_SESSION['cart']->count_contents() ==0){
购物车为空的
}
调用购物车对象可以直接使用$_SESSION['cart'],因为它一开始就初始化了,存放在当前session中
(责任编辑:最模板) |