Hostinger AI Builder 人工模式下如何修改网店价格格式:详细教程
本文介绍如何在**Hostinger AI Builder 手动模式** 创建的在线商店中更改商品价格的格式。
相关更改将显示在商品列表区块、商品页面和购物袋中。默认价格仍会显示在结账页面、发票以及商店管理区域中。
开始之前
- 如果你尚未创建在线商店,请先在 Hostinger AI Builder 手动模式下创建在线商店。
复制代码
<script> const currency = "€"; const thousandsSeparator = "."; const removeCents = false; const priceInTheEnd = false; let fullPrice;
const addCommaToPrice = (price, currency) => { /* currency at the end */ if (price.innerText.slice(0, currency.length) !== currency) { fullPrice = price.innerText.substring( 0, price.innerText.indexOf(currency) ); } else { /* currency in front */ fullPrice = price.innerText.slice(currency.length); }
/* if cents exist */ if (removeCents) { fullPrice = fullPrice.slice(0, -3); } fullPrice = fullPrice .toString() .replace(/B(?=(d{3})+(?!d))/g, thousandsSeparator);
if (priceInTheEnd) { fullPrice = `${fullPrice} ${currency}n`; } else { fullPrice = `${currency} ${fullPrice}n`; }
price.innerText = fullPrice; };
const priceChanged = (price) => { if (price.innerHTML.indexOf("<br>") > -1) { return true; } else { return false; } };
setInterval(() => { /*Product pages*/ if ( document.querySelector(".block-product__price") && !priceChanged(document.querySelector(".block-product__price")) ) { document.querySelectorAll(".block-product__price").forEach((price) => { addCommaToPrice(price, currency); }); }
/*Cart*/ if ( document.querySelector(".cart__price") && !priceChanged(document.querySelector(".cart__price")) ) { document.querySelectorAll("li.cart__list-item").forEach((product) => { if (product.contains(product.querySelector(".cart__sale-price"))) { addCommaToPrice(product.querySelector(".cart__price"), currency); addCommaToPrice(product.querySelector(".cart__sale-price"), currency); } else { addCommaToPrice(product.querySelector(".cart__price"), currency); } });
if (!priceChanged(document.querySelector(".cart__title span"))) { addCommaToPrice(document.querySelector(".cart__title span"), currency); } }
const priceSelector = ".product-list-item__price-wrapper span:not(.product-list-item__price-range)";
/*Section*/ if ( document.querySelector(priceSelector) && !priceChanged(document.querySelector(priceSelector)) ) { document.querySelectorAll(".product-list-item").forEach((product) => { if (product.querySelector("p.product-list-item__price-content")) { addCommaToPrice(product.querySelector(priceSelector), currency); addCommaToPrice( product.querySelector("p.product-list-item__price-content"), currency ); } else { addCommaToPrice(product.querySelector(priceSelector), currency); } }); } }, 500);</script>自定义代码
根据你想更改的内容对代码进行相应修改。例如,更改货币符号。
粘贴代码
在构建器手动模式下,进入网站的设置 → 集成,将代码粘贴到**自定义代码字段中。然后,保存更改并更新网站**。