Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Color based variants for products
Artur Smętbb1f690348de
diff --git a/saleor/product/forms.py b/saleor/product/forms.pyindex 3516a6eb86..6267ae4bbc 100644--- a/saleor/product/forms.py+++ b/saleor/product/forms.py@@ -6,32 +6,19 @@ from .models import Bag, BagVariant, Shirt, ShirtVariant, Colorclass BagForm(AddToCartForm):- color = forms.ModelChoiceField(queryset=None)-- def __init__(self, *args, **kwargs):- super(BagForm, self).__init__(*args, **kwargs)- color_field = self.fields['color']- color_field.queryset = self.product.get_available_colors()-def get_variant(self, clean_data):- color = clean_data.get('color')- return BagVariant.objects.get(color=color)+ return BagVariant.objects.get(product__color=self.product.color)class ShirtForm(AddToCartForm):- color = forms.ModelChoiceField(queryset=None)- size = forms.ChoiceField(choices=ShirtVariant.SIZE_CHOICES)+ size = forms.ChoiceField(choices=ShirtVariant.SIZE_CHOICES,+ widget=forms.RadioSelect())def get_variant(self, clean_data):- color = clean_data.get('color')size = clean_data.get('size')- return ShirtVariant.objects.get(size=size, color=color)-- def __init__(self, *args, **kwargs):- super(ShirtForm, self).__init__(*args, **kwargs)- color_field = self.fields['color']- color_field.queryset = self.product.get_available_colors()+ return ShirtVariant.objects.get(size=size,+ product__color=self.product.color)def get_form_class_for_product(product):diff --git a/saleor/product/models.py b/saleor/product/models.pyindex ce3bf8846a..1f7b21ec0b 100644--- a/saleor/product/models.py+++ b/saleor/product/models.py@@ -43,6 +43,13 @@ class Category(MPTTModel):verbose_name_plural = 'categories'+class ProductCollection(models.Model):+ name = models.CharField(max_length=100, db_index=True, unique=True)++ def __str__(self):+ return self.name++class Product(Subtyped, ItemRange):name = models.CharField(@@ -51,6 +58,8 @@ class Product(Subtyped, ItemRange):Category, verbose_name=pgettext_lazy('Product field', 'category'),related_name='products')+ collection = models.ForeignKey(ProductCollection)+def __unicode__(self):return self.name@@ -64,6 +73,9 @@ class Product(Subtyped, ItemRange):return mark_safe(re.sub(r'[-\s]+', '-', value))+ def get_products_from_collection(self):+ return Product.objects.filter(collection=self.collection)+def __iter__(self):return iter(self.variants.all())@@ -205,24 +217,20 @@ class ColoredVariants(models.Model):abstract = True-class Bag(Product, PhysicalProduct):-- def get_available_colors(self):- return Color.objects.filter(bagvariant__in=self.variants.all())-+class Bag(Product, PhysicalProduct, ColoredVariants):+ pass-class Shirt(Product, PhysicalProduct):- def get_available_colors(self):- return Color.objects.filter(shirtvariant__in=self.variants.all())+class Shirt(Product, PhysicalProduct, ColoredVariants):+ pass-class BagVariant(ProductVariant, StockedProduct, ColoredVariants):+class BagVariant(ProductVariant, StockedProduct):product = models.ForeignKey(Bag, related_name='variants')-class ShirtVariant(ProductVariant, StockedProduct, ColoredVariants):+class ShirtVariant(ProductVariant, StockedProduct):SIZE_CHOICES = (('xs', pgettext_lazy('Variant size', 'XS')),@@ -234,6 +242,3 @@ class ShirtVariant(ProductVariant, StockedProduct, ColoredVariants):product = models.ForeignKey(Shirt, related_name='variants')size = models.CharField(choices=SIZE_CHOICES, max_length=3)-- class Meta:- unique_together = ('color', 'size')