feat: added product variants
This commit is contained in:
+18
-1
@@ -2,7 +2,7 @@ from decimal import Decimal
|
||||
|
||||
from django.utils.timezone import now
|
||||
|
||||
from shop.models import Product, ProductPrice, Tax
|
||||
from shop.models import Product, ProductAttribute, ProductAttributeValue, ProductPrice, ProductVariant, Tax
|
||||
|
||||
|
||||
class CreateProductsMixin:
|
||||
@@ -13,3 +13,20 @@ class CreateProductsMixin:
|
||||
product = Product.objects.create(sku=sku, name=name, description=description, is_shipping_method=is_shipping)
|
||||
ProductPrice.objects.create(price=price, product=product, date=now(), tax=tax, current=True)
|
||||
return product
|
||||
|
||||
def create_attribute_value(self, attribute_name='Talla', value='M') -> ProductAttributeValue:
|
||||
attribute, created = ProductAttribute.objects.get_or_create(name=attribute_name)
|
||||
attribute_value, created = ProductAttributeValue.objects.get_or_create(attribute=attribute, value=value)
|
||||
return attribute_value
|
||||
|
||||
def create_product_variant(
|
||||
self, product: Product, sku='000001-V1', price=Decimal('10.00'), stock=Decimal('10'), attribute_values=None
|
||||
) -> ProductVariant:
|
||||
tax, created = Tax.objects.get_or_create(code='IVA', value=21)
|
||||
variant = ProductVariant.objects.create(product=product, sku=sku, stock=stock)
|
||||
|
||||
if attribute_values:
|
||||
variant.attribute_values.set(attribute_values)
|
||||
|
||||
ProductPrice.objects.create(price=price, variant=variant, date=now(), tax=tax, current=True)
|
||||
return variant
|
||||
|
||||
Reference in New Issue
Block a user