Computed definitions

A computed metafield definition gets its value from other fields. You build the value from the following sources:

  • At least one existing compatible expression
  • Optional: Plain text

Considerations for computed metafield definitions

Review the following considerations for computed metafields:

  • You can add a computed metafield definition as a new structured metafield definition or as a field in a metaobject definition.
  • You can only compose a computed metafield for a single value of either of the following types:
    • Single line text
    • Multi-line text
  • Computed metafields aren't compatible with validations, such as maximum values or unique values only.
  • A computed value must include at least one expression, such as a resource attribute or a metafield. You can add plain text alongside expressions, but you can't save a value that contains only plain text.

Switch between composition modes

There are two composition modes: visual and Liquid. You can compose a computed expression in either mode, and switch between each mode at any point. By default, the visual mode is displayed.

In Liquid mode, you can add expressions by entering the expression's Liquid path or by clicking the Dynamic source icon and selecting an expression. You can also enter plain text.

Enter the expression's full Liquid path between double curly brackets {{ }}. For a resource attribute, enter the resource and the attribute. For example, {{ product.title }}. For a metafield, enter the resource, metafields, and the metafield's namespace and key. For example, {{ product.metafields.custom.subtitle }}. The namespace and key is displayed in the Name section of the metafield definition page. Click the Copy icon next to the namespace and key to copy it.

In visual mode, you can add expressions by clicking the Add icon, and selecting an expression. To add plain text, enter the text into the composition field.

The icon displayed to switch composition modes depends on the active mode. To switch between modes, click either of the following icons:

  • In visual mode, click the Edit Liquid icon to switch to Liquid mode.
  • In Liquid mode, click the Use visual editor icon to switch to visual mode.

Composing a computed metafield definition

To compose a computed definition, turn on the Compute this value from an expression option. An input field is then displayed so that you can compose the definition.

The Compute this value from an expression option is displayed only when you select a compatible metafield type. If you add validations to your metafield definition, then the Compute this value from an expression option is deactivated.

Steps:

  1. From your Shopify admin, go to Settings > Metafields and metaobjects.

  2. In the Metafield definitions section, select the part of your store that you want to add a metafield to. For example, Products.

  3. Click Add definition.

  4. In the Name field, enter a name for the definition.

  5. In the Type section, select Single line text or Multi-line text.

  6. In the Options section, turn on Compute this value from an expression.

  7. In the Compute this value from an expression field, compose the value in any of the following ways:

  • To add an existing expression:
    • In Liquid composition mode:
      • Click the Dynamic source icon, and then select an expression.
      • Enter the expression's definition using Liquid.
    • In visual editing mode, click the Add icon, and then select an expression.
  • To add plain text in either composition mode, enter the text in the composition field. Plain text must be combined with at least one expression.
  1. Optional: Add any other details to create your metafield definition.
  2. Click Save.

Add a computed field to a metaobject

You can add a computed metafield definition to a metaobject.

Steps:

  1. From your Shopify admin, go to Content > Metaobjects or Settings > Custom data.
  2. Click Add definition.
  3. In the Name field, enter a name for the definition.
  4. Enter a Field label.
  5. Click Select field type, and then select one of the following types:
  • Single line text.
  • Multi-line text.
  1. Click the arrow down icon.
  2. Click Computation, and then turn on Compute this value from an expression.
  3. In the Compute this value from an expression field, compose the value in any of the following ways:
  • To add an existing expression:
    • In Liquid composition mode:
      • Click the Dynamic source icon, and then select an expression.
      • Enter the expression's definition using Liquid.
    • In visual editing mode, click the Add icon, and then select an expression.
  • To add plain text in either composition mode, enter the text in the composition field. Plain text must be combined with at least one expression.
  1. Optional: Add any other details to build your metaobject.
  2. Optional: Click Add Add field to add any additional metafield definitions.
  3. Click Save.