【问题标题】:How to check if id exist in CodeIgniter Cart?如何检查 CodeIgniter 购物车中是否存在 id?
【发布时间】:2013-08-08 12:47:50
【问题描述】:

有没有办法检查 ID 是否存在于 CI 图表中,如果不添加新项目,则只需更新该产品的数量?

这是我目前所拥有的,但没有任何效果:(

if (isset($_POST['sendorder']))
{
$qty=$_POST['productquantity'];

$data = array(
           'id'      => $id,
               'qty'     => $qty,
               'price'   => $price,
               'name'    => $heading
            );


if (count($this->cart->contents())>0){
                    foreach ($this->cart->contents() as $item){
                        if ($item['id']==$id){
                            $data = array('rowid'=>$item['rowid'],
                            'qty'=>++$item['qty']);
                            $this->cart->update($data);

                        }
            else{
                          $this->cart->insert($data);
                            }
                    }   

}
}

【问题讨论】:

  • 在某处尝试 var_dump($this->cart); 看看里面有什么 ;)

标签: codeigniter insert shopping-cart


【解决方案1】:

试试这个

    // set $flag default value to true for inserting item to the cart session
    $insert_new = TRUE;
    $bag = $this->cart->contents();

    foreach ($bag as $item) {

        // check product id in session, if exist update the quantity
        if ( $item['id'] == '1' ) { // Set value to your variable

            $data = array('rowid'=>$item['rowid'],'qty'=>++$item['qty']);
            $this->cart->update($data);

            // set $insert_new value to False
            $insert_new = FALSE;

        }

    }

    // if $insert_new value is true, insert the item as new item in the cart
    if ($insert_new) {
        $data = array(
                'id' => 1,
                'qty' => 1,
                'price' => 40,
                'name' => 'Product item'
            );
        $this->cart->insert($data);

    }

【讨论】:

  • 您能否稍微解释一下您的更改以帮助做出更好的答案?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-21
  • 2011-04-09
  • 2021-05-15
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
相关资源
最近更新 更多