def form_valid(self, form): cart = utils.get_cart(self.request) book_id = form.cleaned_data['book'] quantity = form.cleaned_data['quantity'] exist = cart.get(str(book_id)) if exist is not None: cart[str(book_id)] += quantity else: cart[str(book_id)] = quantity # Need for update session database self.request.session.modified = True book = get_object_or_404(Book, id=book_id) messages.success(self.request, '"{}" has been added to your cart.'.format( book.title), extra_tags='woocommerce-message') return HttpResponseRedirect( reverse('books:detail', kwargs={ 'pk': book_id, 'slug': book.slug }))
def get_one_book_total_price(context, item_id): try: cart = get_cart(context['request']) book = Book.objects.get(id=item_id) total_price = book.price * cart[str(item_id)] except Exception: total_price = 0 return total_price
def form_valid(self, form): cart = utils.get_cart(self.request) book_id = form.cleaned_data['book'] quantity = form.cleaned_data['quantity'] cart[str(book_id)] = quantity self.request.session.modified = True book = get_object_or_404(Book, id=book_id) return HttpResponseRedirect(reverse('books:detail', kwargs={'pk': book_id, 'slug': book.slug}))
def get_total_price(context): try: total_price = 0 cart = get_cart(context['request']) for key, value in cart.items(): book = Book.objects.get(id=key) total_price += (book.price * int(value)) except Exception: total_price = 0 return total_price
def form_valid(self, form): cart = utils.get_cart(self.request) formset = form for form in formset: book_id = form.cleaned_data['book'] quantity = form.cleaned_data['quantity'] cart.update({str(book_id): quantity}) self.request.session.modified = True messages.success(self.request, "Cart is updated successfully!", extra_tags='woocommerce-message') return HttpResponseRedirect(self.success_url)
def form_valid(self, form): cart = utils.get_cart(self.request) book_id = form.cleaned_data['book'] quantity = form.cleaned_data['quantity'] cart[str(book_id)] = quantity self.request.session.modified = True book = get_object_or_404(Book, id=book_id) return HttpResponseRedirect( reverse('books:detail', kwargs={ 'pk': book_id, 'slug': book.slug }))
def form_valid(self, form): book_id = form.cleaned_data['book'] cart = utils.get_cart(self.request) book_id = form.cleaned_data['book'] if str(book_id) in cart.keys(): del cart[str(book_id)] self.request.session.modified = True book = get_object_or_404(Book, id=book_id) messages.success(self.request, '"{}" has been removed from your cart.'.format(book.title), extra_tags='woocommerce-message') return HttpResponseRedirect(reverse('carts:view'))
def form_valid(self, form): book_id = form.cleaned_data['book'] cart = utils.get_cart(self.request) book_id = form.cleaned_data['book'] if str(book_id) in cart.keys(): del cart[str(book_id)] self.request.session.modified = True book = get_object_or_404(Book, id=book_id) messages.success(self.request, '"{}" has been removed from your cart.'.format( book.title), extra_tags='woocommerce-message') return HttpResponseRedirect(reverse('carts:view'))
def form_valid(self, form): cart = utils.get_cart(self.request) form.instance.user_profile = self.request.user.profile self.object = form.save() for key, value in cart.items(): book = Book.objects.get(pk=int(key)) item = Item.objects.create(product=book, quantity=value, cart=self.object) item.save() cart.clear() self.request.session.modified = True messages.success(self.request, "You checked out successfully.", extra_tags='woocommerce-message') return HttpResponseRedirect(self.get_success_url())
def form_valid(self, form): cart = utils.get_cart(self.request) book_id = form.cleaned_data['book'] quantity = form.cleaned_data['quantity'] exist = cart.get(str(book_id)) if exist is not None: cart[str(book_id)] += quantity else: cart[str(book_id)] = quantity # Need for update session database self.request.session.modified = True book = get_object_or_404(Book, id=book_id) messages.success(self.request, '"{}" has been added to your cart.'.format(book.title), extra_tags='woocommerce-message') return HttpResponseRedirect(reverse('books:detail', kwargs={'pk': book_id, 'slug': book.slug}))
def get_initial(self): cart = utils.get_cart(self.request) initial = [{'book': k, 'quantity': v} for k, v in cart.items()] return initial
def post(self, request, *args, **kwargs): cart = utils.get_cart(request) cart.clear() request.session.modified = True return HttpResponseRedirect(reverse('carts:view'))
def get_quantity(context, item_id): cart = get_cart(context['request']) return cart[str(item_id)]