Cart Transform Function with Metafield

When I setup our app/extension in a clients store, I need to specify the GID of the product from their store that contains the fee that needs to be added to certain orders. As of right now we are handling the setup of the app/extensions for our clients as part of our service.

I am trying to add a metafield to a cart transform function that contains the GID of the product to be added when I need to expand a cart item to add a fee. I was following this GitHub as an example.

Here is the GraphQL I used to add the metafield to the function:

mutation addCartTransform {
  cartTransformCreate(
    functionId: "9869e7dc-2d98-4ad6-b7cd-07fb07abac8d"
    blockOnFailure: true
    metafields: [{namespace: "$app:cart-transforms", key: "fee-product-id", type: "single_line_text_field", value: "gid://shopify/ProductVariant/45723759739034"}]
  ) {
    cartTransform {
      id
      functionId
      metafields(first: 10) {
        nodes {
          id
        }
      }
    }
    userErrors {
      field
      message
    }
  }
}

This query to verify the field is there:

query getCartTransforms {
  cartTransforms(first: 10) {
    nodes {
      id
      metafields(first: 10) {
        nodes {
          id
          namespace
          type
          value
          key
        }
      }
    }
  }
}

Returns this result:

{
  "data": {
    "cartTransforms": {
      "nodes": [
        {
          "id": "gid://shopify/CartTransform/35455130",
          "metafields": {
            "nodes": [
              {
                "id": "gid://shopify/Metafield/33708880625818",
                "namespace": "app--2315872--cart-transforms",
                "type": "single_line_text_field",
                "value": "gid://shopify/ProductVariant/45723759739034",
                "key": "fee-product-id"
              }
            ]
          }
        }
      ]
    }
  },
  "extensions": {
    "cost": {
      "requestedQueryCost": 30,
      "actualQueryCost": 6,
      "throttleStatus": {
        "maximumAvailable": 2000,
        "currentlyAvailable": 1994,
        "restoreRate": 100
      }
    }
  }
}

This is the graphql query that is used in the cart transform function:

query RunInput {
  cart {
    lines {
      id
      merchandise {
        ... on ProductVariant {
          id
          sku
          product {
            title
          }
          sku
          requiresShipping
        }
      }
      cost { 
        amountPerQuantity {
          amount
        }
      }
    }
  }
  cartTransform {
    targetVariantID: metafield(key: "fee-product-id", namespace: "$app:cart-transforms") {
      value
    }
  }
}

Here is the partial result when the function runs on the cart, as you can see the targetVariantID is null.

{ 
  "shopId": 66930835610,
  "apiClientId": 242488672257,
  "payload": {
    "export": "run",
    "input": {
      "cart": {
        "lines": [
          { 
            "id": "gid://shopify/CartLine/aa9bb533-1dde-49af-afde-d5ca6b35798a",
            "merchandise": {
              "id": "gid://shopify/ProductVariant/45677996048538",
              "sku": "EVT-665",
              "product": {
                "title": "My Panel"
              },
              "requiresShipping": false
            },
            "cost": {
              "amountPerQuantity": {
                "amount": "19.0"
              }
            }
          }
        ]
      },
      "cartTransform": {
        "targetVariantID": null
      }
    },

I did all of the work to add the metafield to the function via the Shopify GraphIQL app. Is there a more appropriate place to run that? I read a few posts that said it could be because $app for the GraphIQL app is different that what my app/extension would be using, is this true?

Thoughts?

1 Like

Hey @Timothy_Reed :waving_hand: - you are on point here. If you set this up in the GraphiQL app in the admin, the created function and the metafields would technically be associated with the GraphiQL app and not your Functions app extension.

If you did need to run the cartTransformCreate mutation for your app before setting up the Cart Transform function, the easiest way to do that would be to use an API client like Postman while using your Functions App’s authorization credentials to associate the created function and its metafields with your app rather than the GraphiQL app.

Hope this helps/makes sense - let me know if I can help out further!

@Alan_G Thank you for confirming my suspicion. I was following this page but didn’t see where it mentioned using the app’s token. It details how to use the GraphIQL app.

Anyway, I can confirm that was the fix. Grab the app’s token from the installation process and use that to create the cartTransform in Postman.

Thanks!

1 Like