Codeigniter购物车 [英] Codeigniter Shopping Cart

查看:97
本文介绍了Codeigniter购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When i create a shopping cart with CI Cart Class, it add the product to the cart first 1 and then when i add that product to the cart next time it is also add 1 product not 2.

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

I want to do it the quantity in the cart is increased when someone adds an item to their cart and that exact same item is already in the cart.

推荐答案

如果你可以发布一些代码或什么数据发送到cart类将是有用的,但这应该指向正确的方向:

It would be helpful if you could post some code or what data you are sending to the cart class but this should point you in the right direction:

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中,您将更新和添加类似这样的函数

and in the cart_model you would have update and add functions that look something like this

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

这篇关于Codeigniter购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆