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)
def cart_clear(request): cart = MyCart(request) cart.clear() return redirect('core:cart_detail')