---
schema_version: "1"
id: 7fa2e58a741f30b32c314a615f7270fee3da04ff81796190bd28617d2bbe1549
title: Cart Wrapper and CartItem Wrapper for Custom Pricing Plan
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/industries/cme/guides/cart-wrapper-for-custom-pricing-plan.md
canonical_url: https://developer.salesforce.com/docs/industries/cme/guide/cart-wrapper-for-custom-pricing-plan.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: ce18e095d00808186a0b845303c966faf1ccdad9fe4537e56394a54b61cf233b
products:
  - Communications, Media, Energy and Utilities APIs (Managed Package)
  - Industries Developer Documentation
---
# Cart Wrapper and CartItem Wrapper for Custom Pricing Plan

Cart wrapper and CartItemWrapper interface provides minimum viable functionalities to operate on cart and cart items. Use these interfaces to add your own custom steps in the pricing plan steps.

CartItemWrapper Interface:

![CartItemWrapper Interface](https://developer.salesforce.com/docs/media/cartwraaper_dc.png)

1.  To work with the Cart Wrapper interface:

    1.  Get the cart wrapper instance for CpqCart or CpqCartDocument using the method:

        **note:** Do not use the same instance of CpqCartDocument for every pricing step. For each step, get the instance using the method mentioned below.

        ```

        **For CpqCart
        CartWrapper wrapper = (CartWrapper)PricingPlanService.getFromPricingContext('Parent');

        ** For CpqCartDocument
        CartWrapper wrapper = (CartWrapper)input.get('cartDocument');

        ```

    2.  Call the Item wrapper APIs using the wrapper.call(methodName, args).

        The Below table lists the supported methods for CpqCart:Item

        | Method Name                   | Arguments                                                | Description                                           |
        | ----------------------------- | -------------------------------------------------------- | ----------------------------------------------------- |
        | CpqConstants.GET_CART_FIELD   | CpqConstants.FIELD                                       | Method to get the fields of the order.                |
        | CpqConstants.SET_CART_FIELD   | CpqConstants.FIELD, CpqConstants.VALUE                   | Method to set the order field value.                  |
        | CpqConstants.GET_SOBJECT_TYPE |                                                          | Method to get the SObjectType of the underlying cart. |
        | CpqConstants.GET_ITEM_IDS     |                                                          | Method to get the item ids.                           |
        | CpqConstants.GET_ALL_ITEMS    | CpqConstants.HIERARCHY_LEVEL(optional: -1 full hierachy) | Method to get all the line items.                     |
        | CpqConstants.GET_CHILD_ITEMS  | CpqConstants.ITEM_ID                                     | Method to get all the child line items.               |
        | CpqConstants.DELETE_LINE_ITEM | CpqConstants.ITEM_ID                                     | Method to delete a line item.                         |

2.  To work with the the CartItem Wrapper interface:

    1.  The line items present in the basket can be accessed using the wrapper class of CpqCartDocumentItem or CpqCartItem. Get the cart wrapper instance for CpqCart or CpqCartDocument using the method:

        ```

        **For CpqCartItem
        Map<String, Object> result = wrapper.call('getAllItems', null);
        Map<Id, CartItemWrapper> allItems = (Map<Id, CartItemWrapper>) result.get(CpqConstants.RESULT);

        ** For CpqCartDocumentItem
         Map<String, Object> result = wrapper.call('getAllItems', null);
        Map<String, CartItemWrapper> allItems = (Map<String, CartItemWrapper>) result.get(CpqConstants.RESULT);


        ```

    2.  Call the wrapper APIs using the wrapper.call(methodName, args).

        the below table lists the supported methods for CpqCart:

        | Method Name                      | Arguments                                                                          | Description                                                                                                               |
        | -------------------------------- | ---------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
        | CpqConstants.GET_ITEM_FIELD      | CpqConstants.FIELD                                                                 | Method to get an item field, or a field from the item's related SObjects.                                                 |
        | CpqConstants.SET_ITEM_FIELD      | CpqConstants.FIELD, CpqConstants.VALUE                                             | Method to set the value of an item field.                                                                                 |
        | CpqConstants.GET_SOBJECT_TYPE    |                                                                                    | Method to get the SObjectType of the underlying cartitem.                                                                 |
        | CpqConstants.GET_ITEM_FIELDS     | CpqConstants.FIELDS                                                                | Method to get multiple item fields.                                                                                       |
        | CpqConstants.SET_ITEM_FIELDS     | CpqConstants.FIELD_VALUE_MAP                                                       | Method to set multiple item fields.                                                                                       |
        | CpqConstants.GET_ITEM_ATTRIBUTES | CpqConstants.FLATTENED_ATTRIBUTE_VALUES), CpqConstants.USE_DISPLAY_TEXT_FOR_VALUES | Method to get the attribute node of the item. There are 2 parameters according to which the attribute nodes are returned. |
        | CpqConstants.SET_ITEM_ATTRIBUTES | CpqConstants.ATTRIBUTE_CODE_TO_VALUE_MAP                                           | Method to set the item attributes.                                                                                        |
        | CpqConstants.GET_CHILD_ITEM_IDS  |                                                                                    | Method to get the child item ids.                                                                                         |

    **Sample Code**

    **note:** - The cart wrapper would internally call the V2 APIs for Digital Commerce or CPQ APIs.
    - The cart wrapper would call the V3 APIs for Standard Digital Commerce APIs.

    ```

    // namespace.className managed package

    global inherited sharing class CustomPricingPlanImplementation implements VlocityOpenInterface {

        global boolean invokeMethod(String methodName, Map<String, Object> input, Map<String, Object> output, Map<String, Object> options) {

             if (methodName == 'testV2CustommPricingPlanImpl'){
                 testV2CustomPricingPlan(input, output, options);
             }
             if (methodName == 'testV3CustommPricingPlanImpl'){
                testV3CustomPricingPlan(input, output, options);
             }
            return true;
         }

        private void testV2CustomPricingPlan(Map<String, Object> input, Map<String, Object> output, Map<String, Object> options) {
            Map<String, Object> args  = new Map<String, Object>();


            CartWrapper wrapper = (CartWrapper)PricingPlanService.getFromPricingContext('Parent');
            Map<String, Object> result = wrapper.call('getAllItems', null);
            if(result.containsKey(CpqConstants.RESULT)){

                Map<Id, CartItemWrapper> allItems = (Map<Id, CartItemWrapper>) result.get(CpqConstants.RESULT);
                for(Id itemId : allItems.keySet()) {
                    CartItemWrapper itemWrapper = allItems.get(itemId);

                    // args.put(CpqConstants.FIELD, 'vlocity_lwc8__ItemName__c');
                    // args.put(CpqConstants.VALUE, 'DummyName');

                    args.put(CpqConstants.FIELD, 'vlocity_lwc8__EffectiveRecurringTotal__c');  //
                    args.put(CpqConstants.VALUE, 205);

                    itemWrapper.call(CpqConstants.SET_ITEM_FIELD, args);

                }

            }

        }

        private void testV3CustomPricingPlan(Map<String, Object> input, Map<String, Object> output, Map<String, Object> options) {
            Map<String, Object> args  = new Map<String, Object>();


            CartWrapper wrapper = (CartWrapper)input.get('cartDocument');
            Map<String, Object> result = wrapper.call('getAllItems', null);
            if(result.containsKey(CpqConstants.RESULT)){

                Map<String, CartItemWrapper> allItems = (Map<String, CartItemWrapper>) result.get(CpqConstants.RESULT);
                for(String itemId : allItems.keySet()) {
                    CartItemWrapper itemWrapper = allItems.get(itemId);

                    // args.put(CpqConstants.FIELD, 'vlocity_lwc8__ItemName__c');
                    // args.put(CpqConstants.VALUE, 'DummyName');

                    args.put(CpqConstants.FIELD, 'vlocity_lwc8__EffectiveRecurringTotal__c');  //
                    args.put(CpqConstants.VALUE, 205);
                    itemWrapper.call(CpqConstants.SET_ITEM_FIELD, args);

                }

            }
            Map<String, Object> args1  = new Map<String, Object>();
            args.put(CpqConstants.FIELD, 'vlocity_lwc8__EffectiveRecurringTotal__c');
            args.put(CpqConstants.VALUE, 410);

            wrapper.call(CpqConstants.SET_CART_FIELD, args);

        }

    }
    ```
