Adding Variable Products to Cart

When adding a variation from a variable product to the cart, there are several approaches you can take. Below are examples of each method and their outcomes.

Method 1: No Attribute Data

To make things clear from the start. Adding a variable product without attribute data will fail as the variation cannot be determined. This method is not recommended.
{
    "id": "16",
    "quantity": "1"
}

```json
{
    "id": "16",
    "quantity": "1"
}
Response:
{
    "code": "cocart_no_variation_found",
    "message": "No matching variation found.",
    "data": {
        "status": 404
    }
}

Method 2: Using Attribute Data

Note the lowercase “no”
{
    "id": "16",
    "quantity": "1",
    "variation": {
        "attribute_pa_color": "green",
        "attribute_logo": "no"
    }
}
Response
{
    "code": "cocart_no_variation_found",
    "message": "No matching variation found.",
    "data": {
        "status": 404
    }
}

Method 3: Using Variation ID

You can directly add a specific variation using its ID.
{
    "id": "39",
    "quantity": "1"
}
However, if you specify an invalid attribute with the variation ID you will receive an error. The error will provide the allowed values.
{
    "id": "39",
    "quantity": "1",
    "variation": {
        "attribute_pa_color": "orange",
        "attribute_logo": "Yes"
    }
}
Response
{
    "code": "cocart_invalid_variation_data",
    "message": "Invalid value posted for Color. Allowed values: blue, green, red",
    "data": {
        "status": 400
    }
}

Method 4: Using Alternative Attribute Formats

You can use attribute labels instead of slugs.
{
    "id": "16",
    "quantity": "1",
    "variation": {
        "Color": "blue",
        "Logo": "Yes"
    }
}

Important Notes

  1. Attribute values are case-sensitive
  2. When using variation IDs, attribute data is optional
  3. Custom attribute labels must match exactly
  4. Values maybe limited to specific options
  5. You can use either attribute slugs (attribute_pa_color) or labels (Color)