ストアに数量ルールと量ベースの価格を表示する
ストアのテーマを最新バージョンに更新すると、B2Bカタログの数量ルールと量ベースの価格をストアに表示できます。
テーマの変更や更新を行わない場合は、テーマに以下のコードを追加して、数量ルールと量ベースの価格を表示することができます。
テーマコードに数量ルールと量ベースの価格を追加する際の考慮事項
テーマコードに数量ルールと量ベースの価格を追加する前に、以下の考慮事項を確認してください。
- これは高度なチュートリアルです。テーマコードの読み取りや編集に慣れていない場合は、開発者と協力するか、Shopifyパートナーを雇うことができます。
- テーマファイルを更新する前に、必ずテーマを複製してバックアップコピーを作成してください。
- B2Bカタログの数量ルールは、バージョン 8.0.0 以降の無料の Shopify テーマでサポートされています。量ベースの価格は、バージョン 11.0.0 以降でサポートされています。
商品のカートの数量
商品のバリエーションのカート数量の値は、商品ページまたは特集商品セクションに表示できます。この値は Liquid を使用して取得できます。
Liquid の商品のカート数量コードを追加する
カート数量をサポートするために、テーマ内の以下のファイルにコードを追加できます。
main-product.liquidまたは同等のファイルfeatured-product.liquidまたは同等のファイル
手順:
Shopifyの管理画面から、[オンラインストア] > [テーマ] の順に移動します。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
編集するファイルを開きます。
ファイルの末尾に新しい行を作成し、次のコードを追加します。
{% 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 }}">
{{ cart | line_items_for: product | sum: 'quantity' }}
{{- 'products.product.quantity.in_cart' | t: quantity: cart_qty -}}
</span>- [保存] をクリックします。
Javascriptのカート数量コードを追加する
バリエーションのカート数量が変更されると、商品ページまたは特集商品セクションに表示されている値を更新する必要があります。更新された値は、Javascriptコードを使用して取得できます。
theme.js ファイルまたは同等のファイルにコードを追加できます。
手順:
Shopifyの管理画面から、[オンラインストア] > [テーマ] の順に移動します。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
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の管理画面から、[オンラインストア] > [テーマ] の順に移動します。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
編集するファイルを開きます。
ファイルの末尾に新しい行を作成してから、次のコードを追加します。
{% 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の管理画面から、[オンラインストア] > [テーマ] の順に移動します。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
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;
});- [保存] をクリックします。
量ベースの価格
Liquid の量ベースの価格コードを追加する
量ベースの価格を表示するために、テーマ内の以下のファイルにコードを追加できます。
main-product.liquidまたは同等のファイルfeatured-product.liquidまたは同等のファイル
手順:
Shopifyの管理画面から、[オンラインストア] > [テーマ] の順に移動します。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
編集するファイルを開きます。
ファイルの末尾に新しい行を作成してから、次のコードを追加します。
{%- if product.quantity_price_breaks_configured? -%}
<div class="volume-pricing-note">
<span>{{ 'products.product.volume_pricing.note' | t }}</span>
</div>
<volume-pricing class="parent-display" id="Volume-{{ section.id }}">
{%- if product.selected_or_first_available_variant.quantity_price_breaks.size > 0 -%}
<span class="caption-large">{{ 'products.product.volume_pricing.title' | t }}</span>
<ul class="list-unstyled no-js-hidden">
<li>
<span>{{ product.selected_or_first_available_variant.quantity_rule.min }}+</span>
{%- assign price = product.selected_or_first_available_variant.price
| money_with_currency
-%}
<span data-text="{{ 'products.product.volume_pricing.price_at_each' | t: price: variant_price }}">
{{ 'sections.quick_order_list.each' | t: money: price -}}
</span>
</li>
{%- for price_break in product.selected_or_first_available_variant.quantity_price_breaks -%}
{%- assign price_break_price = price_break.price | money_with_currency -%}
<li class="{%- if forloop.index >= 3 -%}show-more-item hidden{%- endif -%}">
<span>
{{- price_break.minimum_quantity -}}
<span aria-hidden="true">+</span></span
>
{%- assign price = price_break.price | money_with_currency -%}
<span data-text="{{ 'products.product.volume_pricing.price_at_each' | t: price: price_break_price }}">
{{ 'sections.quick_order_list.each' | t: money: price -}}
</span>
</li>
{%- endfor -%}
</ul>
{%- if product.selected_or_first_available_variant.quantity_price_breaks.size >= 3 -%}
<show-more-button>
<button
class="button-show-more link underlined-link no-js-hidden"
id="Show-More-{{ section.id }}"
type="button"
>
<span class="label-show-more label-text"
><span aria-hidden="true">+ </span>{{ 'products.facets.show_more' | t }}
</span>
</button>
</show-more-button>
{%- endif -%}
{%- endif -%}
</volume-pricing>
{%- endif -%}- [保存] をクリックします。
Javascriptの量ベースの価格コードを追加する
量ベースの価格を表示するために、テーマ内の以下のファイルにコードを追加できます。
theme.js
手順:
Shopifyの管理画面から、[オンラインストア] > [テーマ] の順に移動します。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
theme.jsファイルを開きます。ファイルの末尾に新しい行を作成してから、次のコードを追加します。
if (!customElements.get('show-more-button')) {
customElements.define(
'show-more-button',
class ShowMoreButton extends HTMLElement {
constructor() {
super();
const button = this.querySelector('button');
button.addEventListener('click', (event) => {
this.expandShowMore(event);
const nextElementToFocus = event.target
.closest('.parent-display')
.querySelector('.show-more-item');
if (
nextElementToFocus &&
!nextElementToFocus.classList.contains('hidden') &&
nextElementToFocus.querySelector('input')
) {
nextElementToFocus.querySelector('input').focus();
}
});
}
expandShowMore(event) {
const parentDisplay = event.target
.closest('[id^="Show-More-"]')
.closest('.parent-display');
const parentWrap = parentDisplay.querySelector('.parent-wrap');
this.querySelectorAll('.label-text').forEach((element) =>
element.classList.toggle('hidden'),
);
parentDisplay
.querySelectorAll('.show-more-item')
.forEach((item) => item.classList.toggle('hidden'));
if (!this.querySelector('.label-show-less')) {
this.classList.add('hidden');
}
}
},
);
}- [保存] をクリックします。
ロケールを追加する
数量ルールと量ベースの価格がオンラインストアのすべての翻訳バージョンで表示されるようにするには、en.default.json ファイルに次の JSON 翻訳文字列を追加して、ロケールを追加します。
手順:
Shopifyの管理画面から、[オンラインストア] > [テーマ] の順に移動します。
編集するテーマを見つけて、[...] ボタンをクリックしてアクションメニューを開き、[コードを編集] をクリックします。
en.default.jsonファイルを開きます。ファイルの末尾に新しい行を作成してから、次のコードを追加します。
"products": {
"product": {
"volume_pricing": {
"title": "Volume Pricing",
"note": "Volume pricing available",
"price_at_each": "at /ea"
}
"facets": {
"show_more": "Show more"
}
}
}- [保存] をクリックします。