Accessing cost_per_item from InventoryItem in Checkout Cart Validation Extension

Hi,
I’m working on a Checkout Cart Validation extension using the JavaScript version of the Shopify Functions, and I need to access the cost_per_item (unitCost) for the items in the cart.

A few specific questions:

Is it possible to access the cost_per_item from within the cartLines or any accessible object in the cart validation extension?

If not directly available, is there a recommended way to fetch or reference the related InventoryItem to get this data?

Can we enrich the extension inputs with metafields or additional data from the Admin API (e.g., tagging products with cost info)?

I’m using the JavaScript version of the Shopify extension framework, and I didn’t see unitCost exposed directly in the cart line data.

Any insights or suggestions would be greatly appreciated!

Hey @Maksym_Krauze :waving_hand: - I did a little digging on my end here, and you are correct, it doesn’t look like it’s possible to grab the unit cost via Cart Validation Functions (a bit more info here in our docs where we have a list of the input options on Cart Lines which is mainly input focused: Cart and Checkout Validation API reference)

My understanding is that since unit cost is generally tied to the inventory item and is more of an object related to fulfillment, it is excluded from the Validation API since in most cases we wouldn’t see the per unit pricing surfaced to a customer (just the cart line cost they’re paying upfront).

Your idea regarding metafields makes sense though, since we do expose metafield values in the Validation Functions API (more info here) - I think that would be the best workaround. You could set up a metafield like: namespace: “inventory”, key: “unit_cost” and then run something like this to pull the info in your input query:

query RunInput {
  cart {
    lines {
      quantity
      merchandise {
        __typename
        ... on ProductVariant {
          id
          product {
            metafield(namespace: "inventory", key: "unit_cost") {
              value 
            }
          }
        }
      }
    }
  }
}

Hope this helps - let me know if I can clarify anything on our end here :slight_smile: