Shopify Liquidで変数に初期値を設定する

Setting initial values for variables in Shopify Liquid

小笠原 京平

"RuffRuff 予約販売" supports the reduction of opportunity losses that occur when products are out of stock, new products are not on sale, or are on backorder.
It's incredibly easy to create pre-orders on your Shopify store, allowing you to register an unlimited number of pre-order products and accept unlimited pre-orders. You can also restrict people from ordering pre-order items and regular items at the same time, or send notifications when the item will be back in stock if it is not yet determined.
There are many unique features that are not found in other pre-order sales apps, such as the "Pay Later" feature that allows you to collect payment before the product is shipped.


This is Ogasawara from Tsun Inc.!
While developing Shopify apps, I often write Liquid when developing theme extensions.
This time, I will introduce how to set the initial value of variables when handling variables, which are essential when writing Liquid.

Target audience

  • Those who have basic knowledge of Liquid

What are Shopify Liquid variables?

Shopify Liquid allows you to create variables using the assign tag.
Variables are used in the same way as in other programming languages.
For those with JavaScript experience, I think it will be easier to understand if you think of them as equivalent to var and let.

If you create a variable with the same name as a Liquid object, it will be overwritten, so be careful not to create a variable with the same name as a Liquid object.

For basic usage of assign, please refer to the official reference. https://shopify.dev/docs/api/liquid/tags/assign

Advantages of setting initial values for variables

There is basically no problem if you do not set an initial value when handling variables, but it is very advantageous when writing code to make it clear that some value has been set.
For example, if you want to handle nullable values in Liquid objects or JSON, you need to check that the values are not nil before comparing them. However, by setting an initial value, you no longer need to check that the values are not nil, and you can simplify your code.

How to set initial values for variables

Use the default filter.
It's simple to use: just use the filter together with the initial value for the variable you want to set an initial value for.
You can set initial values for empty, false, and nil values. https://shopify.dev/docs/api/liquid/filters/default

Set initial value of string

This is the basic usage of default filter.

{%- liquid
  assign your_message = your_json.message | default: 'default message'
-%}

Set initial value of numerical value

When setting the initial value of a numeric value, it is safe to force the conversion to a numeric type.
Liquid does not have a filter to convert to numeric type, but it is possible to convert to numeric type by adding 0. (Similar to multiplying by 1, etc.)

{%- liquid
  assign your_price = your_json.price | default: 1000 | plus: 0
-%}

Set the initial value of a boolean

Be careful with booleans.
The default filter has the behavior that the initial value is set when the variable is false, so if the initial value is set to true, it will always be true even if the variable value is false.
Therefore, if you want to set the initial value to true, set the allow_false option.
https://shopify.dev/docs/api/liquid/filters/default#default-allow_false

{%- liquid
  assign has_product = your_json.has_product | default: true, allow_false: true
-%}

Summary

This time, we introduced how to set initial values for variables in Shopify Liquid and its benefits.
It's a small topic, but I think I use it often when writing Liquid, so I'd be happy if it was useful.

Closing

Tsun Inc. uses Shopify to build stores and develop apps.
If you would like to build a store or develop an app, please contact Tsun Inc.

👆️目次はShopifyアプリ「RuffRuff 目次作成」を利用

👆️Table of contents by the Shopify app RuffRuff Table of Contents

Back to blog
  • Shopifyで領収書を発行する方法|Order Printerの設定手順・サンプルコード・おすすめアプリを解説

    Shopifyで領収書を発行する方法|Order Printerの設定手順・サンプルコード・お...

    Tsun ライターチーム

    Shopifyで日本式の領収書を発行する3つの方法を紹介します。無料のShopify Order Printerで使えるサンプルコード、適格簡易請求書の前提条件、日本向け領収書アプリ5選と運用の注意点を解説します。

    Shopifyで領収書を発行する方法|Order Printerの設定手順・サンプルコード・お...

    Tsun ライターチーム

    Shopifyで日本式の領収書を発行する3つの方法を紹介します。無料のShopify Order Printerで使えるサンプルコード、適格簡易請求書の前提条件、日本向け領収書アプリ5選と運用の注意点を解説します。

  • Shopifyアプリ「日本インボイス領収書」とは|税率別の端数処理と軽減税率の注記に対応

    What is the Shopify app "JP Invoice Receipt"? |...

    Scanoria

    JP Invoice Receipt (日本インボイス領収書) is an app that issues receipts from Shopify orders. It supports rounding per tax rate, notes for the reduced tax rate, and issuance by buyers themselves.

    What is the Shopify app "JP Invoice Receipt"? |...

    Scanoria

    JP Invoice Receipt (日本インボイス領収書) is an app that issues receipts from Shopify orders. It supports rounding per tax rate, notes for the reduced tax rate, and issuance by buyers themselves.

  • 【決定版】Shopifyアプリ おすすめ一覧:売上アップ・効率化・無料アプリまで徹底解説!

    [2026 Latest] Recommended Shopify Apps: A Compl...

    Tsunライターチーム

    A detailed, purpose-by-purpose guide to recommended apps that streamline Shopify operations. Covers customer acquisition, CVR improvement, blog extensions, CRM, workload reduction, and AI-powered apps. Curated information useful for beginners and...

    [2026 Latest] Recommended Shopify Apps: A Compl...

    Tsunライターチーム

    A detailed, purpose-by-purpose guide to recommended apps that streamline Shopify operations. Covers customer acquisition, CVR improvement, blog extensions, CRM, workload reduction, and AI-powered apps. Curated information useful for beginners and...

  • Shopify Canvasとは?できること・使い方・注意点を解説

    What is Shopify Canvas? Capabilities, how to us...

    Tsun ライターチーム

    Shopify Canvas, announced on October 1, 2026, is a design screen where you can edit your theme together with Sidekick while viewing every page of your store side by side....

    What is Shopify Canvas? Capabilities, how to us...

    Tsun ライターチーム

    Shopify Canvas, announced on October 1, 2026, is a design screen where you can edit your theme together with Sidekick while viewing every page of your store side by side....

  • STORESからShopifyへのデータ移行手順|注意点も紹介

    [2026 Edition] How to Migrate Data from STORES ...

    Tsunライターチーム

    For merchants considering a move from STORES to Shopify, this guide explains the differences in pricing and features between the two services, which data can and cannot be migrated, the...

    [2026 Edition] How to Migrate Data from STORES ...

    Tsunライターチーム

    For merchants considering a move from STORES to Shopify, this guide explains the differences in pricing and features between the two services, which data can and cannot be migrated, the...

  • 【2026年最新】Shopifyの手数料一覧!プラン別の料率と費用を抑えるポイント

    [Latest for 2026] List of Shopify fees! Rates b...

    Tsun ライターチーム

    Explains the types of fees charged on Shopify and lists the rates for the Basic, Grow, Advanced, and Plus plans. Also introduces cost simulations by monthly sales and four points...

    [Latest for 2026] List of Shopify fees! Rates b...

    Tsun ライターチーム

    Explains the types of fees charged on Shopify and lists the rates for the Basic, Grow, Advanced, and Plus plans. Also introduces cost simulations by monthly sales and four points...

1 of 6
  • 【ストアインタビュー後編】やりながら育てるShopify。「野菜をMOTTO」の試行錯誤:モンマルシェ株式会社 河野雄士さん

    [Store Interview, Part 2] Building and Growing ...

    テキサス・アユミ

    "Even when e-commerce is the main battlefield, e-commerce isn't the goal." An in-depth look at our interview with Yuji Kono of Mon Marché, whose brand "Yasai wo MOTTO" grew its...

    [Store Interview, Part 2] Building and Growing ...

    テキサス・アユミ

    "Even when e-commerce is the main battlefield, e-commerce isn't the goal." An in-depth look at our interview with Yuji Kono of Mon Marché, whose brand "Yasai wo MOTTO" grew its...

  • 【ストアインタビュー前編】 ECが主戦場でも、ECが目的ではない。「野菜をMOTTO」が貫くEC戦略:モンマルシェ株式会社 河野雄士さん

    [Store Interview, Part 1] Even When E-Commerce ...

    テキサス・アユミ

    "Even when e-commerce is the main battlefield, e-commerce isn't the goal." Part 1 of our interview with Yuji Kono of Mon Marché, whose brand "Yasai wo MOTTO" grew its e-commerce...

    [Store Interview, Part 1] Even When E-Commerce ...

    テキサス・アユミ

    "Even when e-commerce is the main battlefield, e-commerce isn't the goal." Part 1 of our interview with Yuji Kono of Mon Marché, whose brand "Yasai wo MOTTO" grew its e-commerce...

  • 【ストアインタビュー後編】取り返しのつかない失敗さえしなければ、全部学びになる。EC戦略と経営哲学:Beverich株式会社 代表取締役 木下慶さん

    [Store Interview, Part 2] As Long as You Don't ...

    テキサス・アユミ

    Kei Kinoshita, President of Beverich, discusses his strategy for splitting roles between Shopify and Amazon, how he uses the RuffRuff apps, and a management philosophy that turns failure into learning....

    [Store Interview, Part 2] As Long as You Don't ...

    テキサス・アユミ

    Kei Kinoshita, President of Beverich, discusses his strategy for splitting roles between Shopify and Amazon, how he uses the RuffRuff apps, and a management philosophy that turns failure into learning....

1 of 3
  • Our Editorial Process

    For readers new to e-commerce and Shopify, we write our articles with care to explain difficult commerce and marketing terms, along with how everything works from building a store through to selling, as “gently,” “clearly,” and “accurately” as we can.

    Learn more 
  • About the information on this site

    The information published here is based on Tsun Inc.’s own experience and research. While we make every effort to publish information that is as “current” and “accurate” as possible, we cannot fully guarantee its contents. Tsun Inc. accepts no liability whatsoever for any damages arising from this site. If you find an error in the information published here, we would be grateful if you would contact Tsun Inc.