Welcome Guest, Not a member yet? Register   Sign In
Cart class weirdness
#1

[eluser]bendog[/eluser]
Hi,

Just started looking at the latest release and played a bit with the cart class.

I'm finding some design choice with the class a bit weird, thought I should report the findings if someone is running in the same problems..

Problem 1
The insert method seems to always wipe any existing matching entry with the same rowid..
line 235: unset($this->_cart_contents[$rowid])

This means that when you add an item to the cart a second time it basically replaces it.. most cart implementation have a "add to cart" button, not a "replace cart entry with this new item" button.

Seems odd.

Alternatively, if there was a way to know
a) if an item is already in the cart
b) what the quantity of that item is

we could do a conditional
if (item exist)
update(old item with new quantity + old quantity)
else
insert(new item)

but no function is provided to check if an item is already in the cart or what its quantity is (other than looping through the content, which seems a bit heavy handed)

am I missing something?

this version of the _insert function fixe the problem for me:

Code:
function _insert($items = array())
{
    /** .. all good at the beginning **/
    
    /* Fix */    
    // Now that we have our unique "row ID", we'll add our cart items to the master array
    if (isset($this->_cart_contents[$rowid]))
    {
        $this->_cart_contents[$rowid]['qty'] += $items['qty'];
    }
    else
    {
        // Create a new index with our new row ID
        $this->_cart_contents[$rowid]['rowid'] = $rowid;
    
        // And add the new items to the cart array            
        foreach ($items as $key => $val)
        {
            $this->_cart_contents[$rowid][$key] = $val;
        }
    }
    
    // Woot!
    return TRUE;
}

Problem 2
Finally, there is one more bug I think... the total_items method returns the number of unique rowids, not the number of products in the cart.
line 392: $this->_cart_contents['total_items'] = count($this->_cart_contents);

this should really be calculated at the same time as the total price is calculated.

otherwise, all good Big Grin

Ben
#2

[eluser]bendog[/eluser]
Forgot to post the fix for problem 2.

Code:
function _save_cart()
{
    // Unset these so our total can be calculated correctly below
    unset($this->_cart_contents['total_items']);
    unset($this->_cart_contents['cart_total']);

    // Lets add up the individual prices and set the cart sub-total
    $total = 0;
    $qty = 0;
    foreach ($this->_cart_contents as $key => $val)
    {
        // We make sure the array contains the proper indexes
        if ( ! is_array($val) OR ! isset($val['price']) OR ! isset($val['qty']))
        {
            continue;
        }

        $total += ($val['price'] * $val['qty']);
        $qty += $val['qty'];
        
        // Set the subtotal
        $this->_cart_contents[$key]['subtotal'] = ($this->_cart_contents[$key]['price'] * $this->_cart_contents[$key]['qty']);
    }

    // Set the cart total and total items.
    $this->_cart_contents['total_items'] = $qty;            
    $this->_cart_contents['cart_total'] = $total;

    // Is our cart empty?  If so we delete it from the session
    if (count($this->_cart_contents) <= 2)
    {
        $this->CI->session->unset_userdata('cart_contents');
        
        // Nothing more to do... coffee time!
        return FALSE;
    }

    // If we made it this far it means that our cart has data.
    // Let's pass it to the Session class so it can be stored
    $this->CI->session->set_userdata(array('cart_contents' => $this->_cart_contents));

    // Woot!
    return TRUE;    
}
#3

[eluser]jegbagus[/eluser]
hi bendog,
really great post!

here my opinion regarding your problem :

Problem no 1.
I think CI developer goes to the right way.
there is a missing point in your post.
user should also can reduce their quantity product.
with your solution, they can't reduce their cart quantity, it will always increase.

Problem no 2.
yes i think you are right. they return number of unique item. not total of product in cart.
but you can calculate easily with loop trough the array.
#4

[eluser]InsiteFX[/eluser]
It's a CodeIgniter Library, if it doe's not do what you want just extend the Cart Library.

Enjoy
InsiteFX
#5

[eluser]bendog[/eluser]
[quote author="jegbagus" date="1252938428"]
there is a missing point in your post.
user should also can reduce their quantity product.
with your solution, they can't reduce their cart quantity, it will always increase.
[/quote]

The only practicle case where that would happen is when editing the cart, the update method should take care of that I think...
#6

[eluser]bendog[/eluser]
[quote author="InsiteFX" date="1252985548"]It's a CodeIgniter Library, if it doe's not do what you want just extend the Cart Library.
[/quote]

I wasn't complaining about the library, I think it's great that CI provides the library.

I'm just reporting on the problems I've come across and the solutions I've found in case someone was experiencing the same issues.
#7

[eluser]jegbagus[/eluser]
bendog, you should put this post at featured request.
or create extend for cart library and share it to us Smile
it will help this community much.
regards.
#8

[eluser]tashigiri[/eluser]
[quote author="jegbagus" date="1253003287"]bendog, you should put this post at featured request.
or create extend for cart library and share it to us Smile
it will help this community much.
regards.[/quote]
im agree with that..^^
#9

[eluser]Unknown[/eluser]
This is driving me crazy. I've spent days figuring this out. Yes, I have
read other postings about this, but obviously I'm missing something (part of
my brain perhaps).


finding nemo
Wisconsin Individual Health Insurance
#10

[eluser]Unknown[/eluser]
Bendog thanks for posting your fixes!

The new Cart class has saved me time in my current project, but that first problem was really bugging me.




Theme © iAndrew 2016 - Forum software by © MyBB