Liquid、JavaScriptなどに関する質問
コレクションページにて、
コレクションに属する商品(全てのバリエーションを含む)の在庫数を合計した値を表示したいです。
{%- 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 id="variant-inventory">
{% 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 %}
</div>
主な変更点について
ご参考まで。
(キュー小坂)
成功
ご質問いただいている、コレクションでの商品合計表示の件ですが、
記載いただいていたコードを調整し、在庫数の合計が表示するよう調整しました。
下記調整コード
<div id="variant-inventory">
{% 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 %}
</div>
主な変更点について
ご参考まで。
(キュー小坂)
キュー小坂様
ご回答いただき、ありがとうございます!
いただいたコードで希望通りの表示ができ、非常に助かりました。
また、変更点など参考になりました。
この度は、ありがとうございました。
Shopifyペイメント決済サービスを利用していて、ストアの管理画面の通知セクションに突然「Shopifyペイメントの使用を継続するために必要な情報」というバナーメッセージが表示さ...
By Mirai Dec 3, 2023すべてのShopifyアカウントはデフォルトではmyshopify.comのURLと関連付けられており、これはアカウント設定時に使用したビジネス名に基づいて作成されます。しかし、オ...
By Nina_13 Nov 26, 2023このトピックは英語版コミュニティの投稿:Shopify Web Pixel Manager Sandbox FAQの日本翻訳です。
By Mirai Nov 19, 2023