# 基本にある条件に、除外する条件を追加する場合の記述について

**URL:** <https://community.shopify.com/t/topic/326626>\
**Category:** 技術的なQ＆A\
**Created:** [May 28, 2024, 5:02am UTC](https://community.shopify.com/t/topic/326626 "2024-05-28T05:02:36Z")\
**Posts on this page:** 7\
**Page:** 1

## AI-generated summary

**問題の概要:**
在庫数に応じて3パターンの文言を表示するコードがあり、特定のタグが付いた商品については在庫表示を非表示にしたいという要望。商品グリッドでの実装を希望。

**解決プロセス:**
- Takase_Rが`inventory_hidden`フラグを使用した条件分岐のコード例を提示
- 商品タグに特定のラベルが含まれる場合に`true`を設定し、在庫表示をスキップする仕組み

**追加の課題と解決:**
質問者が複数タグ対応のため`or`演算子を使用したところ、全商品が非表示になる問題が発生。

原因は条件式の記述方法:
- 誤: `{% if tag contains 'label-1' or 'label-2' %}`
- 正: `{% if tag contains 'label-1' or tag contains 'label-2' %}`

Takase_Rの修正案により、2つのタグに正常に適用され解決。`elsif`を使った記述方法も基本的には問題ないが、やや冗長になるとの補足あり。

**結果:** 質問者の希望通りの表示が実現し、解決済み。

<div class="post-metadata">

**Author:** ![Snownow](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@Snownow](https://community.shopify.com/u/Snownow)\
**Post date:** [May 28, 2024, 5:02am UTC](https://community.shopify.com/t/topic/326626/1 "2024-05-28T05:02:36Z")

</div>

ご閲覧頂きありがとうございます。

現在、在庫数に合わせた文言の表示が3パターンあるのですが、

特定のタグが付いた商品については、在庫数に合わせた文言を非表示にしたいと考えております。

どのような記述をすれば、ご存じの方いらっしゃいましたら、ご教授のほど宜しくお願い致します。

---

<div class="post-metadata">

**Author:** ![Takase\_R](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/takase_r/32/295219_2.png) [@Takase\_R](https://community.shopify.com/u/Takase_R)\
**Post date:** [May 29, 2024, 6:05am UTC](https://community.shopify.com/t/topic/326626/2 "2024-05-29T06:05:38Z")

</div>

はじめまして、Takaseと申します。

実際のコードを見ていないので正確な回答が難しいのですが、

商品ページでの文言の非表示であれば、下記のような記述で実現可能かと思います。

```markup
{% assign inventory_hidden = false %}

{% for tag in product.tags %}
  {% if tag contains 'label' %}
    {% assign inventory_hidden = true %}
  {% endif %}
{% endfor %}

{% if inventory_hidden == false %}
  {% comment %} 在庫数に合わせた文言をここに記述 {% endcomment %}
{% endif %}

```

---

<div class="post-metadata">

**Author:** ![Snownow](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@Snownow](https://community.shopify.com/u/Snownow)\
**Post date:** [May 30, 2024, 12:34am UTC](https://community.shopify.com/t/topic/326626/3 "2024-05-30T00:34:13Z")

</div>

Takase 様

ご教授をありがとうございます。

足らない情報の中、本当にありがとうございます。

1. 商品グリッドに表示したいと思っております。

2. (在庫数の合わせた文言の出力)実際のコードは以下の通りです。

```markup
{% if product.variants.first.inventory_management == "shopify" and product.variants.first.inventory_quantity <= 0 %}
        {{ 'products.product.out_of_stock' | t }}

    {% elsif product.variants.first.inventory_management == "shopify" and product.variants.first.inventory_quantity <= 5 %}
        {{ 'products.product.low_stock_custom' | t }}

    {% elsif product.variants.first.inventory_management == "shopify" and product.variants.first.inventory_quantity >= 6 %}
        {{ 'products.product.in_stock' | t }}

    {% endif %}

```

もし可能でしたら、もう一度お願い致します。

---

<div class="post-metadata">

**Author:** ![Takase\_R](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/takase_r/32/295219_2.png) [@Takase\_R](https://community.shopify.com/u/Takase_R)\
**Post date:** [May 30, 2024, 4:22am UTC](https://community.shopify.com/t/topic/326626/4 "2024-05-30T04:22:18Z")

</div>

追記いただいた内容を踏まえると、下記のようなコードに書き換えることで実現できるかと思われます。

（labelは任意のタグに変更してください）

```markup
{% assign inventory_hidden = false %}

{% for tag in product.tags %}
  {% if tag contains 'label' %}
    {% assign inventory_hidden = true %}
  {% endif %}
{% endfor %}

{% if inventory_hidden == false %}
  {% if product.variants.first.inventory_management == "shopify" and product.variants.first.inventory_quantity <= 0 %}
    {{ 'products.product.out_of_stock' | t }}

  {% elsif product.variants.first.inventory_management == "shopify" and product.variants.first.inventory_quantity <= 5 %}
    {{ 'products.product.low_stock_custom' | t }}

  {% elsif product.variants.first.inventory_management == "shopify" and product.variants.first.inventory_quantity >= 6 %}
    {{ 'products.product.in_stock' | t }}

  {% endif %}
{% endif %}

```

---

<div class="post-metadata">

**Author:** ![Snownow](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@Snownow](https://community.shopify.com/u/Snownow)\
**Post date:** [June 4, 2024, 4:19am UTC](https://community.shopify.com/t/topic/326626/5 "2024-06-04T04:19:03Z")

</div>

Takase 様

返信、誠にありがとうございます。

ご教授頂きました、コードでイメージ通りの表示になりました！

因みになりますが、特定のタグが複数あるので、下のように”or”を使用したのですが、

特定のタグ以外の **全ての商品が非表示** なりました。

```markup
{% assign inventory_hidden = false %}

{% for tag in product.tags %}
  {% if tag contains 'label-1' or 'label-2' %}
    {% assign inventory_hidden = true %}
  {% endif %}
{% endfor %}

```

下記のように"elsif"にしたら、正常に **2つのタグだけ非表示** なりました。

これについて、何かお判りになりますか？

```markup
{% assign inventory_hidden = false %}

{% for tag in product.tags %}
  {% if tag contains 'label-1' %}
    {% assign inventory_hidden = true %}
  {% if tag contains 'label-2' %}
    {% assign inventory_hidden = true %}
  {% endif %}
{% endfor %}

```

追加で、大変恐縮ではございますが宜しければご教授お願い致します。

---

<div class="post-metadata">

**Author:** ![Takase\_R](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/takase_r/32/295219_2.png) [@Takase\_R](https://community.shopify.com/u/Takase_R)\
**Post date:** [June 4, 2024, 6:30am UTC](https://community.shopify.com/t/topic/326626/6 "2024-06-04T06:30:56Z")

</div>

無事イメージ通りの表示になったようでよかったです。

> > これについて、何かお判りになりますか？

{% if tag contains ‘label-1’ or ‘label-2’ %}

の書き方に問題があるように見えるので

{% if tag contains ‘label-1’ or tag contains ‘label-2’ %}

にすればもしかするとうまく表示されるかもしれません。

（試していないので確証はないのですが）

少し書き方は煩雑にはなりますが、elsifを使った記述方法で基本的には問題ないとは思います。

---

<div class="post-metadata">

**Author:** ![Snownow](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@Snownow](https://community.shopify.com/u/Snownow)\
**Post date:** [June 7, 2024, 2:09am UTC](https://community.shopify.com/t/topic/326626/7 "2024-06-07T02:09:22Z")

</div>

Takase様

返信、本当にありがとうございます！

試してみたところ、Takase様の記述で２つのタグに適応されました！

勉強になりました。

ありがとうございました。
