Liquid、JavaScriptなどに関する質問
選択するコレクションによってcart attributeの表示を切り替えることはできませんでしょうか。
具体的には、cart-template.liquidに下記のようなコードを作りたいです。
{% if collection.title == 'A店' %}
<p class="cart-attribute__field">
<label>受取時間</label><br>
<input required class="required" type="radio" name="attributes[受取時間]" value="3時~4時"{% if cart.attributes["受取時間"] == "3時~4時" %} checked{% endif %}> <span>3時~4時</span><br>
<input required class="required" type="radio" name="attributes[受取時間]" value="4時~5時"{% if cart.attributes["受取時間"] == "4時~5時" %} checked{% endif %}> <span>4時~5時</span><br>
<input required class="required" type="radio" name="attributes[受取時間]" value="5時〜6時"{% if cart.attributes["受取時間"] == "5時〜6時" %} checked{% endif %}> <span>5時〜6時</span><br>
</p>
{% elsif collection.title == 'B店' %}
<p class="cart-attribute__field">
<label>受取時間</label><br>
<input required class="required" type="radio" name="attributes[受取時間]" value="3時~4時"{% if cart.attributes["受取時間"] == "3時~4時" %} checked{% endif %}> <span>3時~4時</span><br>
<input required class="required" type="radio" name="attributes[受取時間]" value="4時~5時"{% if cart.attributes["受取時間"] == "4時~5時" %} checked{% endif %}> <span>4時~5時</span><br>
<input required class="required" type="radio" name="attributes[受取時間]" value="5時〜6時"{% if cart.attributes["受取時間"] == "5時〜6時" %} checked{% endif %}> <span>5時〜6時</span><br>
</p>
{% elsif collection.title == 'C店' %}
<p class="cart-attribute__field">
<label>受取時間</label><br>
<input required class="required" type="radio" name="attributes[受取時間]" value="3時~4時"{% if cart.attributes["受取時間"] == "3時~4時" %} checked{% endif %}> <span>3時~4時</span><br>
<input required class="required" type="radio" name="attributes[受取時間]" value="4時~5時"{% if cart.attributes["受取時間"] == "4時~5時" %} checked{% endif %}> <span>4時~5時</span><br>
<input required class="required" type="radio" name="attributes[受取時間]" value="5時〜6時"{% if cart.attributes["受取時間"] == "5時〜6時" %} checked{% endif %}> <span>5時〜6時</span><br>
</p>
{% endif %}
上記コードではエラーになってしまいます。
どなたかアドバイスいただけませんでしょうか。
解決済! ベストソリューションを見る。
成功
カート画面のことかと思いますが、「選択するコレクション」というのは「カート内の商品が属しているコレクション」という意味でしょうか?
その場合、一応以下でコレクションのtitleは取れると思います。
{% assign collection_titles = '' %}
{% for item in cart.items %}
{% for collection in item.product.collections %}
{% assign collection_titles = collection_titles | append: "," | append: collection.title %}
{% endfor %}
{% endfor %}
{% assign collection_titles_array = collection_titles | remove_first: "," | split: ","%}
{% if collection_titles_array contains 'A店' %}
A店が含まれている場合
{% elsif collection_titles_array contains 'B店' %}
B店が含まれている場合
{% elsif collection_titles_array contains 'C店' %}
C店が含まれている場合
{% endif %}
cartオブジェクトからline iltemsを取り出し、さらにそのline itemsからproductを取り出して、product オブジェクトのcollectionsを取り出す、という流れです。
文字列をつなげただけでも事足りる条件かもしれませんが、一応配列として扱える形にして、その配列内に目的の文字列があれば分岐します。
参考:
https://shopify.dev/api/liquid/objects/cart#cart-items
https://shopify.dev/api/liquid/objects/line_item#line_item-product
https://shopify.dev/api/liquid/objects/product#product-collections
ただ、「コレクションA店に属する商品」「コレクションB店に属する商品」「コレクションC店に属する商品」の3種が同時にカートに入る状況も想定されますので、その場合この分岐では最初にあるA店の内容しか表示されません。
この状態が想定されている動作なら問題ありませんが、違う場合はご希望の状態を達成できるように仕組みから作り直す必要があるかもしれません。
または、アプリなどを使って指定コレクションに属する商品を同時にカートに入れられなくする方法も考えられますが、この方法に関しましてはフォーラムに既にスレッドがありますので検索いただければと思います。
以上ですが、もしもサンプルが動かない場合は申し訳ありません。
成功
カート画面のことかと思いますが、「選択するコレクション」というのは「カート内の商品が属しているコレクション」という意味でしょうか?
その場合、一応以下でコレクションのtitleは取れると思います。
{% assign collection_titles = '' %}
{% for item in cart.items %}
{% for collection in item.product.collections %}
{% assign collection_titles = collection_titles | append: "," | append: collection.title %}
{% endfor %}
{% endfor %}
{% assign collection_titles_array = collection_titles | remove_first: "," | split: ","%}
{% if collection_titles_array contains 'A店' %}
A店が含まれている場合
{% elsif collection_titles_array contains 'B店' %}
B店が含まれている場合
{% elsif collection_titles_array contains 'C店' %}
C店が含まれている場合
{% endif %}
cartオブジェクトからline iltemsを取り出し、さらにそのline itemsからproductを取り出して、product オブジェクトのcollectionsを取り出す、という流れです。
文字列をつなげただけでも事足りる条件かもしれませんが、一応配列として扱える形にして、その配列内に目的の文字列があれば分岐します。
参考:
https://shopify.dev/api/liquid/objects/cart#cart-items
https://shopify.dev/api/liquid/objects/line_item#line_item-product
https://shopify.dev/api/liquid/objects/product#product-collections
ただ、「コレクションA店に属する商品」「コレクションB店に属する商品」「コレクションC店に属する商品」の3種が同時にカートに入る状況も想定されますので、その場合この分岐では最初にあるA店の内容しか表示されません。
この状態が想定されている動作なら問題ありませんが、違う場合はご希望の状態を達成できるように仕組みから作り直す必要があるかもしれません。
または、アプリなどを使って指定コレクションに属する商品を同時にカートに入れられなくする方法も考えられますが、この方法に関しましてはフォーラムに既にスレッドがありますので検索いただければと思います。
以上ですが、もしもサンプルが動かない場合は申し訳ありません。
「選択するコレクション」というのは「カート内の商品が属しているコレクション」という意味で合っております!
教えていただいたコードで、コレクションのタイトルを取得し、取得したタイトルによってカート内の表示を切り替えることができました。
ありがとうございます!!
まだ試すことができていませんが、異なるコレクションがカート内に入らないようにするアプリについて過去Jizo_Inagaki 様が回答されていたスレッドを見つけました。異なるコレクションはカート入らないようにしたいので、こちら試してみたいと思います。ありがとうございます!
いつもShopifyをご利用いただき、ありがとうございます。 Shopifyは、皆様の日本語での利用体験の向上に努めております。さらなる改善のために皆様のご意見をお寄せい...
By JasonH May 9, 2025Shopify アカデミーの学習パスと認定スキルバッジExpanding Your Shopify Business Internationallyを活用して、国際的にビジネ...
By Shopify Feb 7, 2025Shopify アカデミーの学習パスB2B on Shopify:立ち上げとカスタマイズで卸売販売に進出しましょう。これら3つの無料コースは、ShopifyストアでB2B機能...
By Shopify Jan 31, 2025