[eluser]The Questioner[/eluser]
I've been working on my own ecommerce site based upon the cart class. Along the way, I've extended the cart class for some much-needed functionality. Here are the most useful functions (all to be placed in the cart class):-
To check if a cart item exists (by row ID):
Code:
function does_cart_item_exist($strRowID)
//check if cart item exists by checking key
{
return array_key_exists($strRowID, $this->_cart_contents);
}
To retrieve a specific cart product item (by row ID):
Code:
function get_cart_item($strRowID)
//retrieve specific cart item object array
{
//check if cart item exists first
if (array_key_exists($strRowID,$this->_cart_contents))
{
return $this->_cart_contents[$strRowID];
}
else
{
return NULL;
};
}
To replace the contents of a cart product item with updated values (by row ID):
Code:
function update_existing_cart_item($strRowID, $arrData)
//update specific cart item object array
{
$this->_cart_contents[$strRowID]=$arrData;
$this->_save_cart();
}
$arrData has to contain the full array that is used when inserting a new cart item, as well as the
rowid. For example:
Code:
//prepare all details for updated cart item
$data = array(
'id' => $objProduct->id,
'qty' => $objProduct->qty,
'price' => $objProduct->price_inc_vat,
'name' => $objProduct->extended_name,
'options' => $arrProductAttributes
);
//add rowid with existing cart item row id so that it knows what to replace
$data['rowid']=$strCartItemKey;
$this->cart->update_existing_cart_item($strCartItemKey,$data);
I hope this helps my fellow CodeIgnities.