3

I can't work out the syntax for adding a Gift Card product to the cart using Graphql. What product options need setting?

mutation AddProductToCart($cartId: String!, $product: CartItemInput!) {
    addProductsToCart(cartId: $cartId, cartItems: [$product]) {
        cart {
            id
            ...CartTriggerFragment
            ...MiniCartFragment
            __typename
        }
        __typename
    }
}

fragment CartTriggerFragment on Cart {
    id
    total_quantity
    __typename
}

fragment MiniCartFragment on Cart {
    id
    total_quantity
    prices {
        subtotal_excluding_tax {
            currency
            value
            __typename
        }
        subtotal_including_tax {
            currency
            value
            __typename
        }
        __typename
    }
    ...ProductListFragment
    __typename
}

fragment ProductListFragment on Cart {
    id
    items {
        uid
        product {
            uid
            name
            sku
            url_key
            thumbnail {
                url
                __typename
            }
            stock_status
            ... on ConfigurableProduct {
                variants {
                    attributes {
                        uid
                        __typename
                    }
                    product {
                        uid
                        thumbnail {
                            url
                            __typename
                        }
                        __typename
                    }
                    __typename
                }
                __typename
            }
            __typename
        }
        prices {
            price {
                currency
                value
                __typename
            }
            total_item_discount {
                value
                __typename
            }
            __typename
        }
        quantity
        ... on ConfigurableCartItem {
            configurable_options {
                configurable_product_option_uid
                option_label
                configurable_product_option_value_uid
                value_label
                __typename
            }
            __typename
        }
        __typename
    }
    __typename
}

1 Answer 1

0

You can send following request

mutation ($cartId: String!, $cartItems: [CartItemInput!]!) {
  addProductsToCart(cartId: $cartId, cartItems: $cartItems) {
    cart {
      id
      items {
        uid
        quantity
        product {
          sku
          name
          thumbnail {
            url
            __typename
          }
          __typename
        }
      }
    }
  }
}

where the variables passed for gift card are

{
    "cartId": "{cart id token}",
    "cartItems": [
        {
            "sku": "{product sku}",
            "quantity": {qty},
            "entered_options": [
                {
                    "uid": "Z2lmdGNhcmQvZ2lmdGNhcmRfc2VuZGVyX25hbWU=",
                    "value": "{sender name}"
                },
                {
                    "uid": "Z2lmdGNhcmQvZ2lmdGNhcmRfc2VuZGVyX2VtYWls",
                    "value": "{sender email}"
                },
                {
                    "uid": "Z2lmdGNhcmQvZ2lmdGNhcmRfcmVjaXBpZW50X25hbWU=",
                    "value": "{Recipient name}"
                },
                {
                    "uid": "Z2lmdGNhcmQvZ2lmdGNhcmRfcmVjaXBpZW50X2VtYWls",
                    "value": "{Recipient email}"
                },
                {
                    "uid": "Z2lmdGNhcmQvZ2lmdGNhcmRfbWVzc2FnZQ==",
                    "value": "{message}"
                },
                {
                    "uid": "Z2lmdGNhcmQvY3VzdG9tX2dpZnRjYXJkX2Ftb3VudA==",
                    "value": "{amount}"
                }
            ]
        }
    ]
}

the above uids are base64 encoded with following text prefixed with type giftcard/-

  1. giftcard_sender_name
  2. giftcard_sender_email
  3. giftcard_recipient_name
  4. giftcard_recipient_email
  5. custom_giftcard_amount
  6. giftcard_amount

example -

giftcard/giftcard_sender_name = Z2lmdGNhcmQvZ2lmdGNhcmRfc2VuZGVyX25hbWU=

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.