Search code examples
codeignitercart

How to fix cart codeigniter?


CONTROLLER

In my controller I add product like this, cart is redirecting but not adding a product, how can this be fixed?

function tambah()
{
    $data_produk= array('id'          => $this->input->post('id'),
                        'nama_produk' => $this->input->post('nama_produk'),
                        'harga'       => $this->input->post('harga'),
                        'gambar'      => $this->input->post('gambar'),
                        //'seller'    => $this->input->post('seller'),
                        'qty'     => $this->input->post('qty')
                    );
    //die(print_r($data_produk));
    $this->cart->insert($data_produk);

    $cart = $this->cart->contents();

    if(!empty($cart)){
        // print_r($cart);
    }
    redirect('product');
}

VIEW ;

<div class="col-md-8">
    <div class="product_list">
        <div class="row">
        <?php foreach($shopall as $row){ ?>
            <div class="col-lg-6 col-sm-6">
                <div class="single_product_item">
                     <img src="assets/img/categori/product1.png" alt="" class="img-fluid">
                     <div class="card_area">

                     <form action="<?php echo base_url();?>cart/tambah" method="post" accept-charset="utf-8">
                         <div class="product_count_area" style="margin:-40PX -35PX 0PX 0PX;">
                             <p style="text-align:center;">Rp. <?php echo number_format($row['harga'],0,",",".");?></p>
                             <div class="product_count d-inline-block">
                                 <span class="product_count_item inumber-decrement"> <i class="ti-minus"></i></span>
                                 <input class="product_count_item input-number" name="qty" type="number" value="1" min="1" max="99">
                                  <span class="product_count_item number-increment"> <i class="ti-plus"></i></span>
                              </div>
                           </div>
                           <p><a href="<?php echo base_url('cart/detail/'.$row['id']) ?>"><?php echo $row['nama_produk'] ?></a></p>

                           <input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
                           <input type="hidden" name="nama_produk" value="<?php echo $row['nama_produk']; ?>" />
                           <input type="hidden" name="harga" value="<?php echo $row['harga']; ?>" />
                           <input type="hidden" name="gambar" value="<?php echo $row['gambar']; ?>" />
                           <div style="text-align:center; margin:-20px;" class="add_to_cart">
                           <!--<button type="submit" class="btn_3">add to cart</button>-->
                           <button type="submit" class="btn btn-sm btn-success"><i class="glyphicon glyphicon-shopping-cart"></i> Beli</button>
                       </div>
                   </form>
                   </div>
            </div>
        </div>
    <?php } ?>  
    </div>
    <div class="load_more_btn text-center">
        <a href="#" class="btn_3">Load More</a>
    </div>
  </div>
</div>

In my last project I could use cart functionality with it, but in this project cart remains empty, can you point me where my code is wrong?


Solution

  • Codeigniter Cart Using English in it array keys, (unless you modified it somehow)

    This should work - Notes: Add any extra data you may need to save under options, don't define your own array key. check here for more Codeingiter Cart

      $data_produk = array(
            'id'      => 'sku_123ABC',
            'qty'     => 1,
            'price'   => 39.95,
            'name'    => 'T-Shirt',
            'options' => array('Size' => 'L', 'Color' => 'Red')
    );
    

    instead of this

    $data_produk= array('id' => $this->input->post('id'),
                            'nama_produk' => $this->input->post('nama_produk'),
                            'harga'       => $this->input->post('harga'),
                            'gambar'      => $this->input->post('gambar'),
                            //'seller'    => $this->input->post('seller'),
                            'qty'     => $this->input->post('qty')
                        );
    

    Try to map that according to your language