creating a shopping cart using session

644 views
Skip to first unread message

Hassan Alnatour

unread,
Apr 10, 2012, 6:06:43 AM4/10/12
to web...@googlegroups.com
Dear ALL , 

How can i create somting in the session that i can save things too , i want to create a shopping cart and save some items in it and after the user finish i want to send an email ,, any help guys or do you have any other ideas 


best regards


pbreit

unread,
Apr 10, 2012, 9:09:21 AM4/10/12
to web...@googlegroups.com
You could save the items to the DB and then store the ID in session (session.cart_id=1) or store the contents right in the session cookie: session.cart_items=[{'id':123 ,'qty':1}, {'id':456, 'qty':3}]

hasan alnator

unread,
Apr 10, 2012, 9:11:34 AM4/10/12
to web...@googlegroups.com
if i use the DB how would i know if the user finished shopping , and if i want to use session how do i add items to it ?

pbreit

unread,
Apr 10, 2012, 9:21:16 AM4/10/12
to web...@googlegroups.com
Either way, you know the shopper is finished when they go to check out and pay. Maybe you mean, when do you delete the records from the DB? I would just leave them there. It is good to have a record of abandoned shopping carts.

One way is a simple list append: session.cart_items.append({'id':789 ,'qty':1})

But you may need to do more programming such as to handle adding an item to the cart that is already in the cart, deleting items, etc. But this should all just be basic Python list handling. You're just storing a list in the shopper's session cookie.

hasan alnator

unread,
Apr 10, 2012, 9:23:52 AM4/10/12
to web...@googlegroups.com

The problem is that i dont have payments .. All payment are on delivary

pbreit

unread,
Apr 10, 2012, 9:22:30 PM4/10/12
to web...@googlegroups.com
You don't have to have payments but there should be some indication the buyer has finished shopping, provided their address and is expecting to receive the items.

Hassan Alnatour

unread,
Apr 11, 2012, 3:11:18 AM4/11/12
to web...@googlegroups.com
i am trying to work with session like you showed me but still when i add somthing it just changes the last element in the session to the element i added

Keith Edmunds

unread,
Apr 11, 2012, 3:24:14 AM4/11/12
to web...@googlegroups.com

Show us the code.
--
"You can have everything in life you want if you help enough other people
get what they want" - Zig Ziglar.

Who did you help today?

Hassan Alnatour

unread,
Apr 11, 2012, 3:58:54 AM4/11/12
to web...@googlegroups.com

session.cart_items=[{'id':123 ,'qty':1}, {'id':456, 'qty':3}]
    items = session.cart_items        
    if request.vars:
        
            session.cart_items.append({'id':request.vars.id ,'qty':request.vars.qyt})



and in the view :
<a href="{{=URL('index',vars={'id':'1','qyt':'2'})}}">add</a>

{{=session.cart_items}}
 

pbreit

unread,
Apr 11, 2012, 12:34:47 PM4/11/12
to web...@googlegroups.com
I think that should work but I haven't tried it.

What happens if you delete the first line "session.cart_items=..."? That's going to keep overwriting cart_items each time the controller is called.

You might have to ensure that cart_items is a list:

items = session.cart_items        
if request.vars:
    if not session.cart_items:
        session.cart_items = []
    session.cart_items.append({'id':request.vars.id ,'qty':request.vars.qyt})

The other thing I'm wondering is if you might need to re-write the list each time:

items = session.cart_items or []  
if request.vars:
    items.append({'id':request.vars.id ,'qty':request.vars.qyt})
    session.cart_items = items

Reply all
Reply to author
Forward
0 new messages