|
| 1 | +--- |
| 2 | +group: graphql |
| 3 | +title: addDownloadableProductsToCart mutation |
| 4 | +contributor_name: Atwix |
| 5 | +contributor_link: https://www.atwix.com/ |
| 6 | +--- |
| 7 | + |
| 8 | +A downloadable product can be anything that you can deliver as a file, such as an eBook, music, video, software application, or an update. To add a downloadable product to a cart, you must provide the cart ID, the SKU, and the quantity. In some cases, you must include the IDs for downloadable product links. You can also optionally specify customizable options. |
| 9 | + |
| 10 | +## Syntax |
| 11 | + |
| 12 | +```graphql |
| 13 | +mutation: { |
| 14 | + addDownloadableProductsToCart( |
| 15 | + input: AddDownloadableProductsToCartInput |
| 16 | + ): { |
| 17 | + AddDownloadableProductsToCartOutput |
| 18 | + } |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +## Example usage |
| 23 | + |
| 24 | +The following examples show how to add a downloadable product to a shopping cart , depending on whether the **Links can be purchased separately** option is selected on the **Downloadable Information** section of the product page. |
| 25 | + |
| 26 | +### Add a downloadable product to a cart with `Links can be purchased separately` enabled |
| 27 | + |
| 28 | +The following example shows how to add a downloadable product in which the **Links can be purchased separately** option is enabled. The payload includes custom downloadable links `Episode 2` and `Episode 3`. |
| 29 | + |
| 30 | +**Request** |
| 31 | + |
| 32 | +```graphql |
| 33 | +mutation { |
| 34 | + addDownloadableProductsToCart( |
| 35 | + input: { |
| 36 | + cart_id: "gMV2BFQuNGiQmTnepQlMGko7Xc4P3X1w" |
| 37 | + cart_items: { |
| 38 | + data: { |
| 39 | + sku: "240-LV09" |
| 40 | + quantity: 1 |
| 41 | + } |
| 42 | + downloadable_product_links: [ |
| 43 | + { |
| 44 | + link_id: 7 # Episode 2 |
| 45 | + } |
| 46 | + { |
| 47 | + link_id: 8 # Episode 3 |
| 48 | + } |
| 49 | + ] |
| 50 | + } |
| 51 | + } |
| 52 | + ) { |
| 53 | + cart { |
| 54 | + items { |
| 55 | + product { |
| 56 | + sku |
| 57 | + } |
| 58 | + quantity |
| 59 | + ... on DownloadableCartItem { |
| 60 | + links { |
| 61 | + title |
| 62 | + price |
| 63 | + } |
| 64 | + samples { |
| 65 | + title |
| 66 | + sample_url |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +**Response** |
| 76 | + |
| 77 | +```text |
| 78 | +{ |
| 79 | + "data": { |
| 80 | + "addDownloadableProductsToCart": { |
| 81 | + "cart": { |
| 82 | + "items": [ |
| 83 | + { |
| 84 | + "product": { |
| 85 | + "sku": "240-LV09" |
| 86 | + }, |
| 87 | + "quantity": 1, |
| 88 | + "links": [ |
| 89 | + { |
| 90 | + "title": "Episode 2", |
| 91 | + "price": 9 |
| 92 | + }, |
| 93 | + { |
| 94 | + "title": "Episode 3", |
| 95 | + "price": 9 |
| 96 | + } |
| 97 | + ], |
| 98 | + "samples": [ |
| 99 | + { |
| 100 | + "title": "Trailer #1", |
| 101 | + "sample_url": "https://<M2_INSTANCE>/downloadable/download/sample/sample_id/16/" |
| 102 | + }, |
| 103 | + { |
| 104 | + "title": "Trailer #2", |
| 105 | + "sample_url": "https://<M2_INSTANCE>/downloadable/download/sample/sample_id/17/" |
| 106 | + }, |
| 107 | + { |
| 108 | + "title": "Trailer #3", |
| 109 | + "sample_url": "https://<M2_INSTANCE>/downloadable/download/sample/sample_id/18/" |
| 110 | + } |
| 111 | + ] |
| 112 | + } |
| 113 | + ] |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +### Add a downloadable product to a cart with disabled `Links can be purchased separately` |
| 121 | + |
| 122 | +The following example shows how to add a downloadable product in which the **Links can be purchased separately** option is disabled. All downloadable links are added to the cart automatically. |
| 123 | + |
| 124 | +**Request** |
| 125 | + |
| 126 | +```graphql |
| 127 | +mutation { |
| 128 | + addDownloadableProductsToCart( |
| 129 | + input: { |
| 130 | + cart_id: "gMV2BFQuNGiQmTnepQlMGko7Xc4P3X1w" |
| 131 | + cart_items: { |
| 132 | + data: { |
| 133 | + sku: "240-LV07" |
| 134 | + quantity: 1 |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + ) { |
| 139 | + cart { |
| 140 | + items { |
| 141 | + product { |
| 142 | + sku |
| 143 | + } |
| 144 | + quantity |
| 145 | + ... on DownloadableCartItem { |
| 146 | + links { |
| 147 | + title |
| 148 | + price |
| 149 | + } |
| 150 | + samples { |
| 151 | + title |
| 152 | + sample_url |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +**Response** |
| 162 | + |
| 163 | +```text |
| 164 | +{ |
| 165 | + "data": { |
| 166 | + "addDownloadableProductsToCart": { |
| 167 | + "cart": { |
| 168 | + "items": [ |
| 169 | + { |
| 170 | + "product": { |
| 171 | + "sku": "240-LV07" |
| 172 | + }, |
| 173 | + "quantity": 2, |
| 174 | + "links": [ |
| 175 | + { |
| 176 | + "title": "Solo Power Circuit", |
| 177 | + "price": 14 |
| 178 | + } |
| 179 | + ], |
| 180 | + "samples": [ |
| 181 | + { |
| 182 | + "title": "Trailer #1", |
| 183 | + "sample_url": "https://<M2_INSTANCE>/downloadable/download/sample/sample_id/10/" |
| 184 | + }, |
| 185 | + { |
| 186 | + "title": "Trailer #2", |
| 187 | + "sample_url": "https://<M2_INSTANCE>/downloadable/download/sample/sample_id/11/" |
| 188 | + }, |
| 189 | + { |
| 190 | + "title": "Trailer #3", |
| 191 | + "sample_url": "https://<M2_INSTANCE>/downloadable/download/sample/sample_id/12/" |
| 192 | + } |
| 193 | + ] |
| 194 | + } |
| 195 | + ] |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | +} |
| 200 | +``` |
| 201 | + |
| 202 | +## Input attributes |
| 203 | + |
| 204 | +The top-level `AddDownloadableProductsToCartInput` object is listed first. All child objects are listed in alphabetical order. |
| 205 | + |
| 206 | +### AddDownloadableProductsToCartInput object {#AddDownloadableProductsToCartInput} |
| 207 | + |
| 208 | +The `AddDownloadableProductsToCartInput` object must contain the following attributes: |
| 209 | + |
| 210 | +Attribute | Data Type | Description |
| 211 | +--- | --- | --- |
| 212 | +`cart_id` | String! | The unique ID that identifies the customer's cart |
| 213 | +`cart_items` | [[DownloadableProductCartItemInput!]!](#DownloadableProductCartItemInput) | Contains the cart item IDs and quantity of each item |
| 214 | + |
| 215 | +### CartItemInput object {#CartItemInputVirtual} |
| 216 | + |
| 217 | +The `CartItemInput` object must contain the following attributes: |
| 218 | + |
| 219 | +{% include graphql/cart-item-input.md %} |
| 220 | + |
| 221 | +### CustomizableOptionInput object {#CustomizableOptionInputVirtual} |
| 222 | + |
| 223 | +The `CustomizableOptionInput` object must contain the following attributes: |
| 224 | + |
| 225 | +{% include graphql/customizable-option-input.md %} |
| 226 | + |
| 227 | +### DownloadableProductCartItemInput object {#DownloadableProductCartItemInput} |
| 228 | + |
| 229 | +The `DownloadableProductCartItemInput` object can contain the following attribute: |
| 230 | + |
| 231 | +Attribute | Data Type | Description |
| 232 | +--- | --- | --- |
| 233 | +`customizable_options` |[[CustomizableOptionInput!]](#CustomizableOptionInputVirtual) | An array that defines customizable options for the product |
| 234 | +`data` | [CartItemInput!](#CartItemInputVirtual) | Required. An object containing the `sku` and `quantity` of the product |
| 235 | +`downloadable_product_links` | [[DownloadableProductLinksInput!]](#DownloadableProductLinksInput) | An object containing the `link_id` of the downloadable product link |
| 236 | + |
| 237 | +### DownloadableProductLinksInput object {#DownloadableProductLinksInput} |
| 238 | + |
| 239 | +If specified, the `DownloadableProductLinksInput` object must contain the following attribute. |
| 240 | + |
| 241 | +Attribute | Data Type | Description |
| 242 | +--- | --- | --- |
| 243 | +`link_id` | Int! | A unique ID (`downloadable_link`.`link_id`) of the downloadable product link |
| 244 | + |
| 245 | +## Output attributes |
| 246 | + |
| 247 | +The `AddDownloadableProductsToCartOutput` object contains the `Cart` object. |
| 248 | + |
| 249 | +Attribute | Data Type | Description |
| 250 | +--- | --- | --- |
| 251 | +`cart` |[Cart!](#CartObject) | Describes the contents of the specified shopping cart |
| 252 | + |
| 253 | +### Cart object {#CartObject} |
| 254 | + |
| 255 | +{% include graphql/cart-object.md %} |
| 256 | + |
| 257 | +## Errors |
| 258 | + |
| 259 | +Error | Description |
| 260 | +--- | --- |
| 261 | +`Required parameter "cart_id" is missing` | The mutation does not contain a `cart_id` parameter. |
| 262 | +`Required parameter "cart_items" is missing` | The `cart_items` parameter is empty or is not of type `array`. |
| 263 | +`Please specify product link(s).` | You tried to add a downloadable product in which the `Links can be purchased separately` option is enabled, but you did not specify individual product links. |
0 commit comments