def product_detail(request, slug): instance = get_object_or_404(Product, slug=slug) menu_categories, cart, cart_items = initial_data(request) images = ProductPhotos.objects.filter(product=instance) seo_title = '%s' % instance.title if instance.size: form = CartItemCreateWithAttrForm(instance_related=instance) else: form = CartItemCreate() if request.POST: if instance.size: form = CartItemCreateWithAttrForm(instance, request.POST) if form.is_valid(): qty = form.cleaned_data.get('qty', 1) attribute = form.cleaned_data.get('attribute') order = check_or_create_cart(request) CartItem.create_cart_item(request, order=order, product=instance, qty=qty, size=attribute) messages.success(request, 'The product %s with the size %s added in your cart' % (instance.title, attribute ) ) return HttpResponseRedirect(reverse('product_page', kwargs={'slug': instance.slug})) else: form = CartItemCreate(request.POST) if form.is_valid(): qty = form.cleaned_data.get('qty', 1) order = check_or_create_cart(request) CartItem.create_cart_item(request, order=order, product=instance, qty=qty) messages.success(request, 'The product %s added in your cart' % instance.title) return HttpResponseRedirect(reverse('product_page', kwargs={'slug': instance.slug})) context = locals() return render(request, 'home/product_page.html', context)
def product_detail(request, slug): instance = get_object_or_404(Product, slug=slug) menu_categories, cart, cart_items = initial_data(request) images = ProductPhotos.objects.filter(product=instance) seo_title = '%s' % instance.title if instance.size: form = CartItemCreateWithAttrForm(instance_related=instance) else: form = CartItemCreate() if request.POST: if instance.size: form = CartItemCreateWithAttrForm(instance, request.POST) if form.is_valid(): qty = form.cleaned_data.get('qty', 1) attribute = form.cleaned_data.get('attribute') order = check_or_create_cart(request) CartItem.create_cart_item(request, order=order, product=instance, qty=qty, size=attribute) messages.success( request, 'The product %s with the size %s added in your cart' % (instance.title, attribute)) return HttpResponseRedirect( reverse('product_page', kwargs={'slug': instance.slug})) else: form = CartItemCreate(request.POST) if form.is_valid(): qty = form.cleaned_data.get('qty', 1) order = check_or_create_cart(request) CartItem.create_cart_item(request, order=order, product=instance, qty=qty) messages.success( request, 'The product %s added in your cart' % instance.title) return HttpResponseRedirect( reverse('product_page', kwargs={'slug': instance.slug})) context = locals() return render(request, 'home/product_page.html', context)
def add_product_with_attr_to_cart(request, slug): cart = check_or_create_cart(request) qty = request.POST.get('qty', None) attribute = request.POST.get('attribute', None) product = get_object_or_404(Product, slug=slug) result, message = CartItem.create_cart_item(cart, product, int(qty), attribute) messages.success(request, message) return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
def form_valid(self, form): product = get_object_or_404(Product, slug=self.kwargs['slug']) qty = form.cleaned_data.get('qty', 1) attribute_id = self.request.POST.get('attribute', None) cart = check_or_create_cart(self.request) result, message = CartItem.create_cart_item(cart, product, qty, attribute_id) messages.success(self.request, message) return super(ProductView, self).form_valid(form)