# 商品ごとに注文完了メールの文章を変える方法

**URL:** <https://community.shopify.com/t/topic/146402>\
**Category:** 総合的なディスカッション\
**Created:** [August 26, 2022, 3:27am UTC](https://community.shopify.com/t/topic/146402 "2022-08-26T03:27:38Z")\
**Posts on this page:** 5\
**Page:** 1

## AI-generated summary

目的: 注文確認メールの本文を、購入された商品のタグ（例: クリスマス、ひな祭り）に応じて切り替えたい。

問題点: テンプレート内で product.tags に直接条件分岐を書いても反映されない。

提案された解決策:
- 注文メールでは単一の product ではなく、明細ごとのデータを見る必要がある。
- line_items（または subtotal_line_items）をループし、各 line から line.product.tags を参照して判定する。
- 参考: Shopify ヘルプの通知メール変数（LINE ITEM PROPERTIES）を確認。

実装時のつまずきと修正ポイント:
- line_items ループの内側でさらに tags をループしつつ、同じ条件分岐を内側に置いたため、同じ文面がタグ数ぶん繰り返し表示された。
- 対処: 条件分岐は tags のループ外に出す、もしくは tags のループ自体を省略して contains 判定だけにする（必要に応じてフラグや break 相当の制御）。

結果/ステータス: 投稿者が問題を解決したと報告。コード修正で意図通り1行のみ表示されるようになった。議論は解決済み。

補足: コードスニペットの構造理解（配列/ループ）が解決の鍵。

<div class="post-metadata">

**Author:** ![ponuco](https://avatars.discourse-cdn.com/v4/letter/p/b487fb/32.png) [@ponuco](https://community.shopify.com/u/ponuco)\
**Post date:** [August 26, 2022, 3:27am UTC](https://community.shopify.com/t/topic/146402/1 "2022-08-26T03:27:38Z")

</div>

イベント商品が注文されたときはメールの文章が変わるように、商品タグを使って条件分岐を書いてみたのですが、メールに反映されません。

過去の記事等を参考に書いたのですが、コードが間違っているのでしょうか？

（追加したコードは

〜
の部分です。）

解決方法についてご存じの方がいらっしゃいましたらご教授いただけますと幸いです。

宜しくお願い致します。

```markup
{% capture email_title %}ご購入いただきありがとうございました! {% endcapture %}
{% capture email_body %}

      
  {% if product.tags contains 'クリスマス' %}
   ### メリークリスマス
  {%- endif -%}
  
  {% if product.tags contains 'ひな祭り' %}
   ### ひな祭り
  {%- endif -%}
    

  {% if requires_shipping %}
  {% case delivery_method %}
      {% when 'pick-up' %}
        ご注文の受取の準備が整いましたら、こちらのメールでお知らせいたします。
      {% when 'local' %}
        {{ customer.last_name }}様、ご注文の品を配達する準備を行っております。
      {% else %}
        {{ customer.last_name }}様、ご注文いただき、誠にありがとうございます。注文の発送準備を行なっております。商品が発送されましたら、Eメールにてお知らせいたします。
    {% endcase %}

```

---

<div class="post-metadata">

**Author:** ![\_osamu\_iwasaki](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/_osamu_iwasaki/32/272617_2.png) [@\_osamu\_iwasaki](https://community.shopify.com/u/_osamu_iwasaki)\
**Post date:** [August 26, 2022, 7:16am UTC](https://community.shopify.com/t/topic/146402/2 "2022-08-26T07:16:06Z")

</div>

購入データの構造はまずラインアイテムを見ないとどの商品かわからないので、

**line\_items** か **subtotal\_line\_items** のループで **line** を取得し、

**line.product.tags** のループで **tag** を取得できます。

※ヘルプには line.product.tags の記述はないのですが、構造的にはあるはずです。

**▼LINE ITEM PROPERTIES**

[https://help.shopify.com/ja/manual/orders/notifications/email-variables#line-item-properties](https://help.shopify.com/ja/manual/orders/notifications/email-variables#line-item-properties)

**▼購入した商品のタグ取得例**

{% for line in line\_items %}  
{% for tag in line.product.tags %}  
#{{ tag }}  
{% endfor %}  
{% endfor %}

---

<div class="post-metadata">

**Author:** ![ponuco](https://avatars.discourse-cdn.com/v4/letter/p/b487fb/32.png) [@ponuco](https://community.shopify.com/u/ponuco)\
**Post date:** [August 26, 2022, 9:32am UTC](https://community.shopify.com/t/topic/146402/3 "2022-08-26T09:32:18Z")

</div>

ご連絡いただきありがとうございます！

取得例を参考にコードを入力してみたところ、

タグのテキストは表示されるようになったのですが

タグが3行表示されてしまいました。

1行だけ表示されれば十分なのですが、どうすればいいでしょうか？

もしわかれば教えていただきたいです…！

```markup
{% capture email_title %}ご購入いただきありがとうございました! {% endcapture %}
{% capture email_body %}

   
  {% for line in line_items %}
　　　　{% for tag in line.product.tags %}
    
  {% if line.product.tags contains 'クリスマス' %}
   ### メリークリスマス
  {%- endif -%}   
      
  {% if line.product.tags contains 'ひな祭り' %}
   ### ひな祭り
  {%- endif -%}

　　　{% endfor %}
　　　{% endfor %}
 

```

 ![スクリーンショット 2022-08-26 18.30.16.png](https://canada1.discourse-cdn.com/shopifyforum/original/4X/a/1/f/a1fdb7459e37ee0b248fd92496dd36a7924c990c.png)

---

<div class="post-metadata">

**Author:** ![\_osamu\_iwasaki](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/_osamu_iwasaki/32/272617_2.png) [@\_osamu\_iwasaki](https://community.shopify.com/u/_osamu_iwasaki)\
**Post date:** [August 27, 2022, 12:55pm UTC](https://community.shopify.com/t/topic/146402/4 "2022-08-27T12:55:32Z")

</div>

単純な間違い探しです。あえて答えは言いませんので、配列が何を保持して、何を繰り返しているのかよく考えてみてください。

---

<div class="post-metadata">

**Author:** ![ponuco](https://avatars.discourse-cdn.com/v4/letter/p/b487fb/32.png) [@ponuco](https://community.shopify.com/u/ponuco)\
**Post date:** [August 29, 2022, 1:17am UTC](https://community.shopify.com/t/topic/146402/5 "2022-08-29T01:17:15Z")

</div>

解決できました！ありがとうございました。
