def cart(request): _cart = get_or_create_cart(request, response=None, create=False) cart_products = [] if _cart: cart_products = _cart.productcart_set.all() cart_products_ = list(cart_products) paypal_dict = { "cmd": "_cart", "upload": 1, "business": "*****@*****.**", "return_url": "http://127.0.0.1:8000/success", } cart_products = [] for i, pc in enumerate(cart_products_): cart_products.append([pc.quantity, pc.product]) paypal_dict["amount_%d" % (i+1)] = pc.product.price * pc.quantity paypal_dict["item_name_%d" % (i+1)] = pc.product.title form = PayPalPaymentsForm(initial=paypal_dict) context = {"form": form, "cart_data": cart_products, 'cart': _cart} return render(request, 'mainapp/cart.html', context)
def remove(request): _cart = get_or_create_cart(request, response=None, create=False) prod = Product.objects.filter(id=int(request.GET['item']))[0] product_cart = ProductCart.objects.filter(product=prod, cart=_cart) if product_cart: product_cart[0].delete() _cart.save(_cache=True) return HttpResponse(str(_cart.total_price))
def success(request): _cart = get_or_create_cart(request, response=None, create=False) cart_products = [] if _cart: cart_products = _cart.productcart_set.all() cart_products_ = list(cart_products) cart_products = [] for i, pc in enumerate(cart_products_): cart_products.append([pc.quantity, pc.product]) context = {"cart_data": cart_products, 'cart': _cart} close_cart(_cart) return render(request, "mainapp/success.html", context)
def add(request): response = HttpResponse("Добавлено") product = Product.objects.filter(id=int(request.GET['product_id']))[0] _cart = get_or_create_cart(request=request, response=response) product_cart = ProductCart.objects.filter(product=product, cart=_cart) if not product_cart: ProductCart(product=product, cart=_cart).save() else: product_cart[0].quantity += 1 product_cart[0].save() _cart.save(_cache=True) return response
def get_total_price(request): _cart = get_or_create_cart(request) return HttpResponse('Сумма %s руб.' % str(_cart.total_price))