Hi Jason!
On Wed, 10 Jan 2001, Jason Beebe wrote:
> Hey,
>
> I'm looking for little information from those who have coded their own shopping cart
>apps. what would you say the best way to setup a cart would be? more specificly, the
>method for adding items to the cart.
>
eww, lost wrapping ...
> say the user is broswing around. when they entered the website a new session was
>created. when they go to add an item, what is the best way store variables (such as
>what they bought and quantity) in the session. i have read that it is possible to put
>all of your session variables in a single associative array. however, i have not had
>any luck doing this myself. Under such a method of an array, how would i store each
>item (product id and quantity, possibly price as well) in the cart into the session.
>obviously i don't expect anyone to write an entire app. i am comfortable starting the
>session, and doing th db extraction and presentation. i just need to know how to
>store a variable in a session (passed through post) and how to extract it when need
>be (considering it is an array). Thanks a lot!
I codded a shopping cart which allowed `thin sessions' and `fat sessions'.
Thin would mean I store only the IDs, then retrieve the rest of the
information about the product from the DB (more DB queries, but smaller
session data), while `fat' would mean I store all the data I need to display
when the user invetories his/her cart, namely name, price &|short desc.,
quantity.
Some code snippets would be:
if (!$SES->isRegistered ('cart.object')) {
$CART = new Cart();
$CART->setLanguage (CART_LANGUAGE);
$SES->setAttribute ('cart.object', $CART);
}
$CART = &$SES->getAttribute ('cart.object');
then go ahead and use the cart object:
...
$CART->addItem ($sku, 1, new Product($arr_attr));
$CART->dropItem ($sku);
And the Product class has among others, these two methods:
__setStorageType ($t)
{
if ($t == CART_FAT_SESS) {
$this->_slots = array_keys (get_object_vars ($this));
unset ($this->_slots['_desc']);
}
}
function __sleep ()
{
return (isset ($this->_slots) ? $this->_slots : array ('_quant'));
}
This is in the manner of Session class from PHPLIB which records what is to
be saved in the session (in this case, what attributes.)
And the Cart class has:
function __wakeup ()
{
if ($this->STORAGE == CART_THIN_SESS) {
$pr_attrs = Cart::getItemsAttr (array_keys($this->_items));
foreach ($pr_attrs as $sku => $attrs) {
$attrs['quant'] = $this->_items[$sku]->getQuantity();
$this->_items[$sku] = new Product ($attrs);
}
}
}
Note that __sleep() and __wakeup() are PHP serialisation hooks, which can customize
the marshalling/unmarshalling process (e.g. restore DB connections, etc.)
cheers,
-- teodor
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]