Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add discounts into views
Michał Ociepkac81c2301874a
diff --git a/discount/__init__.py b/discount/__init__.pyindex 375e80a496..4f582d12a3 100755--- a/discount/__init__.py+++ b/discount/__init__.py@@ -46,3 +46,12 @@ class DiscountManager(list):def filter(self, item, **kwargs):return [discount for discount in selfif discount.can_apply(item, **kwargs)]+++def get_discounts(request):+ if hasattr(request, 'discounts'):+ return request.discounts+ from .models import SelectedProduct+ selected_products = SelectedProduct.objects.all()+ request.discounts = DiscountManager(selected_products)+ return request.discountsdiff --git a/product/views.py b/product/views.pyindex 66af858072..09096f9700 100644--- a/product/views.py+++ b/product/views.py@@ -1,5 +1,6 @@from .forms import ProductFormfrom .models import Product, Category+from discount import get_discountsfrom django.http import HttpResponsePermanentRedirectfrom django.shortcuts import get_object_or_404from django.template.response import TemplateResponse@@ -13,6 +14,8 @@ def product_details(request, slug, product_id):data=request.POST or None)if form.is_valid():form.save()+ discounts = get_discounts(request)+ discounts.apply(product)return TemplateResponse(request, 'product/details.html', {'product': product,'form': form