Example #1
0
def cart_final_value(request):
    global voucher_used, shipping_cost, dia_chi_id, total
    now = timezone.now()
    dia_chi = DiaChiKhachHang.objects.filter(user=request.user).order_by("-pk")[:3]
    #dia_chi_chon = None
    dia_chi_id = request.POST['dia_chi_id']
    dia_chi_chon = DiaChiKhachHang.objects.filter(pk=dia_chi_id).first()
    voucher = Voucher.objects.filter(ngay_bat_dau__lte=now, ngay_het_han__gte=now)
    try:
        voucher_id = request.POST['voucher_id']
        if voucher_id == '':
            voucher_id = 0
        voucher_used = Voucher.objects.get(id=voucher_id,ngay_bat_dau__lte=now, ngay_het_han__gte=now)
        discount = voucher_used.gia_tri
    except Voucher.DoesNotExist:
        voucher_id = ''
        discount = 0
        voucher_used = None
    cart = MyCart(request)
    #voucher = Voucher.objects.all
    subtotal = 0
    for key, value in cart.cart.items():
        subtotal += int(value['quantity']) * int(value['price'])
    shipping_cost = 10 + int(subtotal*5/100)
    total = subtotal + shipping_cost - discount
    if total <0:
        total = 0
    context = {'subtotal':subtotal, 'ship':shipping_cost,
               'voucher':voucher, 'discount':discount,
               'total':total, 'voucher_id':voucher_id,'dia_chi':dia_chi,'dia_chi_id':dia_chi_id,'dia_chi_chon':dia_chi_chon}
    return render(request, 'cart/cart_detail.html', context)
Example #2
0
def don_hang(request):
    cart = MyCart(request)
    gio_hang = GioHang.objects.create(user=request.user, tao_vao=timezone.now())
    gio_hang.save()
    dia_chi = DiaChiKhachHang.objects.filter(pk=dia_chi_id).first()
    donhang = DonHang.objects.create(khach_hang=request.user, cart=gio_hang, voucher=voucher_used,
                                     thoi_gian_dat_hang=timezone.now(), dia_chi_giao_hang=dia_chi,
                                     phi_ship=shipping_cost, total=total)
    donhang.save()
    for product in cart:
        remain_item = Sach.objects.get(pk=product['product_id'])
        remain_item.so_luong_con = product['so_luong_con']
        remain_item.save()
        order_item = Sach.objects.get(pk=product['product_id'])
        order_item = ItemTrongDonHang.objects.create(don_hang=donhang, item=order_item, so_luong=product['quantity'])
        order_item.save()
    cart.clear()
    return redirect('core:order_detail', id=donhang.pk)
Example #3
0
def item_decrement(request, id):
    cart = MyCart(request)
    product = Sach.objects.get(id=id)
    cart.decrement(product=product)
    return redirect('core:cart_detail')
Example #4
0
def item_clear(request, id):
    cart = MyCart(request)
    product = Sach.objects.get(id=id)
    cart.remove(product)
    return redirect('core:cart_detail')
Example #5
0
def cart_clear(request):
    cart = MyCart(request)
    cart.clear()
    return redirect('core:cart_detail')
Example #6
0
def cart_add(request, id):
    cart = MyCart(request)
    product = Sach.objects.get(id=id)
    cart.add(product=product)
    return redirect('core:index')