codeigniter update quantity not working

codeigniter cart discount
codeigniter shopping cart session
count cart items in codeigniter
how to update cart in codeigniter

quantity not update only If I add an item with the exact same options more than once to my cart, it replaces instead of increasing the qty of the existing item This is cart view code

`<table class="table table-bordered table-hover">
<thead ><!-- Table head -->
<tr>
    <th class="active">Sl</th>
    <th class="active col-sm-4">Product</th>
    <th class="active col-sm-2">Real Price</th>
    <th class="active ">Qty</th>
    <th class="active ">Disc Price</th>
    <th class="active">Total</th>
    <th class="active">Action</th>

</tr>
</thead><!-- / Table head -->
<tbody><!-- / Table body -->
<?php $cart = $this->cart->contents() ;

?>
<?php $counter =1 ; ?>
<?php if (!empty($cart)): foreach ($cart as $item) : ?>

    <tr class="custom-tr">
        <td class="vertical-td">
            <?php echo  $counter ?>
        </td>
        <td class="vertical-td"><?php echo $item['name'] ?></td>
        <td class="vertical-td"><?php echo $item['pkprice'] ?></td>
        <td class="vertical-td">

            <input  type="text" name="qty" style="width: 50px" value="<?php echo $item['qty'] ?>" onblur ="order(this);" id="<?php echo 'qty'.$item['rowid'] ?>" class="form-control">

        </td>
        <td>


            <div class="input-group">
                    <span class="input-group-addon">
                      <input type="checkbox" id="<?php echo 'opt'.$item['rowid'] ?>" onclick="return price_checkbox(this)" name="custom_price"
                             <?php echo $item['price_option'] == 'custom_price' ? 'checked':'' ?>
                             data-placement="top" data-toggle="tooltip" data-original-title="Custom Price">
                    </span>
                <input  type="text" name="price" value="<?php echo $item['price'] ?>"  onblur ="order(this);" id="<?php echo 'pri'.$item['rowid'] ?>" class="form-control"
                        <?php echo $item['price_option'] == 'custom_price' ? '':'disabled' ?> >
            </div>


            <input type="hidden" name="product_code" value="<?php echo $item['id']  ?>" id="<?php echo 'code'.$item['rowid'] ?>">
        </td>
        <td class="vertical-td"><?php echo number_format($item['subtotal'], 2, '.', ',')  ?></td>

        <td class="vertical-td">
            <?php echo btn_delete('admin/order/delete_cart_item/' . $item['rowid']); ?>
        </td>

    </tr>


    <?php
    $counter++;
endforeach;
    ?><!--get all sub category if not this empty-->





<?php else : ?> <!--get error message if this empty-->
    <td colspan="6">
        <strong>There is no record for display</strong>
    </td><!--/ get error message if this empty-->
<?php endif; ?>
</tbody><!-- / Table body -->

`

This is Controller Code

public function add_cart_item_by_barcode(){

    $product_code = $this->input->post('barcode', true);
    $result = $this->order_model->validate_add_cart_item($product_code);



    if($result){





        $price = $this->check_product_rate($result->product_id, $qty=1);

        //product tax check
        $tax = $this->product_tax_calculate($result->tax_id, $qty=1, $price);

        $data = array(
            'id' => $result->product_code,
            'qty' => 1,
            'price' => $price,
            'buying_price' => $result->buying_price,
            'name' => $result->product_name,
            'pkprice' => $result->p_price,
            'tax' => $tax,
            'price_option' => 'general'
        );
        $this->cart->update($data);
        $this->session->set_flashdata('cart_msg', 'add');
    }

    redirect('admin/order/new_order/'.$flag ='add');
}

public function check_product_rate($product_id=null, $qty=null)
{
    //tier Price check
    $tire_price = $this->order_model->get_tire_price($product_id, $qty);

    if($tire_price)
    {
        return $price = $tire_price->tier_price ;
    }

    //special offer check
    $this->tbl_special_offer('special_offer_id');
    $offer_price = $this->global_model->get_by(array("product_id"=>$product_id), true);

    if(!empty($offer_price)) {
        $today = strtotime(date('Y-m-d'));
        $start_date = strtotime($offer_price->start_date);
        $end_date = strtotime($offer_price->end_date);
        if (($today >= $start_date) && ($today <= $end_date)) {
            return $price = $offer_price->offer_price;
        }
    }

    //return regular rate
    $this->tbl_product_price('product_price_id');
    $general_price = $this->global_model->get_by(array("product_id"=>$product_id), true);
    return $product_price = $general_price->selling_price;

}

/*** Product tax calculation ***/
public function product_tax_calculate($tax_id, $qty ,$price)
{
    $this->tbl_tax('tax_id');
    $tax = $this->global_model->get_by(array('tax_id'=>$tax_id), true);

    //1 = tax in %
    //2 = Fixed tax Rate

    if($tax){
        if($tax->tax_type == 1)
        {
            $subtotal = $price * $qty;
            $product_tax = $tax->tax_rate * ($subtotal / 100);

            //return $result = round($product_tax, 2);
            return $result = $product_tax;

        }else
        {

            //$product_tax = $tax->tax_rate * $qty;
            $product_tax = $tax->tax_rate * $qty;
            return $result = $product_tax;

        }
    }
}

/*** Update Product Cart ***/
public function update_cart_item()
{
    $rowid = $this->input->post('rowid');
    $qty = $this->input->post('qty');
    $product_price = $this->input->post('price');
    $product_code = $this->input->post('product_code');
    $custom_price = $this->input->post('custom_price');


    if($qty !=0 )
    {
        //tbl product
        $this->tbl_product('product_id');
        $result = $this->global_model->get_by(array('product_code'=> $product_code ), true);

        //product Inventory Check
        $this->tbl_inventory('inventory_id');
        $product_inventory = $this->global_model->get_by(array('product_id'=> $result->product_id ), true);

        if($qty > $product_inventory->product_quantity)
        {
            $type = 'error';
            $message = 'Sorry! This product has not enough stock.';
            set_message($type, $message);
            echo 'false';
            return;
        }


        if($custom_price == "on")
        {
               $price = $product_price;
               $price_option = 'custom_price';

        }
        else
        {
            //product price check
            $price = $this->check_product_rate($result->product_id, $qty);
            $price_option = 'general';
        }


        //product tax check
        $tax = $this->product_tax_calculate($result->tax_id, $qty, $price);



        $data = array(
            'rowid' => $rowid,
            'qty' => $qty,
            'price' => $price,
            'tax'   => $tax,
            'price_option' => $price_option

        );
    }else
    {
        $data = array(
            'rowid' => $rowid,
            'qty' => $qty,
        );
    }

    $this->cart->update($data);

    if($this->input->post('ajax') != '1'){
        redirect('admin/order/new_order'); // If javascript is not enabled, reload the page with new data
    }else{
        echo 'true'; // If javascript is enabled, return true, so the cart gets updated
    }
}
/*** Show cart ***/
function show_cart(){
    $this->load->view('admin/order/cart/cart');
}
/*** cart Summery ***/
function show_cart_summary(){
    $this->load->view('admin/order/cart/cart_summary');
}


/*** Delete Cart Item ***/
public function delete_cart_item($id)
{
    $data = array(
        'rowid' => $id,
        'qty' => 0,
    );
    $this->cart->update($data);
    $this->session->set_flashdata('cart_msg', 'delete');
    redirect('admin/order/new_order/'.$flag ='delete');
}

This is working fine when add product or change its quantity all perfect working but If I add an item with the exact same options more than once to my cart, it replaces instead of increasing the qty of the existing item

Have you tried this?

<?php foreach ($this->cart->contents() as $items): ?>

<?php echo form_hidden($counter.'[rowid]', $items['rowid']); ?> // write this

and then this

<td><?php echo form_input(array('name' => $counter.'[qty]', 'value' => $items['qty'], 'maxlength' => '3', 'size' => '5')); ?></td>

instead of-

<?php echo form_input(array('name' =>'rowid1[]', 'type'=>'text', 'value' => $items['rowid'], 'maxlength' => '3', 'size' => '5')); ?>

I hope this works

[ SOLVED ] Update Price to new currency (Cart library), I had solved this problem. My code is indeed working, but the update class in CI Cart library does not update the price. It updates only the quantity  Insert, update and delete records with simple method chains of active record; Submits the user input in a secure way using parameters; Allows you to work with multiple database engines such as MySQL, SQL Server, etc. without rewriting the application code ; CodeIgniter uses drivers specific for each database engine in the background.

Check what query is generated when update query is fire, i think any signal quote is added in your query. for check the last database query use this function:- print_r($this->db->last_query());

Shopping Cart Class, Benchmarking Class · Caching Driver · Calendaring Class; Shopping Cart Class; Config The Cart library is DEPRECATED and should not be used. an Item to The Cart; Adding Multiple Items to The Cart; Displaying the Cart; Updating The Cart The first four array indexes above (id, qty, price, and name) are required. VIEW FILE: update_view.php. In this, we fetched all the names from data base and showed them in links. As user clicks on a particular name, its details appears in form on the right side with update button.

After a lot of struggled i get my code correctly here is the correct code of insert and update

function add_cart_item_by_barcode(){

$product_code = $this->input->post('barcode', true);
$result = $this->order_model->validate_add_cart_item($product_code);

$rowid = $this->input->post('rowid');
$cart = $this->cart->contents();

foreach ($cart as $cart) {

       if($product_code == $cart['id']){

            $rowid=$cart['rowid'];
            $qty=$cart['qty'];


                    $data=array(
                    'rowid'=>$rowid,
                    'qty'=>$qty+1
                    );

        $data=$this->cart->update($data);
        $this->session->set_flashdata('cart_msg', 'add');
        redirect('admin/order/new_order/'.$flag ='add');

                    }
                }
if ($result) {  

// update rate
        $price = $this->check_product_rate($result->product_id, $qty=1);

        //product tax check
        $tax = $this->product_tax_calculate($result->tax_id, $qty=1, $price);

        $data = array(
            'id' => $result->product_code,
            'qty' => $qty,
            'price' => $price,
            'buying_price' => $result->buying_price,
            'name' => $result->product_name,
            'pkprice' => $result->p_price,
            'tax' => $tax,
            'price_option' => 'general'
        );
        $this->cart->insert($data);
        $this->session->set_flashdata('cart_msg', 'add');
}   

redirect('admin/order/new_order/'.$flag ='add');
                      }

Shopping Cart Class : CodeIgniter User Guide, allowing the user to update the quantity or remove items from the cart. Please note that the Cart Class ONLY provides the core "cart" functionality. It does not  CodeIgniter does not require that each database table be its own class file. It instead provides a more simplified interface. Beyond simplicity, a major benefit to using the Query Builder features is that it allows you to create database independent applications, since the query syntax is generated by each database adapter.

CodeIgniter Shopping Cart Sample, It does not provide shipping, credit card authorization, or other processing components. To start with first you need to initialize the class by loading cart library. $data = array( 'rowid' => $rowid, 'qty' => 0 ); // Update cart data, after cancel. If you are serious about Shopping cart. You need to learn how to create a shopping cart using Codeigniter and Ajax. Why? Well, because every e-commerce website there are a lots of images need to load at the same time. if you do not use Ajax, you are in troubles.

(pt107) CodeIgniter, update cart items and display cart totals , In this video we add an update form to our cart page that lets us update individual item Duration: 8:30 Posted: Dec 10, 2018 Like any other framework, we need to interact with the database very often and CodeIgniter makes this job easy for us. It provides rich set of functionalities to interact with database. In this section, we will understand how the CRUD (Create, Read, Update, Delete) functions work with CodeIgniter.

How to fix: Codeigniter not adding products to the cart – Papertank, Now, you'll want to update the config.php file, usually found in To fix this problem, you'll need to overwrite the default Shopping Cart class – CI_Cart – so that Does the $items array contain an id, quantity, price, and name? The primary documentation for CodeIgniter is its User Guide. The User Guide contains an introduction, tutorial, a number of "how to" guides, and then reference documentation for the components that make up the framework. CodeIgniter 4 is the upcoming version of the framework, licensed under the MIT License. CodeIgniter 3 is the current version

Comments
  • do you get some errors?
  • no error getting just product add again and qty still 1
  • do you just inserting data? $this->cart->insert($data);
  • yes all code working fine $this->cart->insert($data); but qty not increased when add product again by barcode
  • you should use update, but you're using insert instead!
  • when i add product to cart then product add successfully and its qty 1 but when i add same product again then qty not increased its still 1
  • I think the problem is with form_input(), can you write it in normal html or change normal html inputs to form inputs?
  • i update the code now please solve the problem i tried near one week but not success
  • have you tried running it again? after code change?
  • foreach($this->cart->contents() as $item){ if($item['id'] == 'your_product_id'){ $item['qty'] = 3; $this->cart->update($item); } }
  • SELECT tbl_product.*, tbl_inventory.product_quantity, tbl_inventory.notify_quantity FROM (tbl_product) LEFT JOIN tbl_inventory ON tbl_inventory.product_id = tbl_product.product_id WHERE tbl_product.status = 1 ORDER BY tbl_product.product_id DESC This is generated
  • hello shoaib, i am just want to look what query is generated when you fire your update query.
  • in my public function add_cart_item_by_barcode there is no update query i never understand that how i am use it if item already in cart how to update its quantity in public function add_cart_item_by_barcode all code is working fine but no any idea that how make update query in public function add_cart_item_by_barcode.