feat: added product variants
This commit is contained in:
+21
-5
@@ -7,7 +7,16 @@ from django.utils.text import gettext_lazy as _
|
||||
from django.views.decorators.http import require_http_methods
|
||||
from django.views.generic import CreateView, TemplateView
|
||||
|
||||
from shop.models import CartItem, Order, OrderLine, Product, ProductCategory, ShippingMethod, WishlistedProduct
|
||||
from shop.models import (
|
||||
CartItem,
|
||||
Order,
|
||||
OrderLine,
|
||||
Product,
|
||||
ProductCategory,
|
||||
ProductVariant,
|
||||
ShippingMethod,
|
||||
WishlistedProduct,
|
||||
)
|
||||
from shop.redsys import RedsysClient
|
||||
from shop.utils import create_order_from_cart
|
||||
from web.forms import CreateOrderForm
|
||||
@@ -37,7 +46,7 @@ class CategoryView(TemplateView):
|
||||
def get_context_data(self, **kwargs):
|
||||
settings = WebSettings.load()
|
||||
slug = kwargs.get('slug')
|
||||
category = ProductCategory.objects.get(slug=slug)
|
||||
category = get_object_or_404(ProductCategory, slug=slug)
|
||||
|
||||
return {
|
||||
'title': _(f'{settings.web_title} - {category.name}'),
|
||||
@@ -53,9 +62,11 @@ class ProductDetail(TemplateView):
|
||||
def get_context_data(self, pk, slug, **kwargs):
|
||||
settings = WebSettings.load()
|
||||
product = get_object_or_404(Product, pk=pk)
|
||||
variants = product.variants.prefetch_related('attribute_values__attribute')
|
||||
|
||||
return {
|
||||
'product': product,
|
||||
'variants': variants,
|
||||
'title': f'{settings.web_title} - {product.name}',
|
||||
'description': product.description,
|
||||
'image': product.images.first(),
|
||||
@@ -201,14 +212,19 @@ class OrdersView(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin):
|
||||
@require_http_methods(['POST'])
|
||||
def add_cart_item(request, *args, **kwargs):
|
||||
product = get_object_or_404(Product, pk=request.POST.get('product'))
|
||||
|
||||
variant = None
|
||||
if product.has_variants:
|
||||
variant = get_object_or_404(ProductVariant, pk=request.POST.get('variant'), product=product)
|
||||
|
||||
cart, created = get_or_create_cart(request)
|
||||
response = HttpResponse(status=201, headers={'HX-Trigger': 'updated-cart'})
|
||||
|
||||
if created and request.user.is_anonymous:
|
||||
response.set_cookie(ANONYMOUS_CART_ID_COOKIE_NAME, cart.uuid, samesite='strict')
|
||||
|
||||
# Comprobamos si existe una línea de carrito para ese carrito de ese producto
|
||||
existing_cart_item = CartItem.objects.filter(cart=cart, product=product).first()
|
||||
# Comprobamos si existe una línea de carrito para ese carrito de ese producto/variante
|
||||
existing_cart_item = CartItem.objects.filter(cart=cart, product=product, variant=variant).first()
|
||||
|
||||
# Si existe, simplemente le sumamos la cantidad a la línea ya existente
|
||||
if existing_cart_item is not None:
|
||||
@@ -216,7 +232,7 @@ def add_cart_item(request, *args, **kwargs):
|
||||
existing_cart_item.save()
|
||||
# Si no, lo creamos
|
||||
else:
|
||||
CartItem.objects.create(cart=cart, quantity=request.POST.get('quantity'), product=product)
|
||||
CartItem.objects.create(cart=cart, quantity=request.POST.get('quantity'), product=product, variant=variant)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
Reference in New Issue
Block a user