Welcome Guest, Not a member yet? Register   Sign In
Problem with Cart Class (Same Product, Same Product Id)
#1

[eluser]Unknown[/eluser]
Hi,
i 've problem using cart class on CI, in my system i've to insert same product (include same id), it happen because sometime i've to give discount / bonus, but it should be on screen / log into system.
example:
a. T-Shirt Blue Ocean id shirt_123, qty 5pcs, price $5
b. T-Shirt Blue Ocean id shirt_123, qty 1pcs, price Free

thanks,


insting
#2

[eluser]WanWizard[/eluser]
Use the options array of the item. Together with the item ID, it generates the unique key for the cart.

So you can have multiple cart entries for the same product, as long as the options of each of them are different. Options are usually used to define properties of the product, like size or colour. In your case, you could add 'Bonus item' as extra option.
#3

[eluser]Unknown[/eluser]
may you tell me the simple script to generate the unique key?
here is my simple code
Code:
function savebarang(){
        $data = array();

        if (isset($_POST['jquery_action']) && $_POST['jquery_action']== 'simpan_orders_detail') {
            $c = parseForm($_POST,'cr_');
          
            $data_barang = array(
                'id'      => $c['id_barang'],
                'qty'     => $c['jumlah'],
                'price'   => 1,
                'name'    => $c['namabarang'],
                'jenis_penjualan' => $c['id_penjualan'],
                'options' => array('kode' => $c['kodebarang'])  
            );              
          
            $this->cart->insert($data_barang,'item_order');
            //pre($this->cart->contents());
        }
        echo "[removed]";
        echo "$(function() {";
        echo "resetForm();";
        echo" });";
        echo "[removed]";
        //echo "Data Barang Tersimpan";
      
        exit;
      
    }
#4

[eluser]WanWizard[/eluser]
The key is generated internally in the Cart class.

Every unique combination of $data_barang['id'] and $data_barang['options'] will generate a unique key, and therefore a new row in the list of cart items.

So
Code:
$data_barang = array(
    'id'      => $c['id_barang'],
    'qty'     => $c['jumlah'],
    'price'   => 1,
    'name'    => $c['namabarang'],
    'jenis_penjualan' => $c['id_penjualan'],
'options' => array('kode' => $c['kodebarang'])  
);

and


Code:
$data_barang = array(
    'id'      => $c['id_barang'],
    'qty'     => $c['jumlah'],
    'price'   => 1,
    'name'    => $c['namabarang'],
    'jenis_penjualan' => $c['id_penjualan'],
'options' => array('kode' => $c['kodebarang'], 'discount' => 'Free bonus item')
);

are two different items in the cart.




Theme © iAndrew 2016 - Forum software by © MyBB