refrence
 http://codereview.stackexchange.com/questions/25671/my-perfect-shopping-cart-class

Easy way is copy this code to your php editor.

1、const ID, QTY for column name, it’s can change by yourself.
2、session only check isset.
3、array_push_after() maybe use not good way.
4、$item from CartItemtoArray(), this is array(). not class object.
5、getIndexOfEntry() try to get Array $container at index.

So use Array to control, no Object. Becasue I don’t know refrence, answer 1 and 2, How to create item add in cart. ID is key.  Answer 1 can run, answer 2 is broken.

How to use:

Normal:

$cart = new SessionCart();
$item = cart->CartItemtoArray(1234);  //item_id = 1234
$cart->add($item);
//then control shopping cart by method

Advance: put item desciption into $item array for count or outprint

$cart = new SessionCart();
$item_other = array(“item_name”=>“apple”, “item_price”=>10, “item_weight”=>0.5);
$item = cart->CartItemtoArray(1234, $item_other );  //item_id = 1234
$cart->add($item);
//then control shopping cart by method

class SessionCart {
    const ID = “item_id”;
    const QTY = “quantity”;
    protected $container;

    public function __construct($storageIndex = “_CART_") {
        if (is_null($this->container)) {
            //if (session_id() == ‘') {
            //    session_start();
            //}
            if (!isset($_SESSION[$storageIndex])) {
                $_SESSION[$storageIndex] = array();
            }

            $this->container = &$_SESSION[$storageIndex];
        }
        //$this->container = &$container;
    }
 
    public function CartItemtoArray($item_id, $others=array(), $qty=1){
        $qty = ($qty < 1)?1:$qty;
        $rt = array(self::ID=>$item_id, self::QTY=>$qty);
     
        foreach($others as $key => $value){
            $rt = array_push_after($rt, array("$key”=>$value), 0);
        }
     
        return $rt;
    }

    public function add($item, $quantity=1) {
        $index = $this->getIndexOfEntry($item);

        if ($index == -1) {
            //$item[self::QTY] += $quantity;  //第一次不用補上
            $this->container[] = $item;
        } else {
            $this->container[$index][self::QTY] += $quantity;

            return $this->container[$index][self::QTY];
        }
    }

    public function remove($item) {
        $index = $this->getIndexOfEntry($item);
        unset($this->container[$index]);
    }

    public function getQuantity($item) {
        $index = $this->getIndexOfEntry($item);
        return $index == -1 ? 0 : $this->container[$index][self::QTY];
    }

    public function setQuantity($item, $quantity) {
        $quantity = (int)$quantity;

        if ($quantity < 1) {
            $this->remove($item);
            return;
        }

        $index = $this->getIndexOfEntry($item);

        if ($index == -1) {
            $item[self::QTY] = $quantity;
            $this->container[] = $item;
        } else {
            $this->container[$index][self::QTY] = $quantity;
        }
    }

    public function isEmpty() {
        return empty($this->container);
    }

    public function getAll($crud="") {
        //return new SplFixedArray($this->container);
     
        //$cart = array();
        //foreach ($this->container as $entry) {
        //    if ($entry->GetQuantity() > 0) {
        //        $cart[] = $entry;
        //    }
        //}
        //return $cart;
 
        return $this->container;      
    }

    public function clear() {
        $this->container = array();
    }

    private function getIndexOfEntry($item) {
        //var_dump($item); var_dump($this->container);
        foreach ($this->container as $key => $entry) {
            //echo “key:$key”; print_r($entry);
            if ($item[self::ID] == $entry[self::ID]) {
                return $key;
            }
        }

        return -1;
    }

    public function getIterator() {
        return new ArrayIterator($this->container);
    }
 
}

function array_push_after($src,$in,$pos){
    if(is_int($pos)) $R=array_merge(array_slice($src,0,$pos+1), $in, array_slice($src,$pos+1));
    else{
        foreach($src as $k=>$v){
            $R[$k]=$v;
            if($k==$pos)$R=array_merge($R,$in);
        }
    }return $R;
}

==========
Now need to SubTotol price for item, Totol price all item,  some discount rule for price.

Thinking…. No goo way for class~