Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8461415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:52:27+00:00 2026-06-10T13:52:27+00:00

When i create a shopping cart with CI Cart Class, it add the product

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:52:28+00:00Added an answer on June 10, 2026 at 1:52 pm

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

    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;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a basic shopping cart, having an issue with the product
I want to create shopping cart without using session variable to avoid load on
I want to create quick view for a shopping cart, Problem 1: When I
I'm working with a shopping cart script that doesn't let me add php to
I'm building a shopping cart with session state. But i've a problem that when
I'm trying to develop a shopping cart for a website for a class project.
I'm trying to build a shopping cart. There's very simple class which creates a
I am working on a shopping cart assignment for that I have created a
I'm looking to re-create the Apple Store shopping cart sidebar. http://store.apple.com/us/configure/MB535LL/A?mco=MTA4MTg3NTQ It is similar
Currently when adding a product to my cart the Add action of my CartController

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.