You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 19, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: src/guides/v2.4/graphql/mutations/add-products-to-cart.md
+199-2Lines changed: 199 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -50,7 +50,7 @@ mutation {
50
50
51
51
These examples show the minimal payload for adding products, including those with customizable options.
52
52
53
-
### Add a product to a cart
53
+
### Add a simple product to a cart
54
54
55
55
The following example adds a simple product to a cart.
56
56
@@ -244,10 +244,13 @@ mutation {
244
244
}
245
245
```
246
246
247
-
### Add a product with entered options
247
+
### Add a simple product with entered options
248
248
249
249
The following example adds a simple product with a customizable option to the cart. The customizable option allows the shopper to specify a message for engraving.
250
250
251
+
{:.bs-callout-info}
252
+
The customizable option is not part of the Luma sample data.
253
+
251
254
**Request:**
252
255
253
256
```graphql
@@ -323,6 +326,200 @@ mutation {
323
326
}
324
327
```
325
328
329
+
## Add a bundle product with selected options to a cart
330
+
331
+
The following example adds the Sprite Yoga Companion Kit bundle product to the cart. The bundle product is comprised of four simple products, and the selected simple products are specified with a value in the `selected_options` array. Use the `products` query to determine these UID values. Note that each UID value is an encoded value representing the following string:
Because the encoded value includes the quantity, the schema does not contain a `quantity` attribute for individual simple products.
336
+
337
+
In this example, the UID values are encoded versions of these strings:
338
+
339
+
```text
340
+
bundle/1/1/1
341
+
bundle/2/4/1
342
+
bundle/3/5/1
343
+
bundle/4/8/1
344
+
```
345
+
346
+
**Request:**
347
+
348
+
```graphql
349
+
mutation {
350
+
addProductsToCart(
351
+
cartId: "ELwvX8VJinGJ9Q2vOXSiCTS4gvCDKP8U"
352
+
cartItems: [
353
+
{
354
+
quantity: 1
355
+
sku: "24-WG080"
356
+
selected_options: [
357
+
"YnVuZGxlLzEvMS8x"
358
+
"YnVuZGxlLzIvNC8x"
359
+
"YnVuZGxlLzMvNS8x"
360
+
"YnVuZGxlLzQvOC8x"
361
+
]
362
+
}
363
+
]
364
+
) {
365
+
cart {
366
+
items {
367
+
uid
368
+
product {
369
+
name
370
+
sku
371
+
}
372
+
quantity
373
+
...onBundleCartItem {
374
+
bundle_options {
375
+
uid
376
+
label
377
+
type
378
+
values {
379
+
id
380
+
label
381
+
price
382
+
quantity
383
+
}
384
+
}
385
+
}
386
+
}
387
+
}
388
+
}
389
+
}
390
+
```
391
+
392
+
**Response:**
393
+
394
+
```json
395
+
{
396
+
"data": {
397
+
"addProductsToCart": {
398
+
"cart": {
399
+
"items": [
400
+
{
401
+
"uid": "MTQ=",
402
+
"product": {
403
+
"name": "Sprite Yoga Companion Kit",
404
+
"sku": "24-WG080"
405
+
},
406
+
"quantity": 1,
407
+
"bundle_options": [
408
+
{
409
+
"uid": "YnVuZGxlLzE=",
410
+
"label": "Sprite Stasis Ball",
411
+
"type": "radio",
412
+
"values": [
413
+
{
414
+
"id": 1,
415
+
"label": "Sprite Stasis Ball 55 cm",
416
+
"price": 23,
417
+
"quantity": 1
418
+
}
419
+
]
420
+
},
421
+
{
422
+
"uid": "YnVuZGxlLzI=",
423
+
"label": "Sprite Foam Yoga Brick",
424
+
"type": "radio",
425
+
"values": [
426
+
{
427
+
"id": 4,
428
+
"label": "Sprite Foam Yoga Brick",
429
+
"price": 5,
430
+
"quantity": 1
431
+
}
432
+
]
433
+
},
434
+
{
435
+
"uid": "YnVuZGxlLzM=",
436
+
"label": "Sprite Yoga Strap",
437
+
"type": "radio",
438
+
"values": [
439
+
{
440
+
"id": 5,
441
+
"label": "Sprite Yoga Strap 6 foot",
442
+
"price": 14,
443
+
"quantity": 1
444
+
}
445
+
]
446
+
},
447
+
{
448
+
"uid": "YnVuZGxlLzQ=",
449
+
"label": "Sprite Foam Roller",
450
+
"type": "radio",
451
+
"values": [
452
+
{
453
+
"id": 8,
454
+
"label": "Sprite Foam Roller",
455
+
"price": 19,
456
+
"quantity": 1
457
+
}
458
+
]
459
+
}
460
+
]
461
+
}
462
+
]
463
+
}
464
+
}
465
+
}
466
+
}
467
+
```
468
+
469
+
### Add a bundle product with entered options to the cart
470
+
471
+
For `entered_options`, the `uid` attribute contains the encoded entered value. The `value` attribute defines the quantity. If the `BundleProduct.items.options.can_change_quantity` attribute is `false`, then the bundle product definition sets the quantity for the simple product. Otherwise, the shopper decides the quantity.
472
+
473
+
The Luma sample data does not provide any bundle products with entered options. The following snippet shows how to construct the mutation.
474
+
475
+
```graphql
476
+
mutation {
477
+
addProductsToCart(
478
+
cartId: "ELwvX8VJinGJ9Q2vOXSiCTS4gvCDKP8U"
479
+
cartItems: [
480
+
{
481
+
quantity: 1
482
+
sku: "bundle1"
483
+
entered_options: [
484
+
{
485
+
uid: "EncodedEnteredValue1"
486
+
value: 1
487
+
}
488
+
]
489
+
selected_options: [
490
+
"EncodedSelectedValue1"
491
+
"EncodedSelectedValue2"
492
+
]
493
+
}
494
+
]
495
+
) {
496
+
cart {
497
+
items {
498
+
uid
499
+
product {
500
+
name
501
+
sku
502
+
}
503
+
quantity
504
+
...onBundleCartItem {
505
+
bundle_options {
506
+
uid
507
+
label
508
+
type
509
+
values {
510
+
id
511
+
label
512
+
price
513
+
quantity
514
+
}
515
+
}
516
+
}
517
+
}
518
+
}
519
+
}
520
+
}
521
+
```
522
+
326
523
## Input attributes
327
524
328
525
The `addProductsToCart` mutation must contain the following attributes:
0 commit comments