简体   繁体   English

无法更新Yii中购物车模块中的项目数量

[英]Unable to update Item quantity in cart module in Yii

I am implementing cart module in Yii, I am able to save everything in session array but each time same product is added the quantity get increment but again got set to 1. 我在Yii中实现购物车模块,我能够将所有内容保存在会话数组中,但是每次添加相同的产品时,数量都会增加,但再次设置为1。

here is my function code snippet 这是我的功能代码片段

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();

}

Seeking help thanks in advance. 预先寻求帮助,谢谢。

You are not updating the SESSION CART where there is found , 您不会更新找到的SESSION CART
Also Your code it just a bit mess( in the sense of optamization). 另外,您的代码有点混乱(在优化意义上)。

try this. 尝试这个。

 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();

    }

You are not accessing the global variable $found . 您没有访问全局变量$found

Here is the modified code: 这是修改后的代码:

    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();

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM