在产品页面上显示剩余的多属性库存
您可以在产品页面或特色产品分区中添加一条消息,用于在产品多属性库存较低时显示库存中的商品数量。若要显示此消息,您需要为产品启用库存跟踪。
此自定义设置的步骤因您的模板而异。点击模板的按钮,然后按照说明操作。
提示:Shopify 的 Supply 模板中已包含显示剩余数量消息设置。您可以在模板辑器中启用此设置。
Debut
Venture / Brooklyn / Simple / Minimal
Boundless
Narrative
针对 Debut 的步骤
编辑 theme.liquid
在 Shopify 后台中,转到在线商店 > 模板。
找到要编辑的模板,然后点击操作 > 编辑代码。
在 Layout 目录中,打开
theme.liquid
。查找代码中的结束
</head>
标记。在结束</head>
标记上方的新行中,粘贴以下代码:
<script> var variantStock = {}; </script>
点击保存。
编辑 product-template.liquid
或 featured-product.liquid
在 Sections 目录中,打开
product-template.liquid
或featured-product.liquid
:打开
product-template.liquid
以将此功能添加到产品页面。打开
featured-product.liquid
以将此功能添加到主页上的特色产品分区。查找
{% form 'product'
。在此标记上方添加以下代码:
<div> {% if current_variant.inventory_quantity > 0 and current_variant.inventory_management == 'shopify' %} <p>Stock: {{ current_variant.inventory_quantity }}</p> {% endif %} </div>
上述代码会输出 Stock: x
。您可以通过调整 <p>
标记中的内容来更改措辞。请确保在 <p>
标记中包含 {{ current_variant.inventory_quantity }}
。
在文件底部,添加以下代码:
<script> {% for variant in product.variants %} variantStock[{{- variant.id -}}] = {{ variant.inventory_quantity }}; {% endfor %} </script>
点击保存。
编辑 theme.js.liquid
或 theme.js
您需要对这些文件所做的更改取决于您使用的 Debut 版本。
版本 17.8.0 及以下
打开
theme.js.liquid
或theme.js
。查找
theme.Product = (function()
。在this.selectors = {
下方添加以下代码:
inventoryHtml: '.inventoryWrapper',
在同一文件中,查找
_updateAddToCart: function(evt) {
。在正下方添加以下代码:
var inventoryWrapper = this.container.querySelector(this.selectors.inventoryHtml);
查找
if (variant.available) {
。在} else {
语句前添加以下代码:js if (variantStock[variant.id] > 0 && variant.inventory_management == 'shopify' && inventoryWrapper !== null) { const inventoryHtml =
Stock: ${variantStock[variant.id]}
; inventoryWrapper.innerHTML = inventoryHtml; } else { inventoryWrapper.innerHTML = ''; }
上述代码会输出
Stock: x
。您可以通过调整<p>
标记中的内容来更改措辞。请确保在<p>
标记中包含${variantStock[variant.id]}
。点击保存。
版本 17.9.0 及以上
打开
theme.js.liquid
或theme.js
。查找
theme.Product = (function()
。在this.selectors = {
下方添加以下代码:
inventoryHtml: '.inventoryWrapper',
在同一文件中,查找
_setProductState: function(evt) {
。在正下方添加以下代码:
var inventoryWrapper = this.container.querySelector(this.selectors.inventoryHtml);
在同一函数中,查找
if (!variant) {
。在右}
括号后,添加以下代码:
else { if (variantStock[variant.id] > 0 && variant.inventory_management == 'shopify' && inventoryWrapper !== null) { const inventoryHtml = `<p>Stock: ${variantStock[variant.id]}</p>`; inventoryWrapper.innerHTML = inventoryHtml; } else { inventoryWrapper.innerHTML = ''; } }
上述代码会输出 Stock: x
。您可以通过调整 <p>
标记中的内容来更改措辞。请确保在 <p>
标记中包含 ${variantStock[variant.id]}
。
点击保存。
Shopify商户官网原文详情:
Show the remaining inventory of a variant on product pages
You can add a message on the product page or featured product section that shows the number of items you have in stock when inventory runs low on a product variant. For this message to show, you need to enable inventory tracking for the product.
The steps for this customization vary depending on your theme. Click the button for your theme and follow the instructions.
Debut
Venture / Brooklyn / Simple / Minimal
Boundless
Narrative
Steps for Debut
Edit
theme.liquid
From your Shopify admin, go to Online Store > Themes.
Find the theme you want to edit, and then click Actions > Edit code.
From the Layout directory, open
theme.liquid
.Find the closing
</head>
tag in the code. On a new line above the closing</head>
tag, paste the following code:<script> var variantStock = {}; </script>Click Save.
Edit
product-template.liquid
orfeatured-product.liquid
From the Sections directory, open
product-template.liquid
orfeatured-product.liquid
:
Open
product-template.liquid
to add this feature to product pages.Open
featured-product.liquid
to add this feature to the featured product section on the home page.Find
{% form 'product'
. Above this tag, add the following code:<div> {% if current_variant.inventory_quantity > 0 and current_variant.inventory_management == 'shopify' %} <p>Stock: {{ current_variant.inventory_quantity }}</p> {% endif %} </div>The code above outputs
Stock: x
. You can change the wording by adjusting the content in the<p>
tags. Make sure to include{{ current_variant.inventory_quantity }}
in your<p>
tags.At the bottom of the file, add the following code:
<script> {% for variant in product.variants %} variantStock[{{- variant.id -}}] = {{ variant.inventory_quantity }}; {% endfor %} </script>Click Save.
Edit
theme.js.liquid
ortheme.js
The changes that you need to make to these files depend on the version of Debut that you're using.
Versions 17.8.0 and below
Open
theme.js.liquid
ortheme.js
.Find
theme.Product = (function()
. Belowthis.selectors = {
, add the following code:inventoryHtml: '.inventoryWrapper',In the same file, find
_updateAddToCart: function(evt) {
. Directly below, add the following code:var inventoryWrapper = this.container.querySelector(this.selectors.inventoryHtml);Find
if (variant.available) {
. Before the} else {
statement, add the following code:js if (variantStock[variant.id] > 0 && variant.inventory_management == 'shopify' && inventoryWrapper !== null) { const inventoryHtml = `<p>Stock: ${variantStock[variant.id]}</p>`; inventoryWrapper.innerHTML = inventoryHtml; } else { inventoryWrapper.innerHTML = ''; }
The code above outputs
Stock: x
. You can change the wording by adjusting the content in the<p>
tags. Make sure to include${variantStock[variant.id]}
in your<p>
tags.Click Save.
Versions 17.9.0 and above
Open
theme.js.liquid
ortheme.js
.Find
theme.Product = (function()
. Belowthis.selectors = {
, add the following code:inventoryHtml: '.inventoryWrapper',In the same file, find
_setProductState: function(evt) {
. Directly below, add the following code:var inventoryWrapper = this.container.querySelector(this.selectors.inventoryHtml);In the same function, find
if (!variant) {
. After the closing}
bracket, add the following code:else { if (variantStock[variant.id] > 0 && variant.inventory_management == 'shopify' && inventoryWrapper !== null) { const inventoryHtml = `<p>Stock: ${variantStock[variant.id]}</p>`; inventoryWrapper.innerHTML = inventoryHtml; } else { inventoryWrapper.innerHTML = ''; } }The code above outputs
Stock: x
. You can change the wording by adjusting the content in the<p>
tags. Make sure to include${variantStock[variant.id]}
in your<p>
tags.Click Save.
文章内容来源:Shopify商户官方网站