Simple Default Field Values Using Liquid Code

Overview

Normally a Custom Field will print only if it has a value in it. In this tutorial, we will guide you through how to make a field print a default message if it doesn't have a value set. We can do this easily within our app just by editing a few lines of code.

1.) Create or have a field ready to edit

For this example, we created a number Custom Field called Shipping Time. This field will show the user how long the item usually takes to ship.

Creating.png

Since most of our products ship within one week, we want the default value to print "This product usually ships within 1 week." This will keep things simple and easy since we won't have to go through all our products and input a value. 

2.) Edit the field's liquid code

After creating the field you will be greeted to the Edit Custom Fields screen (If you already have a field ready, go to Configure Fields > Edit).

Edit_Code1.png

By default, the field has almost everything we need in the Edit Code section. We've simplified our example by removing the unnecessary HTML for clarity.

We want to print the field if it has a value in it, but we also want it to print something if it doesn't have any value. We can do this by using an if/else statement, see also Control Flow Tags.

Edit_Code2.png

We added an else statement that just says that the product ships in 1 week in plain text. We also wrote text around the normal field value that reads "This product usually ships within (field value) weeks."

{% if product.metafields.custom_fields["shipping_time"] != blank %}
      This product usually ships within {{ product.metafields.custom_fields["shipping_time"] }} weeks.
{% else %}
      This product usually ships within 1 week.
{% endif %}

DISCLAIMER: This code will only work if the machine name of your field is "shipping_time"

3.) Test your work!

Now if we input a value for our field on any product we will see the field on the store.

Content_Entry.png

Overridden_Value.png

And if we don't put a value into a field.

Default_Value.png

Default Field Values in Shopify Templates

custom_fields.png

If you are trying to troubleshoot an issue with your field, check your Shopify theme code. Your field should look like this. DISCLAIMER: As the file should state, DO NOT MODIFY THIS FILE DIRECTLY. IT WILL BE OVERWRITTEN. This is because when we Save files within the Custom Fields app, whatever was in the file previously gets overwritten.

If you installed Custom Fields correctly, you should see the Custom Fields include statement wherever you installed the app ie. product.liquid, page.liquid etc.

Using Spreadsheets to Manage Values

If you'd like the same value to appear on every product use the bulk export functionality, add your value to the spreadsheet, then use the bulk import to get the value into every product.

Additional Resources & Information

Shopify: Liquid Coding Documentation

Shopify: Control Flow Tags

Custom Fields: Theming Examples & Tutorials