Welcome Guest, Not a member yet? Register   Sign In
Cart class - total_items() not displaying actual total - bug?
#1

[eluser]nottRobin[/eluser]
Imagine I have a shopping cart with the following items:

Code:
// rows removed for readability
array(2) {
  array() {
    ["id"]=> int(1)
    ["qty"]=> string(1) "5"
    ["name"]=> string(41) "Phantom item one"
  }
  array() {
    ["id"]=> int(2)
    ["qty"]=> string(1) "4"
    ["name"]=> string(41) "Phantom item two"
  }
}

Now to my thinking the "total number of items" in this cart should be
Code:
4+5=9

However, this is not what the "total_items()" (see the Cart documentation) method outputs:
Code:
die((string)$this->cart->total_items()); // outputs "2" - not "9"

Can anyone enlighten me as to why this is? And possibly tell me if the cart class does in fact support outputting "total items" in the way that I want?

Cheers,
Robin.
#2

[eluser]nottRobin[/eluser]
The reason why I think it would be more helpful if the "total_items" method output what I'm going to call "total discrete items" rather than "total item types" is because, if you want the latter, you can get it easily enough using the following code:

Code:
die((string)count($this->cart->contents())); // Outputs "2"

So it would far more helpful if the method "total_items()" had the more complex "total discrete items" functionality instead.
#3

[eluser]nottRobin[/eluser]
If anyone confirms they think this is a bug, I will submit it to bug tracker.

In the meantime, fixing it isn't that hard.

Edit - This fix is deprecated. Please see my next post below.

Just add the following method to the end of /system/libraries/Cart.php (before the final '}'):

Code:
/**
* Get the total number of discrete items in the cart
*
* Unlike total_items - which gets the number of "entries" in the cart,
* but not the actual total number of individual items,
* this method gives the sum of all $item['qty']s.
*
* @access  public
* @return  integer
*/
function total_discrete_items() {
    $total = 0;
    foreach($this->contents() as $item) {
        $total += intval($item['qty']);
    }
    return $total;
}

Now, for the cart mentioned above:
Code:
die(
  (string) $this->cart->total_discrete_items() // outputs "9" - as expected
);

Please let me know if anyone else finds this useful.
#4

[eluser]coffeeandcode[/eluser]
[quote author="nottRobin" date="1280255910"]Just add the following method to the end of /system/libraries/Cart.php (before the final '}'[/quote]

It's much better to extend the library in /system/application/libraries/MY_Cart.php. Anything you add there will be added to the system's class.

Creating Libraries - see the section entitled "Extending Native Libraries".
#5

[eluser]nottRobin[/eluser]
Thanks coffeeandcode.

I've now removed my edits to system/libraries/Cart.php, and created a new file in /system/application/libraries/My_Cart.php:

Code:
<?php

class MY_Cart extends CI_Cart {

    function __construct() {
        parent::CI_Cart();
    }
    
    /**
     * Get the total number of discrete items in the cart
     *
     * Unlike total_items - which gets the number of "entries" in the cart,
     * but not the actual total number of individual items,
     * this method gives the sum of all $item['qty']s.
     *
     * @access  public
     * @return  integer
     */
    function total_discrete_items() {
        $total = 0;
        foreach($this->contents() as $item) {
            $total += intval($item['qty']);
        }
        return $total;
    }
}

/* End of file MY_Cart.php */
/* Location: ./system/application/libraries/MY_Cart.php */
#6

[eluser]stikoo[/eluser]
Thanks nottRobin,

This is exactly what I was after, I too was about to make the same mistake and shove a piece of code into the Core Library lol,

So thanks for the above info!




Theme © iAndrew 2016 - Forum software by © MyBB