Codeigniter new instance of cart
In CodeIgniter, you can create a new instance of the cart by using the following code:
$this->cart = new Cart();
This will create a new instance of the cart, which you can then use to add, update, and retrieve items.
Alternatively, you can also use the following code to create a new instance of the cart:
$this->load->library('cart');
This will load the cart library and create a new instance of it, which you can then use to add, update, and retrieve items.
Once you have created a new instance of the cart, you can use the following methods to add, update, and retrieve items:
add($id, $qty = 1)
- adds a new item to the cartupdate($id, $qty)
- updates the quantity of an existing item in the cartremove($id)
- removes an item from the cartcontents()
- returns an array of all items in the carttotal()
- returns the total cost of all items in the cart
For example:
$this->cart = new Cart();
// Add an item to the cart
$this->cart->add(1, 2);
// Update the quantity of an existing item in the cart
$this->cart->update(1, 3);
// Remove an item from the cart
$this->cart->remove(1);
// Get the contents of the cart
$cart_contents = $this->cart->contents();
// Get the total cost of all items in the cart
$total = $this->cart->total();
Note that the cart is a session-based cart, which means that it will store the cart contents in the user's session. This means that the cart will be preserved across page requests, and will be reset when the user logs out or the session expires.