【问题标题】:Codeigniter Shopping Cart [closed]Codeigniter 购物车 [关闭]
【发布时间】:2012-08-26 07:30:07
【问题描述】:

当我使用 CI Cart Class 创建购物车时,它首先将产品添加到购物车 1,然后当我下次将该产品添加到购物车时,它也会添加 1 个产品而不是 2 个。

我想这样做,当有人将商品添加到他们的购物车并且购物车中已经有完全相同的商品时,购物车中的数量会增加。

【问题讨论】:

    标签: codeigniter shopping-cart


    【解决方案1】:

    如果您可以发布一些代码或您要发送到购物车类的数据将会很有帮助,但这应该会为您指明正确的方向:

    function add_cart_item(){
    
            $data = $_POST;
    
            $id = $data['product_id'];    //get new product id
            $qty = $data['quantity'];     //get quantity if that item       
            $cart = $this->cart->contents(); //get all items in the cart
                $exists = false;             //lets say that the new item we're adding is not in the cart
                $rowid = '';
    
                foreach($cart as $item){
                    if($item['id'] == $id)     //if the item we're adding is in cart add up those two quantities
                    {
                        $exists = true;
                        $rowid = $item['rowid'];
                        $qty = $item['qty'] + $qty;
                    }       
                }
    
                if($exists)
                {
                    $this->cart_model->update_item($rowid, $qty);                   
                    echo 'true'; // For ajax calls if javascript is enabled, return true, so the cart gets updated          
                }
                else
                {
                    if($this->cart_model->add_cart_item() == TRUE)
                    {           
                        echo 'true'; // for ajax calls if javascript is enabled, return true, so the cart gets updated
                    }
                }   
    
    }   
    

    在 cart_model 中,您可以更新和添加类似这样的函数

    function update_item($rowid, $qty){     
    
            // Create an array with the products rowid's and quantities. 
            $data = array(
                 'rowid' => $rowid,
                 'qty'   => $qty
              );
    
        // Update the cart with the new information
            $this->cart->update($data);
        }
    
    
        // Add an item to the cart
        function add_cart_item(){
    
            $id = $this->input->post('product_id'); // Assign posted product_id to $id
            $qty = $this->input->post('quantity'); // Assign posted quantity to $cty
            //$img = $this->input->post('image'); // Assign posted quantity to $img
    
            $this->db->where('id', $id); // Select where id matches the posted id
            $query = $this->db->get('products', 1); // Select the products where a match is found and limit the query by 1
    
            // Check if a row has been found
            if($query->num_rows > 0){
    
                foreach ($query->result() as $row)
                {
                    $data = array(
                        'id'      => $id,
                        'qty'     => $qty,
                        'price'   => $row->price,
                        'name'    => $row->name,
                        //'options' => array('image' => $img),
                    );
    
                    $this->cart->insert($data); 
    
                    return TRUE;
                }
    
            // Nothing found! Return FALSE! 
            }else{
                return FALSE;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 1970-01-01
      • 2013-04-02
      相关资源
      最近更新 更多