Remove Order line Item using update Api

My Request
{
“order”: {
“state”: “OPEN”,
“source”: {
“name”: “CaterZen”
},
“reference_id”: “1678034”,
“location_id”: “LNMMT9D8WJNMC”,
“customer_id”: “YMWPBJDXGYV3T2DFCWHS8DA76G”,
“line_items”: [
{
“quantity”: “10”,
“name”: “1/2 1/2 TEA”,
“base_price_money”: {
“amount”: 279,
“currency”: “USD”
},
“modifiers”: [
{
“uid”: “DE3QYJCDO4SBCZLQW5UOJWZY”,
“name”: “DIET”,
“base_price_money”: {
“amount”: 2000,
“currency”: “USD”
},
“catalog_object_id”: “DE3QYJCDO4SBCZLQW5UOJWZY”
}
],
“variation”: {
“uid”: “Z6FBA6DCAJAMNFHSEVBHAUIT”,
“name”: “Small”
},
“note”: “”,
“catalog_object_id”: “Z6FBA6DCAJAMNFHSEVBHAUIT”,
“uid”: “AFOYDFJDJYHRUEF27BTD4NK7”,
“variation_name”: “Small”
}
],
“discounts”: [
{
“name”: “10% discount”,
“percentage”: “10”,
“type”: “FIXED_PERCENTAGE”,
“scope”: “ORDER”,
“uid”: “145670”
}
],
“taxes”: ,
“metadata”: {
“note”: “CaterZen Order”
},
“version”: 1,
“fulfillments”: [
{
“type”: “PICKUP”,
“pickup_details”: {
“recipient”: {
“customer_id”: “YMWPBJDXGYV3T2DFCWHS8DA76G”
},
“state”: “PROPOSED”,
“pickup_at”: “2024-08-07T22:00:00-07:00”
},
“uid”: “S0GNh9jh2pQ0PV0a62bZVD”
}
]
},
“fields_to_clear”: [
“line_items”
]
}

Response

{
“status”: 400,
“reason”: “Bad Request”,
“error”: {
“errors”: [
{
“code”: “INVALID_VALUE”,
“detail”: “Read-only field is calculated and cannot be set by a client.”,
“field”: “order.line_items[AFOYDFJDJYHRUEF27BTD4NK7].gross_sales_money”,
“category”: “INVALID_REQUEST_ERROR”
}
]
}
}

I add 2 line line item and place order.
then i remove last line item i got this error.Please anyone solve this issue??

:wave: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Update Orders
Order Fulfillments
Clear API Object Fields

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

I am already check this this document .but no improvements

The error message indicates that you’re trying to modify a read-only field, specifically the gross_sales_money field under order.line_items. This field is calculated by Square and cannot be directly modified by a client.

When you’re trying to update an order, you should only include the fields that you want to change in the request body. If you want to remove a line item, you should not include the gross_sales_money field in your request.

If you want to remove a line item from an order, you should use the UpdateOrder endpoint and specify the line item to be removed in the fields_to_clear array. The value should be line_items.<uid>, where <uid> is the unique identifier of the line item you want to remove.

In your case, if you want to remove the last line item, your fields_to_clear array should look like this:

"fields_to_clear": [
    "line_items.AFOYDFJDJYHRUEF27BTD4NK7"
]

This tells Square to remove the line item with the uid AFOYDFJDJYHRUEF27BTD4NK7 from the order.

This answer was reviewed by @Bryan-Square.

1 Like