# Add to Cart

Add a product to the cart.

```nolang
POST /v2/cpq/carts/{cart_ID}/items
```

:::note
By default, items deleted from a cart are not evaluated for Vlocity Rules. To enable evaluation of deleted items, set the ShouldConsiderDeletedItems configuration setting to true. CME packages are large.
:::

This API supports the guest user enhancements that Salesforce introduced with the Winter ‘21 release. To encrypt and decrypt data for guest users, use the `UserSecurity` class with this API. See Guest User Technical Details.

For additional information, ​see UserSecurity Class and CPQ and Digital Commerce Changes for Guest Users.

## Remote Method

```nolang
postCartsItems
```

:::note
The postCartsItems method supports the addition of one item at a time. If you must add multiple items, make multiple postCartsItems calls.
:::

## REST Handler

```nolang
APIItemsCartsCpqV2
```

## Apex Remote Service

```nolang
Class: vlocity_cmt.CardCanvasController
Method: global static Object doGenericInvoke(String sClassName, String sMethodName, String input, String options)
```

## Apex Remote Input Map

The following example illustrates how to call this API from Apex code.

```nolang
"sClassName": "vlocity_cmt.CpqAppHandler"
"sMethodName": "postCartsItems"
"input":  SAME AS SAMPLE REQUEST BODY
"options": {"vlcClass":"vlocity_cmt.CpqAppHandler"}
```

```nolang
Map<String, Object> inputMap = new Map<String, Object>();
Map<String, Object> outputMap = new Map<String, Object>();
Map<String, Object> optionsMap = new Map<String, Object>();
Map<String, Object> itemsMap = new Map<String, Object>();
List<Object> items = new List<Object>();
Id priceBookEntryId = Id.valueOf('Replace with the pricebook entry id of the product to be added');
Id cartId = Id.valueOf('replace with the id of the cart');
itemsMap.put('itemId', priceBookEntryId);
items.add(itemsMap);
inputMap.put('items', items);
inputMap.put('cartId', cartId);
inputMap.put('validate', true); // set to false if validation is not required
inputMap.put('price', true); // set to false if pricing is not required
vlocity_cmt.CpqAppHandler cpqAppHandlerService = new  vlocity_cmt.CpqAppHandler();
cpqAppHandlerService.invokeMethod('postCartsItems', inputMap, outputMap, optionsMap);
System.debug('outputMap:' + outputMap);
```

## Package

Communication (vlocity_cmt)

## API Parameters and Response Format

For API parameter names and descriptions, see [Cart-Based API Swagger Reference](https://developer.salesforce.com/docs/industries/cme/references/cpq?meta=Summary).

## Resource Information

| Response Format | JSON                                                                                                 |
| --------------- | ---------------------------------------------------------------------------------------------------- |
| Resource URL    | `/v2/cpq/carts/*/items` Example: `/services/apexrest/{namespace}/v2/cpq/carts/801360000000oQ9/items` |

## Example Request

```nolang
{
  "methodName": "postCartsItems",
  "items": [
    {
      "parentId": "8021N000006wtTTQAY",
      "parentHierarchyPath": "01to000000Am1FGAAZ",
      "itemId": "01u1N00000DPpdFQAT",
      "parentRecord": {
        "records": [
          {
            "displaySequence": -1,
            "Id": {
              "value": "8021N000006wtTTQAY",
              "previousValue": null,
              "originalValue": null,
              "messages": [],
              "label": "Order Product ID",
              "hidden": false,
              "fieldName": "Id",
              "editable": false,
              "dataType": "ID",
              "actions": {}
            },
            "Pricebook2Id": "01so0000000jvwiAAA",
            "Product2Id": "01to000000Am1FGAAZ",
            "UnitPrice": {
              "value": 0,
              "previousValue": null,
              "originalValue": null,
              "messages": [],
              "label": "Unit Price",
              "hidden": true,
              "fieldName": "UnitPrice",
              "editable": false,
              "dataType": "CURRENCY",
              "actions": {}
            },
            "Name": "Product Hier 3",
            "vlocity_cmt__RecurringPrice__c": 0,
            "IsActive": true,
            "Product2": {
              "attributes": {
                "type": "Product2",
                "url": "/services/data/v41.0/sobjects/Product2/01to000000Am1FGAAZ"
              },
              "Name": "Product Hier 3",
              "vlocity_cmt__IsConfigurable__c": false,
              "RecordTypeId": "012o0000000pDXFAA2",
              "Id": "01to000000Am1FGAAZ",
              "vlocity_cmt__JSONAttribute__c": null
            },
            "productId": "01to000000Am1FGAAZ",
            "defaultQuantity": 1,
            "minQuantity": 0,
            "maxQuantity": 1000,
            "groupMinQuantity": 0,
            "groupMaxQuantity": 99999,
            "sequenceNumber": 1,
            "productChildItemDefinition": {
              "attributes": {
                "type": "vlocity_cmt__ProductChildItem__c",
                "url": "/services/data/v41.0/sobjects/vlocity_cmt__ProductChildItem__c/a0ro00000050PjxAAE"
              },
              "Id": "a0ro00000050PjxAAE",
              "Name": "Root PCI",
              "vlocity_cmt__IsVirtualItem__c": false,
              "vlocity_cmt__Quantity__c": 1,
              "vlocity_cmt__MaxQuantity__c": 99999,
              "vlocity_cmt__MinQuantity__c": 0,
              "vlocity_cmt__MaximumChildItemQuantity__c": 99999,
              "vlocity_cmt__MinimumChildItemQuantity__c": 0,
              "vlocity_cmt__IsOverride__c": false,
              "vlocity_cmt__ParentProductId__c": "01to000000Am1FGAAZ",
              "vlocity_cmt__ChildLineNumber__c": "1",
              "vlocity_cmt__SeqNumber__c": 1,
              "vlocity_cmt__CollapseHierarchy__c": false,
              "vlocity_cmt__IsRootProductChildItem__c": true,
              "vlocity_cmt__ParentProductId__r": {
                "attributes": {
                  "type": "Product2",
                  "url": "/services/data/v41.0/sobjects/Product2/01to000000Am1FGAAZ"
                },
                "Name": "Product Hier 3",
                "Id": "01to000000Am1FGAAZ",
                "RecordTypeId": "012o0000000pDXFAA2"
              }
            },
            "productHierarchyPath": "01to000000Am1FGAAZ",
            "name": "Product Hier 3",
            "isVirtualItem": false,
            "provisioningStatus": "New",
            "hasChildren": true,
            "itemType": "lineItem",
            "PricebookEntryId": {
              "value": "01uo000000Bz2qbAAB",
              "previousValue": null,
              "originalValue": null,
              "messages": [],
              "label": "Price Book Entry ID",
              "hidden": false,
              "fieldName": "PricebookEntryId",
              "editable": false,
              "dataType": "REFERENCE",
              "actions": {}
            },
            "Quantity": {
              "value": 1,
              "previousValue": null,
              "originalValue": null,
              "messages": [],
              "label": "Quantity",
              "hidden": false,
              "fieldName": "Quantity",
              "editable": true,
              "dataType": "DOUBLE",
              "actions": {}
            },
            "vlocity_cmt__OneTimeTotal__c": {
              "value": 300,
              "previousValue": null,
              "originalValue": null,
              "messages": [],
              "label": "One Time Total",
              "hidden": false,
              "fieldName": "vlocity_cmt__OneTimeTotal__c",
              "editable": false,
              "dataType": "CURRENCY",
[EXAMPLE TRUNCATED FOR BREVITY]
```

## Example Response

```nolang
{
  "totalSize": 1,
  "messages": [
    {
      "severity": "INFO",
      "messageId": null,
      "message": "Successfully added.",
      "code": "150",
      "bundleId": null,
      "actions": {}
    }
  ],
  "actions": {
    "addtocart": {
      "rest": {
        "params": {
          "cartId": "8011N00000157LkQAI",
          "items": [
            {
              "isPartialPromo": true,
              "itemId": "a2w1N000001F9TiQAK"
            }
          ]
        },
        "method": "POST",
        "link": "/services/apexrest/vlocity_cmt/v2/cpq/carts/8011N00000157LkQAI/promotions"
      },
      "remote": {
        "params": {
          "items": [
            {
              "isPartialPromo": true,
              "itemId": "a2w1N000001F9TiQAK"
            }
          ],
          "cartId": "8011N00000157LkQAI",
          "methodName": "postCartsPromoItems"
        }
      },
      "client": {
        "params": {
          "methodName": "postCartsPromoItems",
          "items": [
            {
              "isPartialPromo": true,
              "itemId": "a2w1N000001F9TiQAK"
            }
          ]
        }
      }
    },
    "itempricesupdated": {
      "rest": {
        "params": {},
        "method": "GET",
        "link": "/services/apexrest/vlocity_cmt/v2/cpq/carts/8011N00000157LkQAI/price"
      },
      "remote": {
        "params": {
          "cartId": "8011N00000157LkQAI",
          "methodName": "getCartLineItemPrices"
        }
      },
      "client": {
        "params": {}
      }
    }
  },
  "records": [
    {
      "messages": [],
      "displaySequence": -1,
      "vlocity_cmt__InCartQuantityMap__c": {
        "value": {
          "01t1N00000AptOXQAZ": 1,
          "01t1N00000AptOcQAJ": 1,
          "01t1N00000ApqNdQAJ": 1,
          "01t1N00000ByiiDQAR": 1
        },
        "previousValue": null,
        "originalValue": null,
        "messages": [],
        "label": "InCartQuantityMap",
        "hidden": true,
        "fieldName": "vlocity_cmt__InCartQuantityMap__c",
        "editable": false,
        "dataType": "TEXTAREA",
        "actions": {}
      },
      "Id": {
        "value": "8021N000006wtTTQAY",
        "previousValue": null,
        "originalValue": null,
        "messages": [],
        "label": "Order Product ID",
        "hidden": false,
        "fieldName": "Id",
        "editable": false,
        "dataType": "ID",
        "actions": {}
      },
      "isVirtualItem": false,
      "lineItems": {
        "totalSize": 0,
        "messages": [],
        "records": [
          {
            "messages": [],
            "actions": {
              "addtocart": {
                "rest": {
                  "params": {
                    "items": [
                      {
                        "quantity": 1,
                        "parentId": "8021N000006wtTTQAY",
                        "parentHierarchyPath": "01to000000Am1FGAAZ",
                        "itemId": "01u1N00000DPpdFQAT"
                      }
                    ],
                    "cartId": "8011N00000157LkQAI",
                    "price": true,
                    "validate": true,
                    "includeAttachment": false,
                    "pagesize": 10,
                    "lastRecordId": null,
                    "query": null
                  },
                  "method": "POST",
                  "link": "/services/apexrest/vlocity_cmt/v2/cpq/carts/8011N00000157LkQAI/items"
                },
                "remote": {
                  "params": {
                    "methodName": "postCartsItems",
                    "items": [
                      {
                        "quantity": 1,
                        "parentId": "8021N000006wtTTQAY",
                        "parentHierarchyPath": "01to000000Am1FGAAZ",
                        "itemId": "01u1N00000DPpdFQAT"
                      }
                    ],
                    "cartId": "8011N00000157LkQAI",
                    "price": true,
                    "validate": true,
                    "includeAttachment": false,
                    "pagesize": 10,
                    "lastRecordId": null,
                    "query": null
                  }
                },
                "client": {
                  "params": {}
                }
              },
              "updateitems": {
                "rest": {
                  "params": {
                    "items": [
                      {
                        "itemId": "8021N000006wtUMQAY"
                      }
                    ],
                    "filters": null,
                    "fields": null,
                    "cartId": "8011N00000157LkQAI",
                    "price": true,
                    "validate": true,
                    "includeAttachment": false,
                    "pagesize": 10,
                    "lastRecordId": null,
                    "query": null
                  },
                  "method": "PUT",
                  "link": "/services/apexrest/vlocity_cmt/v2/cpq/carts/8011N00000157LkQAI/items"
                },
                "remote": {
                  "params": {
                    "methodName": "putCartsItems",
                    "items": [
                      {
                        "itemId": "8021N000006wtUMQAY"
                      }
                    ],
                    "filters": null,
                    "fields": null,
                    "cartId": "8011N00000157LkQAI",
                    "price": true,
                    "validate": true,
                    "includeAttachment": false,
                    "pagesize": 10,
                    "lastRecordId": null,
                    "query": null
                  }
                },
                "client": {
                  "params": {}
                }
              },
[EXAMPLE TRUNCATED FOR BREVITY]
```
