Insert records Cart codeigniter

Asked

Viewed 29 times

0

I have the following PHP

if($this->input->post('stock_unity_push')=='0'){
    $stock_unity_push = 0;
} else {
    $stock_unity_push = str_replace(".", "", str_replace("R$ ", "", $this->input->post('stock_unity_push')));
    $stock_unity_push = number_format((float)$stock_unity_push, 2, '.', '');
}

$data = array(
    'id'      => $this->input->post('product_id'),
    'qty'     => $this->input->post('stock_purchase'),
    'price'   => ($stock_unity_push) ? $stock_unity_push : '0',
    'name'    => 'product_'.$this->input->post('product_id')
    //'options' => array('supplier' => $this->input->post('supplier'))
);

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

But to add to cart, it adds only one record, and replaces the current Cart. I am using the codeigniter Cart.

1 answer

1

According to the documentation the way to add more than one item is by doing so.

$data = array(
        array(
                'id'      => 'sku_123ABC',
                'qty'     => 1,
                'price'   => 39.95,
                'name'    => 'T-Shirt',
                'options' => array('Size' => 'L', 'Color' => 'Red')
        ),
        array(
                'id'      => 'sku_567ZYX',
                'qty'     => 1,
                'price'   => 9.95,
                'name'    => 'Coffee Mug'
        ),
        array(
                'id'      => 'sku_965QRS',
                'qty'     => 1,
                'price'   => 29.95,
                'name'    => 'Shot Glass'
        )
);

But you can use the function array_push to assemble your $data and only at the end call the Insert method.

  • It would be right, but then as I would in my PHP?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.