background image

添加一个商品到购物车,在这里,我把它想成是两个动作。
第一个动作:添加商品到购物车数据库里面。
第二个动作:查找购物车所有商品,并显示出来。
首先是第一个动作:
代码如下:
 <?php
/**
* 添加商品
*/
public function goodsAddAction()
{
// 添加商品使用 get 请求
$goods_id = $this->_getParam('goods_id');//商品 ID
$goods_spec = $this->_getParam('filter_name');//商品属性(颜色,尺码)
$goods_number = $this->_getParam('goods_number');//商品数量
$promote_name = $this->_getParam('promote_name', 'Default');//促销策略
//获取购物车实例
$cartB = $this->_getCart();
$cartB->goodsAdd($goods_id, $goods_spec, $goods_number, $promote_name);
//添加成功,跳转到下一步,查找购物车所有商品,并显示出来。
$this->_showMessage(Bll_Context::isError() ? Bll_Context::getError() : '添加到购物篮成功!', 
Bll_Context::getRecirect('/orderv2'), 3);
}
?>

上段代码第

15 行:

 $cartB->goodsAdd($goods_id, $goods_spec, $goods_number, $promote_name);
这就是添加商品操作函数,这里面的

$promote_name 是促销的一个参数。

这里只要知道是添加这个商品的基本信息,存入到

php 购物车数据库里即可。

其次就是第二个动作:
代码示例

:

<?php
/**
* 购物列表
*/
public function indexAction()
{
//获取购物车实例
$cartB = $this->_getCart();
//列出购物车内所有商品
$this->view->goods_list = $cartB->goodsViewList();
//获取用于显示规则消息的规则实例列表
$this->view->tips = $cartB->goodsTipRules();
//购物车内商品总数量