Cart session callback function

53 views
Skip to first unread message

mostwanted

unread,
Jul 2, 2019, 8:35:03 AM7/2/19
to web2py-users
Can anyone please explain to me in lay-man terms how the code below works! It is a function for adding items to the shopping cart. I am trying to use the same concept to have customers add item sizes as well as they add the items to the cart but i am missing something!

VIEW
<br />
   
<span id="{{='item%s'%p.id}}" style="font-weight: bold; color: red;">{{=session.cart.get(p.id,0)}}</span> in cart - {{=A('add to cart',callback=URL('cart_callback',vars=dict(id=p.id,action='add')),target='item%s'%p.id,_class='button pill')}}
   
<br />

CART VIEW
</td></tr>
  {{for id, qty in cart.items():}}{{p=db.product(id)}}
 
<tr id="boughtItems">
     
<td><span style="font-weight: bold;">{{=p.item}}</span></td>
     
<td><span style="font-weight: bold;">{{=MoneyFormat(p.price)}}</span></td>
     
<td><span id="{{='item%s'%p.id}}" style="font-weight: bold;">{{=qty}}</span></td>
     
<td><span style="font-weight: bold;">{{=MoneyFormat(qty*p.price)}}</span></td>
     
<td>

CONTROLLER
def cart_callback():
    id
= int(request.vars.id)
   
if request.vars.action == 'add':
        session
.cart[id]=session.cart.get(id,0)+1
   
if request.vars.action == 'sub':
        session
.cart[id]=max(0,session.cart.get(id,0)-1)
   
return str(session.cart[id])

Regards

Mostwanted


Val K

unread,
Jul 2, 2019, 2:40:54 PM7/2/19
to web2py-users
user press 'button pill'  - web2py.js does ajax request with URL = ('cart_callback', ...) and writes response to  html element with id = target = 'items%s'%p.id 

mostwanted

unread,
Jul 3, 2019, 1:11:35 AM7/3/19
to web2py-users
I wanna be able to have my users select size on the items they are selecting and be able to send it to cart with the other details of the selected item, i dont know how to achieve this, any ideas?

<br />
   
<span id="{{='item%s'%p.id}}" style="font-weight: bold; color: red;">{{=session.cart.get(p.id,0)}}</span> in cart - {{=A('add to cart',callback=URL('cart_callback',vars=dict(id=p.id,action='add')),target='item%s'%p.id,_class='button pill')}}
   
<br />

<select>
<option selected="selecred">Choose Size</option>
<option>1</option>

<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>



Val K

unread,
Jul 3, 2019, 2:25:20 PM7/3/19
to web2py-users
As you can see url callback is generated at server, so to do what you want you have to use js (jquery or ...)
Reply all
Reply to author
Forward
0 new messages