FROM CACHE - jp_header
解決済

コレクションによってcart attributeの内容を変えて表示したい

machiko
遊覧客
15 0 2

 

選択するコレクションによって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 %}
    

 

 

上記コードではエラーになってしまいます。

どなたかアドバイスいただけませんでしょうか。

1 件の受理された解決策

Jizo_Inagaki
Shopify Partner
1006 380 697

成功

カート画面のことかと思いますが、「選択するコレクション」というのは「カート内の商品が属しているコレクション」という意味でしょうか?

その場合、一応以下でコレクションの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 | フリーランスのwebデザイナー
- テーマのカスタム承れます。
- 記載した回答で解決できましたらベストソリューションの承認をお願いします。
- DMや指名による対応はご依頼として有料でのみ承ります。

元の投稿で解決策を見る

2件の返信2

Jizo_Inagaki
Shopify Partner
1006 380 697

成功

カート画面のことかと思いますが、「選択するコレクション」というのは「カート内の商品が属しているコレクション」という意味でしょうか?

その場合、一応以下でコレクションの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 | フリーランスのwebデザイナー
- テーマのカスタム承れます。
- 記載した回答で解決できましたらベストソリューションの承認をお願いします。
- DMや指名による対応はご依頼として有料でのみ承ります。
machiko
遊覧客
15 0 2

「選択するコレクション」というのは「カート内の商品が属しているコレクション」という意味で合っております!

教えていただいたコードで、コレクションのタイトルを取得し、取得したタイトルによってカート内の表示を切り替えることができました。

ありがとうございます!!

 

まだ試すことができていませんが、異なるコレクションがカート内に入らないようにするアプリについて過去Jizo_Inagaki 様が回答されていたスレッドを見つけました。異なるコレクションはカート入らないようにしたいので、こちら試してみたいと思います。ありがとうございます!

https://community.shopify.com/c/%E6%8A%80%E8%A1%93%E7%9A%84%E3%81%AAQ-A/%E3%82%B3%E3%83%AC%E3%82%AF%...