def order_create(request): cart = Cart(request) if request.method == 'POST': form = OrderCreateForm(request.POST) if form.is_valid(): order = Order(**form.data) order.save() order.paid="1" for item in cart: order.products.connect(item["product"], {'quantity':item['quantity']}) order.save() print(order.products.all()) product_ids = list(cart.cart.keys()) print(product_ids) products = [] for id in product_ids: query = f"match (a) where ID(a) = {id} return a" result, meta = db.cypher_query(query) products.append(Product.inflate(result[0][0])) r = Recommender() r.products_bought([products]) # clear the cart cart.clear() return render(request, 'orders/order/created.html', {'order': order}) else: form = OrderCreateForm() return render(request, 'orders/order/create.html', {'cart': cart, 'form': form})
def __iter__(self): """ Iterate over the items in the cart and get the products from the database. """ product_ids = list(self.cart.keys()) # get the product objects and add them to the cart # products = Product.objects.filter(id__in=product_ids) products = [] for id in product_ids: # print(id) query = f"match (a) where ID(a) = {id} return a" result, meta = db.cypher_query(query) products.append(Product.inflate(result[0][0])) cart = self.cart.copy() for product in products: cart[str(product.id)]['product'] = product for item in cart.values(): item['price'] = Decimal(item['price']) item['total_price'] = item['price'] * item['quantity'] yield item
def cart_add(request, product_id): cart = Cart(request) query = f"match (a) where ID(a) = {product_id} return a" product, _ = db.cypher_query(query) product = Product.inflate(product[0][0]) form = CartAddProductForm(request.POST) if form.is_valid(): cd = form.cleaned_data cart.add(product=product, quantity=cd['quantity'], override_quantity=cd['override']) return redirect('cart:cart_detail')