|
| 1 | +--- |
| 2 | +layout: tutorial |
| 3 | +group: graphql |
| 4 | +title: Step 3. Add products to the cart |
| 5 | +subtitle: GraphQL checkout tutorial |
| 6 | +level3_subgroup: graphql-checkout |
| 7 | +return_to: |
| 8 | + title: GraphQL Overview |
| 9 | + url: graphql/index.html |
| 10 | +menu_order: 30 |
| 11 | +functional_areas: |
| 12 | + - Integration |
| 13 | +contributor_name: Atwix |
| 14 | +contributor_link: https://www.atwix.com/ |
| 15 | +--- |
| 16 | + |
| 17 | +GraphQL supports [two types of product]({{ page.baseurl }}/graphql/reference/product-interface-implementations.html) which can be added into shopping cart: |
| 18 | + |
| 19 | + * simple product |
| 20 | + * virtual product |
| 21 | + |
| 22 | +{:.bs-callout .bs-callout-info} |
| 23 | +If you add a product to the shopping cart as a registered customer, be sure to send the customer's authorization token in the `Authorization` parameter of the header. See ["Get customer authorization token"]({{ page.baseurl }}/graphql/get-customer-authorization-token.html) for more details. |
| 24 | + |
| 25 | +`{ CART_ID }` is the unique shopping cart ID from [Step 2. Create empty cart]({{ page.baseurl }}/graphql/tutorials/checkout/checkout-add-product-to-cart.html). |
| 26 | + |
| 27 | +## Add a simple product into the shopping cart |
| 28 | + |
| 29 | +The following mutation adds a **simple product** into shopping cart. |
| 30 | + |
| 31 | +**Request** |
| 32 | + |
| 33 | +```text |
| 34 | +mutation { |
| 35 | + addSimpleProductsToCart( |
| 36 | + input: { |
| 37 | + cart_id: "{ CART_ID }" |
| 38 | + cart_items: [ |
| 39 | + { |
| 40 | + data: { |
| 41 | + quantity: 1 |
| 42 | + sku: "simple-product" |
| 43 | + } |
| 44 | + } |
| 45 | + ] |
| 46 | + } |
| 47 | + ) { |
| 48 | + cart { |
| 49 | + items { |
| 50 | + id |
| 51 | + product { |
| 52 | + sku |
| 53 | + stock_status |
| 54 | + } |
| 55 | + quantity |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +**Response** |
| 63 | + |
| 64 | +```json |
| 65 | +{ |
| 66 | + "data": { |
| 67 | + "addSimpleProductsToCart": { |
| 68 | + "cart": { |
| 69 | + "items": [ |
| 70 | + { |
| 71 | + "id": "508", |
| 72 | + "product": { |
| 73 | + "sku": "simple-product", |
| 74 | + "stock_status": "IN_STOCK" |
| 75 | + }, |
| 76 | + "quantity": 1 |
| 77 | + } |
| 78 | + ] |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +## Add a virtual product into the shopping cart |
| 86 | + |
| 87 | +The following mutation adds a **virtual product** into shopping cart. |
| 88 | + |
| 89 | +**Request** |
| 90 | + |
| 91 | +```text |
| 92 | +mutation { |
| 93 | + addVirtualProductsToCart( |
| 94 | + input: { |
| 95 | + cart_id: "{ CART_ID }" |
| 96 | + cart_items: [ |
| 97 | + { |
| 98 | + data: { |
| 99 | + quantity: 1 |
| 100 | + sku: "virtual-product" |
| 101 | + } |
| 102 | + } |
| 103 | + ] |
| 104 | + } |
| 105 | + ) { |
| 106 | + cart { |
| 107 | + items { |
| 108 | + id |
| 109 | + product { |
| 110 | + sku |
| 111 | + stock_status |
| 112 | + } |
| 113 | + quantity |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +**Response** |
| 121 | + |
| 122 | +```json |
| 123 | +{ |
| 124 | + "data": { |
| 125 | + "addVirtualProductsToCart": { |
| 126 | + "cart": { |
| 127 | + "items": [ |
| 128 | + { |
| 129 | + "id": "509", |
| 130 | + "product": { |
| 131 | + "sku": "virtual-product", |
| 132 | + "stock_status": "IN_STOCK" |
| 133 | + }, |
| 134 | + "quantity": 1 |
| 135 | + } |
| 136 | + ] |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +Use the `updateCartItems` mutation to update shopping cart items and `removeItemFromCart` to remove a product from the shopping cart. |
| 144 | + |
| 145 | +## Verify this step {#verify-step} |
| 146 | + |
| 147 | +1. Sign in as a customer to the website using the email `[email protected]` and password `b1b2b3l@w+`. |
| 148 | + |
| 149 | +2. Go to the shopping cart. All the items you added are displayed. |
0 commit comments