0

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

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

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

}
}
4

1 回答 1

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

    }
于 2014-06-15T16:48:52.497 回答