you should go to cakeforge.org and add this as a snippet ;)

On 6/22/06, jeko < [EMAIL PROTECTED]> wrote:

Thought this might be useful for some, manages a session cart/wishlist:

<?php
class WishlistController extends AppController
{
        var $uses = 'Product';
        var $helpers = array('Html', '_javascript_');
        var $layout = 'default';

        // loop thru products in session
        // get info and make a delete key
        function index()
        {
                $i = 0;
                $wishlist = null;
                $products = $this->Session->read('Wishlist');
                pr($products);
                if (isset($products))
                {
                        foreach ($products as $product => $qty)
                        {
                                $wishlist[$i] = $this->Product->find(array('product_number' =>
$product), 'product_number, name, wholesale');
                                $wishlist[$i]['Product']['deleteKey'] = $product;
                                $wishlist[$i]['Product']['quantity'] = $qty;
                                $i++;
                        }
                }
                $this->set('wishlist', $wishlist);
        }

        // save any changes made to qunitites of items
        // otherwise proceed to checkout information
        function save()
        {
                $wishlist = $this->Session->read('Wishlist');
                $arr = $this->data['Wishlist'];
                $count = count($wishlist);
                //quantities changed
                if ($this->params['form']['update'])
                {
                        foreach ($arr as $product => $qty)
                        {
                                $wishlist[$product] = $qty;
                        }
                        $this->Session->write('Wishlist', $wishlist);
                        $this->redirect('/wishlist/');
                }
                //proceed to check out
                else if ($this->params['form']['next'])
                {
                        $this->redirect('/orders/billing/');
                }
                else
                {
                        $this->redirect('/wishlist');
                }
        }

        // adds a product id to session
        // default quantity is one
        function add()
        {
                $product = $this->data;
                if (empty($this->data))
                {
                        $this->render();
                }
                else
                {
                        $wishlist = $this->Session->read('Wishlist');
                        $wishlist[$product['Product']['product_number']] = 1;

                        $key = 'Wishlist';
                        $value = $wishlist;
                        if ($this->Session->write($key, $value))
                        {
                                $this->flash($product['name'].' added.','/product_offerings');
                        }
                        else
                        {
                                $this->set('wishlist', $this->data);
                                $this->render();
                        }
                }
                $wishlist = $this->Session->read('Wishlist');
                $this->set('wishlist', $wishlist);
        }

        // delete key in session
        function delete($key=null)
        {
                $this->Session->del('Wishlist.'.$key);
                $this->redirect('/wishlist');
        }
}
?>



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply via email to