0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Shopifyのliquidでは四則演算ができない

Last updated at Posted at 2021-02-17

四則演算とは

足し算・引き算・掛け算・割り算のことです。

やりたいこと

  • 割引金額を算出したい
  • 割引率を算出したい

できなかった

こんな感じで算出したかったけどできなかった。

{%- assign deff_price = product.compare_at_price_max - product.price -%}
{{ deff_price }}

Shopifyのliquidでの計算方法

Shopifyの計算では以下のMath filtersを使います。

  • plus(足し算)
  • minus(引き算)
  • times(掛け算)
  • divided_by(割り算)

金額を表示するときはMoney filtersも便利。

productから出している金額の違いはThe product objectを参照。

割引金額を算出


{% if product.compare_at_price_max > product.price %}
    {{ product.compare_at_price_max | minus: product.price}}
{% endif %}

割引率を算出


{% if product.compare_at_price_max > product.price %}
    {{ product.compare_at_price | minus: product.price | times: 100.0 | divided_by: product.compare_at_price }}%
{% endif %}

割引率を四捨五入

round フィルターをつけたら四捨五入できます。
round フィルター


{% if product.compare_at_price_max > product.price %}
    {{ product.compare_at_price | minus: product.price | times: 100.0 | divided_by: product.compare_at_price | round }}%
{% endif %}

参考

https://shopify.dev/docs/themes/liquid/reference/filters/math-filters
https://shopify.dev/docs/themes/liquid/reference/filters/money-filters
https://qiita.com/keninph1/items/350fd307e8c073ae6551
https://www.shopify.com/partners/blog/shopify-filters

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?