Discussing Shopify Functions development, deployment, and usage in Shopify apps.
The basic goal of what it'm trying to achieve is detecting if a user has signed up as a Member on the site, and if not, require them to purchase a Membership product the first time they check out.
The general logic and behavior is:
- Inspect the customer to see if they have no previous orders, and/or have (or do not have) a specific tag (exact mechanism is TBD, but is likely to be one of these two things).
- If that inspection shows they are not a Member, show an error, prevent checkout, and if they don't already have a Membership product in their cart, display an Add button that will add the required Membership product to the cart.
I can easily do most of this with a Checkout Validation Function using buyerIdentity.customer.numberOfOrders. That can display the error message and prevent checkout. Great.
I can also use a simple Checkout UI Extension to show the Add button for the Membership product if it is not in the cart already. Great, almost.
What i cannot figure out how to do is link triggering the display of the Add button to the outcome of the Validation. I ONLY want to display the Add button if the customer is not a member (per data on the customer) and there is no Membership in the cart yet.
There doesn't seem to be a way to write back to the Cart during the Validation, and the customer data i want to inspect is not available to the Checkout UI Extension.
I feel like i may be missing something obvious here, but i can't figure out what. Is this something that should be possible? It seems like a very common use case, and in fact is mentioned in the "Example Use Cases" section here:
https://shopify.dev/docs/api/functions/reference/cart-checkout-validation
There is nothing showing how that might be done of course, so not a very useful list.
Solved! Go to the solution
This is an accepted solution.
Replying to my own topic...
The above does not appear to be possible. What i ended up doing was setting a metafield on the Customer using a Flow. We can then inspect the metafield value from both the Checkout Validation Function and a Checkout UI Extension.
To aid whoever might see this pop up in a search, to get the metafield included for the Checkout Validation Function, it needs to be included in the query defined in run.graphql. For the Checkout UI Extension, it needs to be specified in the .toml file under the [[extensions.metafields]] key.
This is an accepted solution.
Replying to my own topic...
The above does not appear to be possible. What i ended up doing was setting a metafield on the Customer using a Flow. We can then inspect the metafield value from both the Checkout Validation Function and a Checkout UI Extension.
To aid whoever might see this pop up in a search, to get the metafield included for the Checkout Validation Function, it needs to be included in the query defined in run.graphql. For the Checkout UI Extension, it needs to be specified in the .toml file under the [[extensions.metafields]] key.