forked from CGM_Public/pretix_original
Cross selling (#4185)
Product categories can now be marked as "cross-selling categories", causing them to appear in the add-on checkout step as additional recommendations, depending on their cross-selling visibility (always, only if certain products are already in the cart, or only if they qualify for a discount according to discount rules). --------- Co-authored-by: Raphael Michel <michel@rami.io> Co-authored-by: Richard Schreiber <schreiber@rami.io>
This commit is contained in:
@@ -40,7 +40,8 @@ from urllib.parse import urlencode
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db.models import Max
|
||||
from django.db.models import Max, Q
|
||||
from django.forms import ChoiceField, RadioSelect
|
||||
from django.forms.formsets import DELETION_FIELD_NAME
|
||||
from django.urls import reverse
|
||||
from django.utils.functional import cached_property
|
||||
@@ -79,11 +80,67 @@ class CategoryForm(I18nModelForm):
|
||||
'name',
|
||||
'internal_name',
|
||||
'description',
|
||||
'is_addon'
|
||||
'cross_selling_condition',
|
||||
'cross_selling_match_products',
|
||||
]
|
||||
widgets = {
|
||||
'description': I18nMarkdownTextarea,
|
||||
'cross_selling_condition': RadioSelect,
|
||||
}
|
||||
field_classes = {
|
||||
'cross_selling_match_products': SafeModelMultipleChoiceField,
|
||||
}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
tpl = '{} <span class="text-muted">{}</span>'
|
||||
self.fields['category_type'] = ChoiceField(widget=RadioSelect, choices=(
|
||||
('normal', mark_safe(tpl.format(
|
||||
_('Normal category'),
|
||||
_('Products in this category are regular products displayed on the front page.')
|
||||
)),),
|
||||
('addon', mark_safe(tpl.format(
|
||||
_('Add-on product category'),
|
||||
_('Products in this category are add-on products and can only be bought as add-ons.')
|
||||
)),),
|
||||
('only', mark_safe(tpl.format(
|
||||
_('Cross-selling category'),
|
||||
_('Products in this category are regular products, but are only shown in the cross-selling step, '
|
||||
'according to the configuration below.')
|
||||
)),),
|
||||
('both', mark_safe(tpl.format(
|
||||
_('Normal + cross-selling category'),
|
||||
_('Products in this category are regular products displayed on the front page, but are additionally '
|
||||
'shown in the cross-selling step, according to the configuration below.')
|
||||
)),),
|
||||
))
|
||||
self.fields['category_type'].initial = self.instance.category_type
|
||||
|
||||
self.fields['cross_selling_condition'].widget.attrs['data-display-dependency'] = '#id_category_type_2,#id_category_type_3'
|
||||
self.fields['cross_selling_condition'].widget.attrs['data-disable-dependent'] = 'true'
|
||||
self.fields['cross_selling_condition'].widget.choices = self.fields['cross_selling_condition'].widget.choices[1:]
|
||||
self.fields['cross_selling_condition'].required = False
|
||||
|
||||
self.fields['cross_selling_match_products'].widget = forms.CheckboxSelectMultiple(
|
||||
attrs={
|
||||
'class': 'scrolling-multiple-choice',
|
||||
'data-display-dependency': '#id_cross_selling_condition_2'
|
||||
}
|
||||
)
|
||||
self.fields['cross_selling_match_products'].queryset = self.event.items.filter(
|
||||
# don't show products which are only visible in addon/cross-sell step themselves
|
||||
Q(category__isnull=True) | Q(
|
||||
Q(category__is_addon=False) & Q(Q(category__cross_selling_mode='both') | Q(category__cross_selling_mode__isnull=True))
|
||||
)
|
||||
)
|
||||
|
||||
def clean(self):
|
||||
d = super().clean()
|
||||
if d.get('category_type') == 'only' or d.get('category_type') == 'both':
|
||||
if not d.get('cross_selling_condition'):
|
||||
raise ValidationError({'cross_selling_condition': [_('This field is required')]})
|
||||
self.instance.category_type = d.get('category_type')
|
||||
return d
|
||||
|
||||
|
||||
class QuestionForm(I18nModelForm):
|
||||
|
||||
Reference in New Issue
Block a user