addVirtualProductsToCart mutation

A virtual product represents a saleable item that is not physical, such as a membership, service, warranty, or subscription. Virtual products do not need to be shipped or downloaded, nor do they require stock management.

The addVirtualProductsToCart mutation allows you to add multiple virtual products to the cart at the same time, but you cannot add other product types with this mutation. To add a virtual product to a cart, you must provide the cart ID, the SKU, and the quantity. You can also optionally provide customizable options.

Syntax

mutation: {addVirtualProductsToCart(input: AddVirtualProductsToCartInput): {AddVirtualProductsToCartOutput}}

Example usage

The Luma sample data does not include any virtual products. The following example requires that you create a virtual product with the sku value of Membership-Gold with a price of $49.99.

Request:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
mutation {
  addVirtualProductsToCart(
    input: {
      cart_id: "IeTUiU0oCXjm0uRqGCOuhQ2AuQatogjG",
      cart_items: [
        {
          data: {
            quantity: 1
            sku: "Membership-Gold"
          }
        }
       ]
    }
  ) {
    cart {
      items {
        product {
          name
        }
        quantity
      }
      prices {
        grand_total {
          value
          currency
        }
      }
    }
  }
}

Response:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{
  "data": {
    "addVirtualProductsToCart": {
      "cart": {
        "items": [
          {
            "product": {
              "name": "Gold Membership"
            },
            "quantity": 1
          }
        ],
        "prices": {
          "grand_total": {
            "value": 49.99,
            "currency": "USD"
          }
        }
      }
    }
  }
}

Input attributes

The top-level AddVirtualProductsToCartInput object is listed first. All child objects are listed in alphabetical order.

AddVirtualProductsToCartInput object

The AddVirtualProductsToCartInput object must contain the following attributes:

Attribute Data Type Description
cart_id String! The unique ID that identifies the customer’s cart
cart_items VirtualProductCartItemInput! Contains the cart item IDs and quantity of each item

CartItemInput object

The CartItemInput object must contain the following attributes:

Attribute Data Type Description
quantity Float! The quantity of the item to add to the cart
sku String! The sku of the product to be added to the cart

CustomizableOptionInput object

The CustomizableOptionInput object must contain the following attributes:

Attribute Data Type Description
id Int! A unique ID assigned to the customizable option
value_string String! A value assigned to the customizable option

VirtualProductCartItemInput object

The VirtualProductCartItemInput object must contain the following attributes:

Attribute Data Type Description
customizable_options [CustomizableOptionInput] An array that defines customizable options for the product
data CartItemInput! An object containing the sku and quantity of the product

Output attributes

The AddVirtualProductsToCartOutput object contains the Cart object.

Attribute Data Type Description
cart Cart! Describes the contents of the specified shopping cart

Cart object

Attribute Data Type Description
applied_coupon AppliedCoupon Deprecated. Use applied_coupons instead
applied_coupons [AppliedCoupon] An array of AppliedCoupon objects. Each object contains the code text attribute, which specifies the coupon code
applied_gift_cards [AppliedGiftCard] An array of AppliedGiftCard objects. An AppliedGiftCard object contains the code text attribute, which specifies the gift card code. applied_gift_cards is a Commerce-only attribute, defined in the GiftCardAccountGraphQl module
applied_store_credit AppliedStoreCredit Contains store credit information applied to the cart. applied_store_credit is a Commerce-only attribute, defined in the CustomerBalanceGraphQl module
available_payment_methods [AvailablePaymentMethod] Available payment methods
billing_address BillingCartAddress Contains the billing address specified in the customer’s cart
email String The customer’s email address
id ID! The ID of the cart
is_virtual Boolean! Indicates whether the cart contains only virtual products
items [CartItemInterface] Contains the items in the customer’s cart
prices CartPrices Contains subtotals and totals
selected_payment_method SelectedPaymentMethod Selected payment method
shipping_addresses [ShippingCartAddress]! Contains one or more shipping addresses
total_quantity Float! Total Quantity of products in the cart

Cart query output provides more information about the Cart object.

Errors

Error Description
Could not find a cart with ID "XXX" The specified cart_id value does not exist in the quote_id_mask table.
Could not find a product with SKU "YYY" A virtual product with the SKU specified in the data.sku argument does not exist.
Required parameter "cart_id" is missing The cart_id argument was omitted or contains an empty value.
Required parameter "cart_items" is missing The cart_items argument was omitted or contains an empty value.
The current user cannot perform operations on cart XXX An unauthorized user (guest) tried to add the product into a customer’s cart, or an authorized user (customer) tried to add the product into the cart of another customer.
The product's required option(s) weren't entered. Make sure the options are entered and try again. A virtual product has customizable options that were not specified in the mutation, but are required for adding the product into the cart.