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.

The mutation for adding configurable products is defined in the ConfigurableProductGraphQl module.

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 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 The AppliedCoupon object contains the code text attribute, which specifies the coupon code
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
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

Cart query output provides more information about the Cart object.