Cart API - Add to Cart
This API allows users to add items to their cart, including handling product variants and stock quantity validation.
Endpoints Overview
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/v1/cart/add/<str:product_id>/ |
Add an item to the user's cart. |
Cart API - Add to Cart
1. Add Item to Cart
Endpoint
- URL:
/api/v1/cart/add/<str:product_id>/ - Method:
POST - Permission Required:
IsAuthenticated
Description
Adds an item to the authenticated user's cart. The request must provide the product variant and quantity. If the item already exists in the cart, its quantity will be updated. If no variant is specified, the most available variant will be selected.
Request Parameters
product_id(Required): The ID of the product to add to the cart.
Request Body
variant_id(Optional): The ID of the product variant to add to the cart.quantity(Optional): The quantity of the product to add to the cart. Defaults to 1 if not provided.is_customized(Optional): Boolean flag to indicate if the product should be customized. Defaults to false.customized_fields(Optional, if is_customized is true): A dictionary of fields for customization, such as measurements or text.
Example Request
This is the endpoint
POST /api/v1/cart/add/22/
Request Body
This is the request body without customization
{
"variant_id": 10,
"quantity": 2,
"is_customized": false
}
Request Body
This is the request body with customization
{
"variant_id": 10,
"quantity": 2,
"is_customized": true,
"customized_fields": {
"waist_size": "30",
"hip_size": "40"
}
}
Success
{
"id": 28,
"product": {
"id": 34,
"name": "New Product",
"price": 222.0,
"discount_percent": 22.0,
"description": "xcvxc",
"images": [
{
"id": 19,
"image": "/media/product/image/gg_pt1x1sS.png"
},
{
"id": 20,
"image": "/media/product/image/1_ss_gTsD8uy.png"
}
],
"primary_image": "/media/product/image/gg_pt1x1sS.png"
},
"variant": {
"id": 76,
"variant_price": 222.0,
"discounted_price": 173.16,
"stock_quantity": 32,
"is_available": true,
"is_active": true,
"size": {
"id": 2,
"size": "Small",
"value": "S"
}
},
"quantity": 1,
"total_price": 222.0,
"discount_amount": 48.84,
"final_price": 173.16,
"is_direct_purchase": false,
"has_purchased": false,
"created_at": "12-16-2024 09:05:33",
"updated_at": "12-16-2024 09:05:33"
}
Error
{
"error": "No available variants with stock for this product."
}
Error
{
"error": "Quantity must be greater than 0"
}