# 同じ商品タイプを同時に購入できないようにしたい

**URL:** <https://community.shopify.com/t/topic/286418>\
**Category:** 技術的なQ＆A\
**Created:** [January 18, 2024, 8:35am UTC](https://community.shopify.com/t/topic/286418 "2024-01-18T08:35:59Z")\
**Posts on this page:** 5\
**Page:** 1

## AI-generated summary

**課題:**
Shopifyカートで特定の商品タイプの組み合わせを制限したい。具体的には:
- 商品グループAとBが同時にカートに入った場合
- 商品グループB同士の**異なる商品**が複数カートに入った場合

これらの条件でチェックアウトボタンを非表示にしたい。

**提案された解決策:**
NIIGAKI_HYUGA氏がLiquidコードを提供:
- `typeA_count`と`typeB_count`でカウント
- `typeB_unique_products`変数で商品グループBの異なる商品IDを追跡
- 商品IDをカンマ区切りで格納し、split/sizeメソッドでユニーク商品数を計算
- 条件に応じてチェックアウトボタンの表示/非表示を制御

**現状:**
質問者が実装を試したが、商品グループBの商品がカートに入った際、product.idに関係なくチェックアウトが常に表示される問題が発生。解決には至っていない状態で議論が継続中。

<div class="post-metadata">

**Author:** ![20220314](https://avatars.discourse-cdn.com/v4/letter/2/74df32/32.png) [@20220314](https://community.shopify.com/u/20220314)\
**Post date:** [January 18, 2024, 8:35am UTC](https://community.shopify.com/t/topic/286418/1 "2024-01-18T08:35:59Z")

</div>

下記の書き方で商品グループAと商品グループBが一緒にカートに入ったときに  
チェックアウトが表示されないようにしてるのですが、  
それにプラスして商品グループB同士の違う商品がカートに入ったときでもチェックアウトが表示されないようにすることは可能でしょうか？

{%- for item in cart.items -%}  
{%- if item.product.type == ‘商品グループA’ -%}  
{% assign typeA = true %}  
{%- endif -%}  
{%- if item.product.type == ‘商品グループB’ -%}  
{% assign typeB = true %}  
{%- endif -%}  
{%- endfor -%}  
{%- unless typeA == true and typeB == true -%}  
購入ボタン  
{%- endunless -%}

---

<div class="post-metadata">

**Author:** ![NIIGAKI\_HYUGA](https://avatars.discourse-cdn.com/v4/letter/n/f05b48/32.png) [@NIIGAKI\_HYUGA](https://community.shopify.com/u/NIIGAKI_HYUGA)\
**Post date:** [January 18, 2024, 1:16pm UTC](https://community.shopify.com/t/topic/286418/2 "2024-01-18T13:16:13Z")

</div>

@20220314 様

初めまして。新垣です。

こちらはいかがでしょうか。

```markup
{% assign typeA_count = 0 %}
{% assign typeB_count = 0 %}

{%- for item in cart.items -%}
  {%- if item.product.type == '商品グループA' -%}
    {% assign typeA_count = typeA_count | plus: 1 %}
  {%- elsif item.product.type == '商品グループB' -%}
    {% assign typeB_count = typeB_count | plus: 1 %}
  {%- endif -%}
{%- endfor -%}

{%- if typeA_count > 0 and typeB_count > 0 -%}
  #AタイプとBタイプの商品が同時に1点以上ある場合チェックアウトを非表示にする
{%- elsif typeB_count > 1 -%}
  #商品グループBが2つ以上ある場合もチェックアウトを非表示にする
{%- else -%}
  購入ボタン
{%- endif -%}

```

参考になりましたら幸いです。

---

<div class="post-metadata">

**Author:** ![20220314](https://avatars.discourse-cdn.com/v4/letter/2/74df32/32.png) [@20220314](https://community.shopify.com/u/20220314)\
**Post date:** [January 19, 2024, 12:42pm UTC](https://community.shopify.com/t/topic/286418/3 "2024-01-19T12:42:47Z")

</div>

参考になりましたありがとうございます。

商品グループB同士の同じproduct\_idの場合はチェックアウトを表示したいのですが可能でしょうか？

---

<div class="post-metadata">

**Author:** ![NIIGAKI\_HYUGA](https://avatars.discourse-cdn.com/v4/letter/n/f05b48/32.png) [@NIIGAKI\_HYUGA](https://community.shopify.com/u/NIIGAKI_HYUGA)\
**Post date:** [January 19, 2024, 11:25pm UTC](https://community.shopify.com/t/topic/286418/4 "2024-01-19T23:25:47Z")

</div>

下記のコードをお試しください。

```markup
{% assign typeA_count = 0 %}
{% assign typeB_count = 0 %}
{% assign typeB_unique_products = '' %}

{%- for item in cart.items -%}
  {%- if item.product.type == '商品グループA' -%}
    {% assign typeA_count = typeA_count | plus: 1 %}
  {%- elsif item.product.type == '商品グループB' -%}
    {% assign typeB_count = typeB_count | plus: 1 %}
    {% unless typeB_unique_products contains item.product.id %}// typeB_unique_productsにカート内itemのidが含まれていない場合
      {% assign typeB_unique_products = typeB_unique_products | append: item.product.id | append: ',' %} // typeB_unique_productsにカート内itemのidを格納
    {% endunless %}
  {%- endif -%}
{%- endfor -%}

// カートに含まれる商品グループBの異なる商品の数を計算
{%- assign typeB_unique_count = typeB_unique_products | split: ',' | size | minus: 1 -%}

{%- if typeA_count > 0 and typeB_count > 0 -%}
  #AタイプとBタイプの商品が同時に1点以上ある場合チェックアウトを非表示にする
{%- elsif typeB_unique_count > 1 -%}
  #商品グループBの異なる商品が2つ以上ある場合もチェックアウトを非表示にする
{%- else -%}
  購入ボタン
{%- endif -%}

```

// カートに含まれる商品グループBの異なる商品の数を計算の部分でminus:1をしているのは、splitメソッドによって追加される余分な空の要素を除外するためです。

---

<div class="post-metadata">

**Author:** ![20220314](https://avatars.discourse-cdn.com/v4/letter/2/74df32/32.png) [@20220314](https://community.shopify.com/u/20220314)\
**Post date:** [January 23, 2024, 3:22am UTC](https://community.shopify.com/t/topic/286418/5 "2024-01-23T03:22:56Z")

</div>

ありがとうございます。

試してみたのですが

typeBの商品が入ったときはidに関わらずチェックアウトが出る状態になってしまいました、、
