【问题标题】:Unable to update Item quantity in cart module in Yii无法在 Yii 的购物车模块中更新商品数量
【发布时间】:2014-12-25 23:21:11
【问题描述】:

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

这是我的功能代码sn-p

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

}

提前寻求帮助谢谢。

【问题讨论】:

  • 您是否使用任何扩展来实现购物车功能?

标签: php arrays session yii cart


【解决方案1】:

您没有更新 SESSION CART
另外,您的代码有点混乱(在优化的意义上)。

试试这个。

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

    }

【讨论】:

  • 是的,我几分钟前就知道了。谢谢这使我的代码正确。
【解决方案2】:

您没有访问全局变量$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();

        }

【讨论】:

  • 如您所见,我正在打印 new added,所以如果该 ID 不存在,它会清楚地说明 new added。我猜发现并不是伤害代码的原因。更多的发现是在功能块中声明的,因此它的范围是整个具有更新值的函数。每次添加相同的项目时,数量都是 2,无论我重复多少次,它都不会变成 3 或 4。
  • 好吧!它与 GLOBAL 无关,因为 $found 仍在范围内。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-08
  • 1970-01-01
  • 2021-12-26
相关资源
最近更新 更多