Shopifyテーマに数量ルールを追加する
B2Bのカタログ用の数量ルールは、無料のShopifyテーマバージョン8.0.0以降のみでサポートされています。テーマを変更あるいは更新したくない場合、以下のコードをテーマに追加して数量ルールを表示させることができます。
商品カート数量
商品バリエーションのカート数量の値は、商品ページまたは特集商品セクションに表示されます。値は、Liquidを使用して取得できます。
Liquidの商品カート数量コードを追加する
テーマ内の以下のファイルにコードを追加して、カート数量をサポートできます。
-
main-product.liquid
またはこれに相当するファイル -
featured-product.liquid
またはこれに相当するファイル
手順:
管理画面から [オンラインストア] > [テーマ] の順に移動します。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
Shopifyアプリから [Store] をタップします。
[販売チャネル] セクションで、[オンラインストア] をタップします。
[テーマを管理する] をタップします。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
Shopifyアプリから [Store] をタップします。
[販売チャネル] セクションで、[オンラインストア] をタップします。
[テーマを管理する] をタップします。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
- 編集するファイルを開きます。
- ファイルの最下部に新しい行を作成し、以下のコードを追加します。
{% 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>
- [保存] をクリックします。
Javascriptのカート数量コードを追加する
バリエーションのカート数量が変更された際に、商品ページまたは特集商品セクションに表示される値を更新する必要があります。更新された値は、Javascriptコードを使用して取得できます。
theme.js
ファイルまたはこれに相当するファイル。
手順:
管理画面から [オンラインストア] > [テーマ] の順に移動します。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
Shopifyアプリから [Store] をタップします。
[販売チャネル] セクションで、[オンラインストア] をタップします。
[テーマを管理する] をタップします。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
Shopifyアプリから [Store] をタップします。
[販売チャネル] セクションで、[オンラインストア] をタップします。
[テーマを管理する] をタップします。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
-
theme.js
ファイルを開きます。 - ファイルの最下部に新しい行を作成し、以下のコードを追加します。
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;
});
- [保存] をクリックします。
数量ルール
商品バリエーションの数量ルールは、商品ページまたは特集商品セクションに表示されます。Liquidを使用してルールを取得できます。
Liquidの数量ルールコードを追加する
テーマ内の以下のファイルにコードを追加して、商品カート数量をサポートできます。
-
main-product.liquid
またはこれに相当するファイル -
featured-product.liquid
またはこれに相当するファイル
手順:
管理画面から [オンラインストア] > [テーマ] の順に移動します。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
Shopifyアプリから [Store] をタップします。
[販売チャネル] セクションで、[オンラインストア] をタップします。
[テーマを管理する] をタップします。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
Shopifyアプリから [Store] をタップします。
[販売チャネル] セクションで、[オンラインストア] をタップします。
[テーマを管理する] をタップします。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
- 編集するファイルを開きます。
- ファイルの最下部に新しい行を作成し、以下のコードを追加します。
{% 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>
-
{{- '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>
-
{{- 'products.product.quantity.maximum_of' | t: quantity: product.selected_or_first_available_variant.quantity_rule.max -}}
</span>
{%- endif -%}
</div>
- [保存] をクリックします。
JavaScriptの数量ルールコードを追加する
商品の各バリエーションに、固有の数量ルールを適用することができます。別のバリエーションを選択すると、商品ページまたは特集商品セクションに表示される数量ルールも更新する必要があります。更新された値は、Javascriptコードを使用して取得できます。
-
theme.js
またはこれに相当するファイル -
en.default.json
手順:
管理画面から [オンラインストア] > [テーマ] の順に移動します。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
Shopifyアプリから [Store] をタップします。
[販売チャネル] セクションで、[オンラインストア] をタップします。
[テーマを管理する] をタップします。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
Shopifyアプリから [Store] をタップします。
[販売チャネル] セクションで、[オンラインストア] をタップします。
[テーマを管理する] をタップします。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
-
theme.js
ファイルを開きます。 - ファイルの最下部に新しい行を作成し、以下のコードを追加します。
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;
});
- [保存] をクリックします。
ロケールを追加する
JSON翻訳文字列を追加する
手順:
管理画面から [オンラインストア] > [テーマ] の順に移動します。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
Shopifyアプリから [Store] をタップします。
[販売チャネル] セクションで、[オンラインストア] をタップします。
[テーマを管理する] をタップします。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
Shopifyアプリから [Store] をタップします。
[販売チャネル] セクションで、[オンラインストア] をタップします。
[テーマを管理する] をタップします。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
-
en.default.json
ファイルを開きます。 - ファイルの最下部に新しい行を作成し、以下のコードを追加します。
{
"products":{
"quantity":{
"minimum_of":"Minimum of ",
"maximum_of":"Maximum of ",
"multiples_of":"Increments of ",
"in_cart": " in cart"
}
}
}
- [保存] をクリックします。