How To Add Configurable Products By Using GraphQL in Cart in Magento 2.x / Adobe Commerce 2.x

How To Add Configurable Products By Using GraphQL in Magento 2.x / Adobe Commerce 2.x

Use the updateCartItems mutation to update shopping cart items and removeItemFromCart to remove a product from the shopping cart.

The following below steps need to follow to add Configurable products by using GraphQL in Magento 2.x

Step [1] – Recommendation to use below Configurable Product syntax

Here Using Postman

Syntax –

 mutation {
   addConfigurableProductsToCart(
      input: AddConfigurableProductsToCartInput
   ) {
     AddConfigurableProductsToCartOutput
   }
 }

Step [2] – Put below Request

mutation {
  addConfigurableProductsToCart(
    input: {
      cart_id: "s6Wgi4j0H2ULX0YHfD3mhOMHuAL10qLL"
      cart_items: [{
        parent_sku: "MH01"
        data: {
          quantity: 2,
          sku: "MH01-XS-Black"
        }
      }]
    }
  ) {
    cart {
      items {
        id
        product {
          name
          sku
          options_container
        }
        quantity
        ... on ConfigurableCartItem{
        	configurable_options{
            id
            option_label
            value_label
            value_id
          }
        }
      }
    }
  }
}

Step [3] – Below output response

{
    "data": {
        "addBundleProductsToCart": {
            "cart": {
                "items": [
                    {
                        "id": "131",
                        "quantity": 2,
                        "product": {
                            "sku": "Gaming-Software",
                            "name": "Gaming-Software"
                        }
                    },
                    {
                        "id": "132",
                        "quantity": 4,
                        "product": {
                            "sku": "24-WG080",
                            "name": "Sprite Yoga Companion Kit"
                        },
                        "bundle_options": [
                            {
                                "id": 1,
                                "label": "Sprite Stasis Ball",
                                "type": "radio",
                                "values": [
                                    {
                                        "id": 2,
                                        "label": "Sprite Stasis Ball 65 cm",
                                        "price": 27,
                                        "quantity": 1
                                    }
                                ]
                            },
                            {
                                "id": 2,
                                "label": "Sprite Foam Yoga Brick",
                                "type": "radio",
                                "values": [
                                    {
                                        "id": 4,
                                        "label": "Sprite Foam Yoga Brick",
                                        "price": 5,
                                        "quantity": 2
                                    }
                                ]
                            },
                            {
                                "id": 3,
                                "label": "Sprite Yoga Strap",
                                "type": "radio",
                                "values": [
                                    {
                                        "id": 7,
                                        "label": "Sprite Yoga Strap 10 foot",
                                        "price": 21,
                                        "quantity": 1
                                    }
                                ]
                            },
                            {
                                "id": 4,
                                "label": "Sprite Foam Roller",
                                "type": "radio",
                                "values": [
                                    {
                                        "id": 8,
                                        "label": "Sprite Foam Roller",
                                        "price": 19,
                                        "quantity": 1
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *