On this article, We will be able to find out about find out how to upload configurable merchandise to the cart the use of Graphql in Magento 2.
Within the earlier weblog, We discovered about find out how to upload easy merchandise to cart the use of GraphQl Methods to upload easy merchandise to cart the use of GraphQl
Apply the under step so as to add configurable merchandise to the cart the use of Graphql in Magento 2.
GraphQL Endpoint:Â BASE_URL/graphql
Instance: http://instance.com/graphql
First off, we wish to create an empty cart so as to add merchandise in Magento 2.
We’ve got created an empty cart for the visitor person the use of the next mutation, token isn’t required for the visitor person.
Upload a configurable product.
Use the addConfigurableProductsToCart
 mutation so as to add configurable merchandise to the buying groceries cart.
Within the following instance upload one product with configuration choices colour is purple.
Request:
mutation { addConfigurableProductsToCart( enter: { cart_id: "SPBJroclB7zT45lgY0stDJmpPnvcsGx2" cart_items: [{ parent_sku: "configurable" data: { quantity: 1, sku: "configurable-red" } }] } ) { cart { pieces { identification product { title sku options_container } amount ... on ConfigurableCartItem{ configurable_options{ identification option_label value_label value_id } } } } } }
Reaction:
Execute the mutation via your GraphQL shopper, and the configurable product can be added to the cart.
{ "information": { "addConfigurableProductsToCart": { "cart": { "pieces": [ { "id": "732", "product": { "name": "configurable", "sku": "configurable", "options_container": "container2" }, "quantity": 1, "configurable_options": [ { "id": 93, "option_label": "Color", "value_label": "red", "value_id": 4 } ] } ] } } } }
Upload a downloadable product to a cart
Use the addDownloadableProductsToCart
 mutation so as to add downloadable merchandise to the buying groceries cart.
A downloadable product can also be the rest that you’ll obtain as a record, corresponding to an eBook, tune or video.
Request:
mutation { addDownloadableProductsToCart( enter: { cart_id: "SPBJroclB7zT45lgY0stDJmpPnvcsGx2" cart_items: { information: { sku: "downlodableProdc" amount: 1 } downloadable_product_links: [ { link_id: 2 } { link_id: 5 } ] } } ) { cart { pieces { product { identification sku title } amount ... on DownloadableCartItem { hyperlinks { name value } samples { name sample_url } } } } } }
Reaction:
Execute the mutation via your GraphQL shopper, and the downloadable product can be added to the cart.
{ "information": { "addDownloadableProductsToCart": { "cart": { "pieces": [ { "product": { "id": 76, "sku": "downlodableProdc", "name": "downlodableProdc" }, "quantity": 1, "links": [ { "title": "sampleAudio", "price": 21 }, { "title": "Sample 2", "price": 32 } ], "samples": [ { "title": "tre", "sample_url": "https://example.com/downloadable/download/sample/sample_id/1/" } ] } ] } } } }
Upload a package product to a cart
Use the addBundleProductsToCart
 mutation so as to add package merchandise to the buying groceries cart.
This case provides one package product to the buying groceries cart. The SKU of this product  bundleProduct
Request:
mutation { addBundleProductsToCart( enter: { cart_id: "SPBJroclB7zT45lgY0stDJmpPnvcsGx2" cart_items: [ { data: { sku: "bundleProduct" quantity: 1 } bundle_options: [ { id: 2 quantity: 1 value: [ "4" ] } ] }, ] }) { cart { pieces { identification amount product { sku title } ... on BundleCartItem { bundle_options { identification label kind values { identification label value amount } } } } } } }
Reaction:
Execute the mutation via your GraphQL shopper, and the package product can be added to the cart.
{ "information": { "addBundleProductsToCart": { "cart": { "pieces": [ { "id": "114", "quantity": 1, "product": { "sku": "bundleProduct", "name": "bundleProduct" }, "bundle_options": [ { "id": 2, "label": "Simple", "type": "select", "values": [ { "id": 4, "label": "simple", "price": 10, "quantity": 1 } ] } ] } ] } } } }
Upload VirtualProduct to a cart
Use the addVirtualProductsToCart
 mutation so as to add digital merchandise to the buying groceries cart.
A digital product represents a saleable merchandise that’s not bodily, corresponding to a club, provider, guaranty, or subscription. Digital merchandise don’t wish to be shipped or downloaded, nor do they require inventory control.
Request:
mutation { addVirtualProductsToCart( enter: { cart_id: "pIRMnP9PtmZ7lu3b22mb7niTMnFhu0Nw", cart_items: [ { data: { quantity: 1 sku: "virtualProduct" } } ] } ) { cart { pieces { product { title } amount } costs { grand_total { price foreign money } } } } }
Reaction:
Execute the mutation via your GraphQL shopper, and the digital product can be added to the cart.
{ "information": { "addVirtualProductsToCart": { "cart": { "pieces": [ { "product": { "name": "virtualProduct" }, "quantity": 1 } ], "costs": { "grand_total": { "price": 100, "foreign money": "USD" } } } } } }
Hope this may can help you.
Thank you 🙂
You’ll test the next blogs associated with GraphQl:
Product Varieties in Magento 2
Methods to upload easy merchandise to cart the use of GraphQL
GraphQl implementation in Magento 2
GraphQL Mutation