Skip to content

CartItemSerializer Documentation

This section outlines the serializer used in the CartItem API. The CartItemSerializer is responsible for transforming the data between complex types (e.g., Django models) and Python data types (e.g., JSON), allowing the creation, update, and retrieval of cart items in a shopping cart.


Serializers

  1. CartItem Serializer: CartItemSerializer

1. CartItem Serializer

Serializer Overview

CartItemSerializer is used to serialize cart item data. A cart item represents an individual product added to the shopping cart, with details such as product information, quantity, pricing, discounts, and variants.

Fields

Field Type Description
id integer The unique identifier for the cart item.
cart object The cart to which this item belongs.
product object Details of the product in the cart, including name, price, and images.
variant object Details of the variant of the product (if applicable).
quantity integer The quantity of the product added to the cart.
total_price decimal The total price of the cart item (quantity * unit price).
discount_amount decimal The amount of discount applied to the cart item.
final_price decimal The final price after applying the discount.
is_customized boolean Indicates whether the product is customized.
customized_fields JSON The fields representing customization options, stored as a JSON object.
is_direct_purchase boolean Indicates whether the product is for direct purchase.
has_purchased boolean Indicates whether the item has been purchased.
created_at datetime The timestamp when the cart item was created.
updated_at datetime The timestamp when the cart item was last updated.

Example Response

Response

{
            "id": 471,
            "product": {
                "id": 483,
                "name": "Product 2",
                "discount_percent": 0.0,
                "description": "lina aaba kehi xaina",
                "images": [
                    {
                        "id": 110,
                        "image": "http://127.0.0.1:8000/media/product/image/SAMAYA_LOGO_Ij0iQsY.png"
                    },
                    {
                        "id": 111,
                        "image": "http://127.0.0.1:8000/media/product/image/SAMAYA_LOGO_niAb0ee.png"
                    },
                    {
                        "id": 112,
                        "image": "http://127.0.0.1:8000/media/product/image/SAMAYA_LOGO_bW1g2Ar.png"
                    },
                    {
                        "id": 113,
                        "image": "http://127.0.0.1:8000/media/product/image/SAMAYA_LOGO_37uXex8.png"
                    },
                    {
                        "id": 114,
                        "image": "http://127.0.0.1:8000/media/product/image/SAMAYA_LOGO_RDiRngt.png"
                    }
                ],
                "primary_image": "http://127.0.0.1:8000/media/product/image/SAMAYA_LOGO_Ij0iQsY.png",
                "shop_details": {
                    "id": 1,
                    "name": "G store",
                    "description": "damii chhha hai",
                    "location": "koteshwor kathmandu",
                    "image": "http://127.0.0.1:8000/media/shop/images/C1_Zq7po3q.jpeg",
                    "cover_image": "http://127.0.0.1:8000/media/shop/images/C2_toyrSD5.jpeg"
                }
            },
            "variant": {
                "id": 978,
                "variant_price": 200.0,
                "discounted_price": 200.0,
                "stock_quantity": 102,
                "is_available": true,
                "is_active": true,
                "variant_images": [
                    {
                        "id": 113,
                        "image": "http://127.0.0.1:8000/media/product/image/SAMAYA_LOGO_37uXex8.png",
                        "is_primary": false
                    },
                    {
                        "id": 114,
                        "image": "http://127.0.0.1:8000/media/product/image/SAMAYA_LOGO_RDiRngt.png",
                        "is_primary": false
                    }
                ],
                "size": {
                    "id": 1,
                    "size": "Medium",
                    "value": "M"
                }
            },
            "quantity": 2,
            "price": 200.0,
            "total_price": 400.0,
            "discount_amount": 0.0,
            "final_price": 200.0,
            "is_direct_purchase": false,
            "has_purchased": false,
            "created_at": "03-10-2025 11:00:34",
            "updated_at": "03-10-2025 11:00:47",
            "is_customized": false,
            "customized_price": null,
            "customized_fields": null
        }
    ],
    "subtotal": 400.0,
    "total_discount": 0.0,
    "grand_total": 400.0,
    "total_items": 1,
    "created_at": "02-25-2025 04:07:32",
    "updated_at": "02-25-2025 04:07:32"
}


Serializer Methods

  • CartItemSerializer.get_product(self, obj)

This method retrieves and serializes the product details related to the cart item, including product images, price, discount, and description.

  • CartItemSerializer.get_variant(self, obj)

This method retrieves and serializes the variant details, including variant price, discounted price, stock quantity, and size (if available). It calculates the discounted price based on the product discount.

  • CartItemSerializer.get_total_price(self, obj)

This method calculates and returns the total price of the cart item (unit price * quantity).

  • CartItemSerializer.get_discount_amount(self, obj)

This method calculates and returns the discount amount for the cart item.

  • CartItemSerializer.get_final_price(self, obj)

This method calculates and returns the final price after applying the discount to the total price. The final price is never negative, so the method ensures the value is at least 0.00.