在商店顯示數量規則與按量定價
您可以透過 updating your store's theme 至最新版本,在商店中顯示 B2B 目錄的數量規則與按量定價。
若您不想變更或更新佈景主題,也可以在佈景主題中加入以下程式碼,以顯示數量規則與按量定價。
本頁內容
將數量規則與按量定價加入佈景主題程式碼的注意事項
在將數量規則與按量定價加入佈景主題程式碼前,請先檢閱下列注意事項:
- 這是進階教學課程。若您不熟悉閱讀與編輯佈景主題程式碼,建議與開發人員合作,或 hire a Shopify Partner。
- 在更新佈景主題檔案之前,請先 duplicate your theme,以建立備份副本。
- B2B 目錄的數量規則支援 free Shopify themes 8.0.0 版或更新版本。按量定價則支援 11.0.0 版或更新版本。
購物車商品數量
商品子類在購物車中的數量可以顯示在產品頁面或精選商品區段。您可以使用 Liquid 擷取該數值。
新增 Liquid 購物車商品數量程式碼
您可以在佈景主題的下列檔案中加入程式碼,以支援購物車數量:
main-product.liquid或同等檔案featured-product.liquid或同等檔案
步驟:
在 Shopify 管理介面中,前往 網路商店 > Themes。
找到要編輯的佈景主題,按一下 ... 按鈕以開啟動作選單,然後按一下 編輯程式碼。
開啟要編輯的檔案。
在檔案底部新增一行,並加入下列程式碼:
{% 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 管理介面中,前往 網路商店 > Themes。
找到要編輯的佈景主題,按一下 ... 按鈕以開啟動作選單,然後按一下 編輯程式碼。
開啟
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 管理介面中,前往 網路商店 > Themes。
找到要編輯的佈景主題,按一下 ... 按鈕以開啟動作選單,然後按一下 編輯程式碼。
開啟要編輯的檔案。
在檔案底部新增一行,然後加入下列程式碼:
{% 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 管理介面中,前往 網路商店 > Themes。
找到要編輯的佈景主題,按一下 ... 按鈕以開啟動作選單,然後按一下 編輯程式碼。
開啟
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 管理介面中,前往 網路商店 > Themes。
找到要編輯的佈景主題,按一下 ... 按鈕以開啟動作選單,然後按一下 編輯程式碼。
開啟要編輯的檔案。
在檔案底部新增一行,然後加入下列程式碼:
{%- 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 管理介面中,前往 網路商店 > Themes。
找到要編輯的佈景主題,按一下 ... 按鈕以開啟動作選單,然後按一下 編輯程式碼。
開啟
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 管理介面中,前往 網路商店 > Themes。
找到要編輯的佈景主題,按一下 ... 按鈕以開啟動作選單,然後按一下 編輯程式碼。
開啟
en.default.json檔案。在檔案底部新增一行,然後加入下列程式碼:
"products": {
"product": {
"volume_pricing": {
"title": "Volume Pricing",
"note": "Volume pricing available",
"price_at_each": "at /ea"
}
"facets": {
"show_more": "Show more"
}
}
}- 按一下 儲存。