test of in cart quantity
This commit is contained in:
@@ -65,7 +65,12 @@ class ProductVariantSerialzier(serializers.ModelSerializer):
|
|||||||
request = self.context.get('request')
|
request = self.context.get('request')
|
||||||
if not request or not request.user.is_authenticated:
|
if not request or not request.user.is_authenticated:
|
||||||
return 0
|
return 0
|
||||||
return 1
|
cart_items = self.context.get('cart_items', [])
|
||||||
|
if cart_items:
|
||||||
|
for item in cart_items:
|
||||||
|
if item['product']['id'] == obj.id:
|
||||||
|
return item['quantity']
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ from rest_framework.permissions import IsAuthenticatedOrReadOnly
|
|||||||
from utils.pagination import StructurePagination
|
from utils.pagination import StructurePagination
|
||||||
from drf_spectacular.utils import extend_schema, OpenApiParameter, OpenApiTypes
|
from drf_spectacular.utils import extend_schema, OpenApiParameter, OpenApiTypes
|
||||||
from rest_framework.permissions import AllowAny
|
from rest_framework.permissions import AllowAny
|
||||||
|
from order.serializers import OrderItemSerailzier
|
||||||
|
|
||||||
# class APIView(APIView):
|
# class APIView(APIView):
|
||||||
# def __init__(self, *args, **kwargs):
|
# def __init__(self, *args, **kwargs):
|
||||||
@@ -54,7 +54,10 @@ class ProductView(APIView):
|
|||||||
# authentication_classes = []
|
# authentication_classes = []
|
||||||
def get(self, request, pk):
|
def get(self, request, pk):
|
||||||
product = get_object_or_404(ProductModel, id=pk)
|
product = get_object_or_404(ProductModel, id=pk)
|
||||||
product_ser = self.serializer_class(instance=product, many=False, context={'request': request, 'view_type': 'instance'})
|
cart_obj, _ = OrderModel.objects.get_or_create(user=request.user, status='CART')
|
||||||
|
cart_items = cart_obj.items.all()
|
||||||
|
cart_items_ser = OrderItemSerailzier(cart_items, many=True)
|
||||||
|
product_ser = self.serializer_class(instance=product, many=False, context={'request': request, 'view_type': 'instance', 'cart_items': cart_items_ser.data})
|
||||||
return Response(product_ser.data, status=status.HTTP_200_OK)
|
return Response(product_ser.data, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user