# コレクションの全ての在庫数を合算して表示したい

**URL:** <https://community.shopify.com/t/topic/191438>\
**Category:** 技術的なQ＆A\
**Tags:** css\
**Created:** [February 13, 2023, 9:14am UTC](https://community.shopify.com/t/topic/191438 "2023-02-13T09:14:12Z")\
**Posts on this page:** 3\
**Page:** 1

## AI-generated summary

コレクションページで、そのコレクションに属する全商品の全バリエーションの在庫数を合計して表示する方法についての質問です。

**当初の問題:**
- 各商品の在庫数が個別に表示されてしまい、コレクション全体での合計値が表示できない状態でした。
- 在庫がない場合は「Sold Out」と表示したい要望もありました。

**解決方法:**
Qcoltd(キュー小坂)が調整コードを提供し、以下の変更を実施:
1. コレクション全体の合計用変数 `{% assign totals = 0 %}` を追加
2. 各商品のバリエーション処理後に `{% assign totals = totals | plus: total %}` で値を加算
3. 在庫数に応じた分岐処理を追加(在庫あり/Sold Out)

**結果:**
質問者(g_ouc)は提供されたコードで希望通りの表示が実現でき、解決したことを報告しました。

<div class="post-metadata">

**Author:** ![g\_ouc](https://avatars.discourse-cdn.com/v4/letter/g/b782af/32.png) [@g\_ouc](https://community.shopify.com/u/g_ouc)\
**Post date:** [February 13, 2023, 9:14am UTC](https://community.shopify.com/t/topic/191438/1 "2023-02-13T09:14:12Z")

</div>

コレクションページにて、  
コレクションに属する商品（全てのバリエーションを含む）の在庫数を合計した値を表示したいです。

```auto
{%- for product in collection.products -%}
<div id="variant-inventory">
   {% assign total = 0 %}
    {% for variant in product.variants %}
     {% capture i %}{{ total | plus:variant.inventory_quantity }}{% endcapture %}
      {% assign total = i %}
    {% endfor %}
	{%- if product.available -%}
      残り在庫 {{ total }} 個
	{%- else -%}
      Sold Out
	{%- endif -%}
</div>
{%- endfor -%}

```

コレクション内の各商品の在庫数を下記のように表示することができました。  
残り在庫 7 個  
残り在庫 1 個  
残り在庫 7 個  
Sold Out  
残り在庫 3 個  
残り在庫 1 個  
残り在庫 1 個

実現したいのは、下記のように1行でまとめて表示したいです。

残り在庫 20 個

また、コレクションに1つも在庫がないときにも、下記のように1行だけ表示したいです。  
Sold Out

ご存じの方がおられましたら、ご教示いただけますと幸いです。  
よろしくお願いいたします。

---

<div class="post-metadata">

**Author:** ![Qcoltd](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/qcoltd/32/900_2.png) [@Qcoltd](https://community.shopify.com/u/Qcoltd)\
**Post date:** [February 16, 2023, 10:35am UTC](https://community.shopify.com/t/topic/191438/2 "2023-02-16T10:35:16Z")

</div>

ご質問いただいている、コレクションでの商品合計表示の件ですが、

記載いただいていたコードを調整し、在庫数の合計が表示するよう調整しました。

**下記調整コード**

```markup

{% assign totals = 0 %}
{%- for product in collection.products -%}
  {% assign total = 0 %}
  {% for variant in product.variants %}
    {% capture i %}{{ total | plus:variant.inventory_quantity }}{% endcapture %}
    {% assign total = i %}
  {% endfor %}
  {% assign totals = totals | plus: total %}
{%- endfor -%}
{% if totals != 0 %}
  残り在庫 {{ totals }} 個
{% else %}
  Sold Out
{% endif %}

```

**主な変更点について**

1. 合計用の関数\*\*{% assign totals = 0 %}\*\*を追加
2. \*\*{% assign totals = totals | plus: total %} **で** {% for variant in product.variants %}\*\*の処理で出来た値を加算
3. \*\*{%- for product in collection.products -%}\*\*の外で、残り在庫数の分岐処理を追加

ご参考まで。

(キュー小坂)

---

<div class="post-metadata">

**Author:** ![g\_ouc](https://avatars.discourse-cdn.com/v4/letter/g/b782af/32.png) [@g\_ouc](https://community.shopify.com/u/g_ouc)\
**Post date:** [February 17, 2023, 1:21am UTC](https://community.shopify.com/t/topic/191438/3 "2023-02-17T01:21:57Z")

</div>

キュー小坂様

ご回答いただき、ありがとうございます！

いただいたコードで希望通りの表示ができ、非常に助かりました。  
また、変更点など参考になりました。

この度は、ありがとうございました。
