Welcome Guest, Not a member yet? Register   Sign In
Two shopping carts possible with $this->load->library('cart'); ??
#1

[eluser]osfan[/eluser]
Hi Guys,

I've used the CI cart library for building a shopping cart for my application, and it works very well, however, I need to have 2 separate shopping carts for the site that I'm building, or some easy way to distinguish between the 2 carts when I'm working with them.

Obviously it's unusual to have 2 carts, but I don't have a choice, so I'm wondering if anyone has any suggestions for telling the items apart for each cart ? Item id's will look exactly the same, i.e. 'id' => 1 or 'id' => 2 etc... for both carts so not easy to decide where to look for the data, or show that there are items in the cart.

Some lateral thinking is required, should I be looking to extend the cart library, or as I usually prefer keep it simple and find a more obvious way that requires less code and regression testing.

All thoughts gratefully received.

Thanks
#2

[eluser]WanWizard[/eluser]
I've quickly converted the Cart class to a Multicart class.

It is compatible with CI's Cart class, you can slot it in without code modifications by loading it like this:
Code:
$this->load->library('Multicart', array(), 'cart');

It adds a new method create() which allows you to create new named carts. All other methods ( except format_number() ) have an optional extra parameter to pass the name of the cart. To provide backward compatibility, it creates a cart named 'default' when you load the library, which all method calls will use if you don't pass a cart name.

Example:
Code:
$this->load->library('Multicart', array(), 'cart');

$this->cart->create('cart1');
$data1 = array(
               array(
                       'id'      => 'sku_123ABC',
                       'qty'     => 1,
                       'price'   => 39.95,
                       'name'    => 'T-Shirt',
                       'options' => array('Size' => 'L', 'Color' => 'Red')
                    )
            );
$this->cart->insert($data1, 'cart1');

print_r($this->cart->contents('cart1'));

$this->cart->destroy('cart1');

$this->cart->create('cart2');
$data2 = array(
               array(
                       'id'      => 'sku_567ZYX',
                       'qty'     => 1,
                       'price'   => 9.95,
                       'name'    => 'Coffee Mug'
                    )
            );
$this->cart->insert($data2, 'cart2');

print_r($this->cart->contents('cart2'));

// shows the 'default' cart, so no contents
print_r($this->cart->contents());

// what's in session storage right now?
print_r($this->CI->session->userdata('cart_contents'));
#3

[eluser]osfan[/eluser]
Hi wizard,

Thanks for the reply, and the code, I'll try it out, it's save me a lot of messing around.

One of the reasons that I like CI so much is the people using it, always ready to help Smile
#4

[eluser]mighty_falcon[/eluser]
For those that plan to use this library, there is a small bug in the _update function:

Look for the line:

Code:
if ( ! isset($items['qty']) OR ! isset($items['rowid']) OR ! isset($this->_cart_contents[$items['rowid']])) {

and change it to

Code:
if ( ! isset($items['qty']) OR ! isset($items['rowid']) OR ! isset($this->_cart_contents[$cart][$items['rowid']])) {

The developer seems to have left out the cart name index which will make the update function always return FALSE and never actually update the quantity.
#5

[eluser]mlieshout[/eluser]
The _update is still not working, it always returns FALSE. I do have an array with an rowid and a qty.

Any idea what's wrong?




Theme © iAndrew 2016 - Forum software by © MyBB