无法更新Yii中购物车模块中的商品数量

时间:2014-12-20 12:54:30

标签: php arrays session yii cart

我在Yii中实现购物车模块,我能够保存会话数组中的所有内容,但每次添加相同的产品时数量增加但是再次设置为1.

这是我的功能代码段

public function actionAddToCart($id) {
$found = 0;
$cart = array();
$cart_item = $this->loadModel($id)->name;
$cart_price = $this->loadModel($id)->price;

$newItem = Yii::app()->session['cart'];

foreach ($newItem as $item) {
    if($id == $item['id']) {
        echo "quantity" . $item['quantity'];
        $item['quantity'] += 1;
        $found = 1;
        $newItem[$id] = array('name'=>$cart_item,'price'=>$cart_price, 'quantity'=>$item['quantity'], 'id'=>$id ) ;
        echo "Element Found=" . $found . 'Now quantity is '.$item['quantity'];
    }

}
if($found != 1) {
    $newItem[$id] = array('name'=>$cart_item,'price'=>$cart_price, 'quantity'=>1, 'id'=>$id ) ;
    Yii::app()->session['cart'] = $newItem ;
    echo "New added" ;
}
//array_push($cart, Yii::app()->session['cart'][$id] ) ;         //array('name'=>$cart_item,'price'=>$cart_price)) ;

echo json_encode(array("items"=>$newItem));
Yii::app()->end();

}

提前寻求帮助。

2 个答案:

答案 0 :(得分:1)

您没有更新找到的 SESSION CART
你的代码也只是有点乱(在optamization的意义上)。

试试这个。

 public function actionAddToCart($id) {

        $newItem = Yii::app()->session['cart'];

        if(isset($newItem[$id])){
            $newItem[$id]['quantity']++;
            echo "Element Found= 1 Now quantity is ".$newItem[$id]['quantity'];
        }
        else{
            $cartItem= $this->loadModel($id)
            $newItem[$id] = array('name'=>$cartItem->name,'price'=>$cartItem->price, 'quantity'=>1, 'id'=>$id ) ;            
        }

        Yii::app()->session['cart'] = $newItem ;
        //array_push($cart, Yii::app()->session['cart'][$id] ) ;   
        //array('name'=>$cart_item,'price'=>$cart_price)) ;

        echo json_encode(array("items"=>$newItem));
        Yii::app()->end();

    }

答案 1 :(得分:0)

您没有访问全局变量$found

以下是修改后的代码:

    public function actionAddToCart($id) {
        $found = 0;
        $cart = array();
        $cart_item = $this->loadModel($id)->name;
        $cart_price = $this->loadModel($id)->price;

        $newItem = Yii::app()->session['cart'];

        foreach ($newItem as $item) {
            if($id == $item['id']) {
                echo "quantity" . $item['quantity'];
                $item['quantity'] += 1;
                $GLOBALS['found'] = 1;
                $newItem[$id] = array('name'=>$cart_item,'price'=>$cart_price, 'quantity'=>$item['quantity'], 'id'=>$id ) ;
                echo "Element Found=" . $found . 'Now quantity is '.$item['quantity'];
            }

        }
        if($found == 0) {
            $newItem[$id] = array('name'=>$cart_item,'price'=>$cart_price, 'quantity'=>1, 'id'=>$id ) ;
            Yii::app()->session['cart'] = $newItem ;
            echo "New added" ;
        }
        //array_push($cart, Yii::app()->session['cart'][$id] ) ;         //array('name'=>$cart_item,'price'=>$cart_price)) ;

        echo json_encode(array("items"=>$newItem));
        Yii::app()->end();

        }