在您的商店中显示数量规则和基于数量的定价
您可以通过将商店的模板更新到最新版本,在商店中为您的 B2B 产品目录显示数量规则和基于数量的定价。
如果您不想更改或更新模板,则可以将以下代码添加到模板中以显示数量规则和基于数量的定价。
向模板代码中添加数量规则和基于数量的定价的注意事项
在向模板代码中添加数量规则和基于数量的定价之前,请查看以下注意事项:
- 这是一篇高级教程。如果您不习惯阅读和编辑模板代码,则可以与开发人员合作或聘请 Shopify 合作伙伴。
- 在对模板文件进行更新之前,请确保复制您的模板以创建备份副本。
- 版本 8.0.0 或更高版本的Shopify 免费模板支持 B2B 产品目录的数量规则。版本 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');
}
}
},
);
}- 点击保存。
添加区域设置
为确保数量规则和基于数量的定价显示在您在线商店的所有翻译版本中,您可以通过将以下 JSON 翻译字符串添加到您的 en.default.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"
}
}
}- 点击保存。