feat: added product variants
This commit is contained in:
+21
-4
@@ -12,14 +12,29 @@ from django.utils import timezone
|
||||
from django.utils.text import gettext_lazy as _
|
||||
|
||||
from shop.exceptions import RedsysPaymentException, RedsysValidationException
|
||||
from shop.models import Cart, CartItem, Order, OrderLine, Payment, Product, ProductBatch, ProductPrice, ShippingMethod
|
||||
from shop.models import (
|
||||
Cart,
|
||||
CartItem,
|
||||
Order,
|
||||
OrderLine,
|
||||
Payment,
|
||||
Product,
|
||||
ProductBatch,
|
||||
ProductPrice,
|
||||
ProductVariant,
|
||||
ShippingMethod,
|
||||
)
|
||||
from shop.settings import ERROR_CODES
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
def create_order_line_for_product(product: Product, quantity: Decimal, order: Order):
|
||||
price = product.prices.last()
|
||||
def get_effective_price(product: Product, variant: ProductVariant = None) -> ProductPrice:
|
||||
return variant.price if variant else product.price
|
||||
|
||||
|
||||
def create_order_line_for_product(product: Product, quantity: Decimal, order: Order, variant: ProductVariant = None):
|
||||
price = get_effective_price(product, variant)
|
||||
base_total = round(price.price * quantity, 2)
|
||||
tax_value = price.tax.value / Decimal('100')
|
||||
taxes = round(base_total * tax_value, 2)
|
||||
@@ -27,6 +42,7 @@ def create_order_line_for_product(product: Product, quantity: Decimal, order: Or
|
||||
return OrderLine.objects.create(
|
||||
order=order,
|
||||
product=product,
|
||||
variant=variant,
|
||||
quantity=quantity,
|
||||
price=price.price,
|
||||
base_total=base_total,
|
||||
@@ -111,7 +127,7 @@ def create_order_from_cart(
|
||||
|
||||
for item in cart.items.all():
|
||||
product = item.product
|
||||
price: ProductPrice = product.price
|
||||
price: ProductPrice = item.price
|
||||
base_total = price.price * item.quantity
|
||||
tax_value = price.tax.value
|
||||
taxes = base_total * tax_value / Decimal(100)
|
||||
@@ -120,6 +136,7 @@ def create_order_from_cart(
|
||||
OrderLine.objects.create(
|
||||
order=order,
|
||||
product=product,
|
||||
variant=item.variant,
|
||||
quantity=item.quantity,
|
||||
price=price.price,
|
||||
base_total=base_total,
|
||||
|
||||
Reference in New Issue
Block a user