Custom Liquid key-condition basics
How to create customer liquid key conditions in Locksmith
Locksmith allows you to create a custom key conditions that use Liquid to determine whether or not the current visitor has access. This gives you a few more options than what comes with Locksmith's out-of-the-box key conditions.
Before continuing on, you may want to check out Shopify's Liquid reference, as that will give you a good idea of the kinds of things you can use when creating Liquid key conditions. Keep in mind that while Liquid key conditions can add more versatility, they are still limited to the scope of what's already available inside of Shopify's Liquid engine for the Online Store channel.
To create a Liquid key condition, start by selecting it from the key condition list. You'll be presented with the key condition area for Liquid key conditions:

The "Liquid condition"
This is where you'll write the primary condition, using Liquid. The condition simply needs to evaluate to true or false.
For example, consider if you simply wrote the following:
{% if
false
%}
This would result in this particular key never opening, since it's always false. This is not necessarily advisable(it would be the equivalent of adding no keys at all), but it's a proof of concept.
So, here are some examples of useful conditions.
To check for a metafield
{% if customer.metafields.namespace.key == "matching-value" %}
To check if the customer has at least 5 items in the cart currently
{% if cart.item_count > 4 %}
To check if the customer has spent at least $50 in the past
{% if customer.total_spent > 5000 %}
To check if the current page is a using the "collection" template
{% if template == "collection" %}
To check for a specific sub-string inside of the current URL
{% if canonical_url contains "/special-product-handle" %}
To check for some attribute of the request object
{% if request.page_type == "index" %}
The "Liquid prelude"
This is a place for you to write any code that that you may need to set up the Liquid condition. It does not need to evaluate to true or false, but it does need to use valid Liquid syntax.
Basically, you can assign to any custom variable in the prelude, and then use that variable in the condition to ultimately decide whether or not the customer qualifies to use this key.
For example, your prelude could look something like this:
{% assign purchase_allowed = true %}
{% for item in cart.items %}
{% if item.product.handle contains "members-only" %}
{% assign purchase_allowed = false %}
{% endif %}
{% endfor %}
So now, in the "Liquid condition", you would have access to the purchase_allowed
variable, so it would simply look like this:
{% if purchase_allowed %}
So that would give you even more flexibility to write different Liquid conditions that can be used to set up your locks and keys.
Other resources
Relatedly, you can also use Liquid to create Locks! More information on that here:
Liquid locking basicsAs always, feel free to check in with us via email at [email protected], if you have any questions about any of this!
Last updated
Was this helpful?