def add_cart(): pid = request.vars.pid ar_carts =[] if request.cookies.has_key ('cart_shop'): carts = eval(request.cookies['cart_shop'].value) i=0 for cart in carts: cart = eval(cart) if cart['id']==pid: ar_carts.append(str({'id':str(cart['id']) ,'num':str(int(cart['num'])+1)})) i+=1 else: ar_carts.append(str({'id':str(cart['id']) ,'num':str(cart['num'])})) if i==0: ar_carts.append(str({'id':pid ,'num':str(1)})) else: ar_carts.append(str({'id':pid ,'num':str(1)})) response.cookies['cart_shop'] = str(ar_carts) response.cookies['cart_shop']['expires'] = 24 * 3600 response.cookies['cart_shop']['path'] = '/' response.flash = T("Add new cart!") # load view_carts() nhung ko request duoc cookies moi div = DIV() num_car = 0 tong_tien = 0 carts = ar_carts from plugin_cms import CmsModel from plugin_cms import CmsFolder cms = CmsModel() db = cms.db cms.define_table('san_pham') from plugin_app import number_format for cart in carts: cart = eval(cart) row = db((db.san_pham.id==cart['id'])).select().first() if row: div1 = DIV(_class='list_cart') ul = UL() ul.append(LI(row.name)) ul.append(LI(SPAN('Số lượng: '),cart['num'])) ul.append(LI(SPAN('Giá: '),number_format(row.gia_san_pham),' VNĐ')) div1.append(DIV(IMG(_src=cms.get_avatar('san_pham',row.avatar),_class='thumbnail'),_class='col-md-4 box_ivinh')) div1.append(DIV(ul,_class='col-md-8 box_ivinh')) div.append(div1) div.append(HR()) tong_tien += int(row.gia_san_pham)* int(cart['num']) num_car +=1 p_tong = DIV(SPAN('Tổng tiền: ')) p_tong.append(str(number_format(tong_tien))+' VNĐ') div.append(B(p_tong,_class='text-right')) div.append(A('Gửi đơn hàng',_href=URL(c='portal',f='folder',args=['checkout']),_class='btn btn-success')) return div
def view_carts(): div = DIV() num_car = 0 tong_tien = 0 carts = '' try: if request.cookies.has_key('cart_shop'): carts = eval(request.cookies['cart_shop'].value) from plugin_cms import CmsModel from plugin_cms import CmsFolder cms = CmsModel() db = cms.db cms.define_table('san_pham') from plugin_app import number_format for cart in carts: cart = eval(cart) row = db((db.san_pham.id == cart['id']) & (db.san_pham.folder == CmsFolder().get_folder( request.args(0)))).select().first() if row: div1 = DIV(_class='list_cart') ul = UL() ul.append(LI(row.name)) ul.append(LI(SPAN('Số lượng: '), cart['num'])) ul.append( LI(SPAN('Giá: '), number_format(row.gia_san_pham), ' VNĐ')) div1.append( DIV(IMG(_src=cms.get_avatar('san_pham', row.avatar), _class='thumbnail'), _class='col-md-4 box_ivinh')) div1.append(DIV(ul, _class='col-md-8 box_ivinh')) div.append(div1) div.append(HR()) tong_tien += int(row.gia_san_pham) * int(cart['num']) num_car += 1 if num_car > 0: p_tong = DIV(SPAN('Tổng tiền: ')) p_tong.append(str(number_format(tong_tien)) + ' VNĐ') div.append(B(p_tong, _class='text-right')) div.append( A('Thanh toán', _href=URL(c='portal', f='folder', args=request.args(0), vars=dict(page='cart')), _class='btn btn-success')) else: div.append('Giỏ hàng trống') except Exception, e: return e