Shopifyテーマに数量ルールを追加する

B2Bのカタログ用の数量ルールは、無料のShopifyテーマバージョン8.0.0以降のみでサポートされています。テーマを変更あるいは更新したくない場合、以下のコードをテーマに追加して数量ルールを表示させることができます。

商品カート数量

商品バリエーションのカート数量の値は、商品ページまたは特集商品セクションに表示されます。値は、Liquidを使用して取得できます。

Liquidの商品カート数量コードを追加する

テーマ内の以下のファイルにコードを追加して、カート数量をサポートできます。

  • main-product.liquidまたはこれに相当するファイル
  • featured-product.liquidまたはこれに相当するファイル

手順:

  1. 編集するファイルを開きます。
  2. ファイルの最下部に新しい行を作成し、以下のコードを追加します。
{% comment %} Cart quantity {% endcomment %}
<span id="CartQuantity-{{ section.id }}" data-product-url="{{ product.url }}" data-section-id="{{ section.id }}" data-variant-id="{{ product.selected_or_first_available_variant.id }}">
    {%- assign cart_qty = cart | item_count_for_variant: product.selected_or_first_available_variant.id -%}
    {{- 'products.product.quantity.in_cart' | t: quantity: cart_qty -}}
</span>
  1. [保存] をクリックします。

Javascriptのカート数量コードを追加する

バリエーションのカート数量が変更された際に、商品ページまたは特集商品セクションに表示される値を更新する必要があります。更新された値は、Javascriptコードを使用して取得できます。

theme.jsファイルまたはこれに相当するファイル。

手順:

  1. theme.jsファイルを開きます。
  2. ファイルの最下部に新しい行を作成し、以下のコードを追加します。
let productUrl = document.querySelector('[data-product-url]').dataset.productUrl;
let sectionId = document.querySelector('[data-section-id]').dataset.sectionId;
let variantId = document.querySelector('[data-variant-id]').dataset.variantId;

// Fetch updated HTML from Section Rendering API
fetch(`${productUrl}?section_id=${sectionId}&variant=${variantId}`)
    .then((response) => response.text())
    .then((responseText) => {
        // Replace the current HTML in DOM with the updated HTML

        const updatedHtml = new DOMParser().parseFromString(responseText, 'text/html');

        // Update the cart quantity
        const currentCartQuantity = document.querySelector(`#CartQuantity-${sectionId}`);
        const updatedCartQuantity = updatedHtml.querySelector(`#CartQuantity-${sectionId}`);
        currentCartQuantity.innerHTML = updatedCartQuantity.innerHTML;
    });
  1. [保存] をクリックします。

数量ルール

商品バリエーションの数量ルールは、商品ページまたは特集商品セクションに表示されます。Liquidを使用してルールを取得できます。

Liquidの数量ルールコードを追加する

テーマ内の以下のファイルにコードを追加して、商品カート数量をサポートできます。

  • main-product.liquidまたはこれに相当するファイル
  • featured-product.liquidまたはこれに相当するファイル

手順:

  1. 編集するファイルを開きます。
  2. ファイルの最下部に新しい行を作成し、以下のコードを追加します。
{% comment %} Quantity rules {% endcomment %}
<div id="QuantityRules-{{ section.id }}" data-product-url="{{ product.url }}" data-section-id="{{ section.id }}" data-variant-id="{{ product.selected_or_first_available_variant.id }}">
    {%- if product.selected_or_first_available_variant.quantity_rule.increment > 1 -%}
        <span>
        {{- 'products.product.quantity.multiples_of' | t: quantity: product.selected_or_first_available_variant.quantity_rule.increment -}}
        </span>
    {%- endif -%}
    {%- if product.selected_or_first_available_variant.quantity_rule.min > 1 -%}
        <span>
        &nbsp;-&nbsp;
        {{- 'products.product.quantity.minimum_of' | t: quantity: product.selected_or_first_available_variant.quantity_rule.min -}}
        </span>
    {%- endif -%}
    {%- if product.selected_or_first_available_variant.quantity_rule.max != null -%}
        <span>
        &nbsp;-&nbsp;
        {{- 'products.product.quantity.maximum_of' | t: quantity: product.selected_or_first_available_variant.quantity_rule.max -}}
        </span>
    {%- endif -%}
</div>
  1. [保存] をクリックします。

JavaScriptの数量ルールコードを追加する

商品の各バリエーションに、固有の数量ルールを適用することができます。別のバリエーションを選択すると、商品ページまたは特集商品セクションに表示される数量ルールも更新する必要があります。更新された値は、Javascriptコードを使用して取得できます。

  • theme.jsまたはこれに相当するファイル
  • en.default.json

手順:

  1. theme.jsファイルを開きます。
  2. ファイルの最下部に新しい行を作成し、以下のコードを追加します。
let productUrl = document.querySelector('[data-product-url]').dataset.productUrl;
let sectionId = document.querySelector('[data-section-id]').dataset.sectionId;
let variantId = document.querySelector('[data-variant-id]').dataset.variantId;
// `variantId` is set to the current variant's id. Replace this value with the updated variant's id

// Fetch updated HTML from Section Rendering API
fetch(`${productUrl}?section_id=${sectionId}&variant=${variantId}`)
    .then((response) => response.text())
    .then((responseText) => {
        // Replace the current HTML in DOM with the updated HTML

        const updatedHtml = new DOMParser().parseFromString(responseText, 'text/html');

        // Update the quantity rules
        const currentQuantityRules = document.querySelector(`#QuantityRules-${sectionId}`);
        const updatedQuantityRules = updatedHtml.querySelector(`#QuantityRules-${sectionId}`);
        currentQuantityRules.innerHTML = updatedQuantityRules.innerHTML;
    });
  1. [保存] をクリックします。

ロケールを追加する

JSON翻訳文字列を追加する

手順:

  1. en.default.jsonファイルを開きます。
  2. ファイルの最下部に新しい行を作成し、以下のコードを追加します。
{
   "products":{
      "quantity":{
         "minimum_of":"Minimum of ",
         "maximum_of":"Maximum of ",
         "multiples_of":"Increments of ",
         "in_cart": " in cart"
      }
   }
}
  1. [保存] をクリックします。

Shopifyで販売を開始する準備はできていますか?

無料体験を試す