# ページでの条件分岐

**URL:** <https://community.shopify.com/t/topic/174665>\
**Category:** 技術的なQ＆A\
**Created:** [December 8, 2022, 11:18am UTC](https://community.shopify.com/t/topic/174665 "2022-12-08T11:18:35Z")\
**Posts on this page:** 4\
**Page:** 1

## AI-generated summary

カート内の特定タグ（「hoge」）が付いた商品が2個以上ある場合にチェックボックスを表示する条件分岐の実装方法についての質問。

**問題点：**
- `cart.item_count`では特定タグの商品数を取得できず、正しく条件分岐できない
- タグを含む商品が1つでもあればチェックボックスが表示されてしまう

**解決方法：**
- Liquidコードで`assign tag_count = 0`で変数を初期化
- `cart.items`をループし、`item.product.tags`に特定タグが含まれる場合、`item.quantity`を`tag_count`に加算
- `tag_count`が2以上の場合にチェックボックスを表示

同一商品の数量2でもカウントされる仕様で、質問者は無事解決し、assignを使った特定タグの数量取得方法を学習できたとのこと。スレッドは解決済みとしてマークされる予定。

<div class="post-metadata">

**Author:** ![hdm](https://avatars.discourse-cdn.com/v4/letter/h/8797f3/32.png) [@hdm](https://community.shopify.com/u/hdm)\
**Post date:** [December 8, 2022, 11:18am UTC](https://community.shopify.com/t/topic/174665/1 "2022-12-08T11:18:35Z")

</div>

特定のタグが付いた商品が2個以上（同一商品の数量2でも）の時にチェックボックスを表示させたくいろいろと試していますがうまくいきません。

{% for item in cart.items limit:1 %}  
{% for tag in item.product.tags %}  
{% if tag contains “hoge” %}  
{% if cart.item\_count \> 2 %}  
a  
{% endif %}  
{% endif %}  
{% endfor %}  
{% endfor %}

上記内容ですとhogeが一つでも含まれているとチェックボックスが出てしまいます。  
cart.item\_countをカート内のhogeの数が取得できればそちらに変えて条件分岐できないかと思いましたがわかりませんでした。

カート内のhogeの総数を取得できればいけそうな気がしましが何か良い方法がございましたらご教授いただけると助かります。

---

<div class="post-metadata">

**Author:** ![ogasawarakyohei](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/ogasawarakyohei/32/28415_2.png) [@ogasawarakyohei](https://community.shopify.com/u/ogasawarakyohei)\
**Post date:** [December 9, 2022, 6:29am UTC](https://community.shopify.com/t/topic/174665/2 "2022-12-09T06:29:44Z")

</div>

Hdmさん、はじめまして。

Tsunの小笠原と申します。

> 特定のタグが付いた商品が2個以上（同一商品の数量2でも）の時にチェックボックスを表示

下記の感じでどうでしょうか？

テストしてないので、うまく動かなったから申し訳ないです。

```markup
{%- liquid
  assign tag_count = 0

  for item in cart.items
    if item.product.tags contains 'hoge'
      assign tag_count = tag_count | plus: item.quantity
    endif
  endfor

  if tag_count >= 2
    comment
    チェックボックスを表示
    endcomment
  endif
-%}

```

cart.itemsをループして、product.tags内に特定のタグを含む場合に、tag\_countに足しています。

(同一商品でも2つあった場合を許容するということでしたので、商品数分足しています)

以上、よろしくお願いします。

---

<div class="post-metadata">

**Author:** ![hdm](https://avatars.discourse-cdn.com/v4/letter/h/8797f3/32.png) [@hdm](https://community.shopify.com/u/hdm)\
**Post date:** [December 9, 2022, 8:39am UTC](https://community.shopify.com/t/topic/174665/3 "2022-12-09T08:39:12Z")

</div>

上記内容で無事解決しました！

assignで特定のタグのitem.quantityを持ってくればよかったのですね。

とても勉強になりました。

ありがとうございます。

---

<div class="post-metadata">

**Author:** ![ogasawarakyohei](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/ogasawarakyohei/32/28415_2.png) [@ogasawarakyohei](https://community.shopify.com/u/ogasawarakyohei)\
**Post date:** [December 17, 2022, 12:47am UTC](https://community.shopify.com/t/topic/174665/4 "2022-12-17T00:47:06Z")

</div>

Hdmさん、無事解決したようでよかったです。

こちらのスレッドを解決済みにしていただくと、今後参照される方のためにもなりますので、よろしくお願いします。
