【问题标题】:Round number of a plugin output WordPress插件输出 WordPress 的整数
【发布时间】:2023-04-10 21:31:01
【问题描述】:

预订插件将价格显示为带 2 位小数的数字。 由于我找不到要连接的过滤器,我想我可能需要一个 JS 循环。

页面上有几处房产的价格为 123.45 美元。我想把它四舍五入到 123 美元

价格的html是:

<span class="price"><span class="currency">$</span>"123,45<span>

我尝试了一个循环,但我不是很擅长 JS:

  function roundPrice() {

let oldPrices = document.getElementsByClassName("price");

for (let i = 0; i < oldPrices.length; i++) {

    // strip $ of price
    oldPrices[i] = oldPrices[i].substr[1];

    // string to num
    oldPrices[i] = parseInt(oldPrices[i]);

    // round number
    let newPrice = Math.round(oldPrices[i]);

        return oldPrices[i] = newPrice;

    }
}

    roundPrice();

但这不起作用。有这样的解决方案吗?

非常感谢,山姆

【问题讨论】:

  • 嗨,在什么意义上它不起作用?您是否更改了任何输出,或者系统是否崩溃,或者您是否在 console.log 中看到任何错误。由于这是 Wordpress,您能告诉我们您在哪里运行此代码/将其挂钩?谢谢。
  • 我没有得到任何输出。但是在 WP 中你总是必须使用钩子吗?该函数是否没有被调用?这可能是问题所在,我找不到这个钩子。
  • 你把你的代码放在哪里了 - 在functions.php/插件中......?
  • 在一个文件 custom-js.js 中,我把它挂在了 wp_enqueue_scripts 的 functions.php 中
  • 您可以发布您的入队电话以便我们看看吗?可能是您的代码被调用但有点早。它是什么预订插件?

标签: javascript arrays wordpress filter rounding


【解决方案1】:

这个问题有两个部分:如何编写函数以改变相关 DOM 元素中的价格,以及如何让函数在 Wordpress 中运行。

在第一部分,问题中的代码有一些问题。直接的主要是document.getElementsByClassName("price") 返回一个元素集合,而不是字符串数组。

请仔细阅读这段代码,了解如何遍历这些元素并操作它们的 HTML。

function roundPrice() {
let oldPrices = document.getElementsByClassName("price");//this gives you the collection of elements which have class price

for (let i = 0; i < oldPrices.length; i++) {

    // Note: oldprices[i] is an element NOT a string
    let price = oldPrices[i].innerHTML;//this should give you <span class="currency">$</span>"123,45

    let priceArray = price.split('>');//this will give you an array and you want the last bit
    let oldPrice = priceArray.pop();

    
    //now you have the actual price, but with , where JS would expect .
    oldPrice = oldPrice.replace(',','.');
    
    //now you can do the rounding
    let newPrice = Math.round(Number(oldPrice));// NOT parseInt

    //now we put the new price back into the element's innerHTML
    priceArray.push(newPrice);

    //then we write the innerHTML back into the element
    oldPrices[i].innerHTML = priceArray.join('>');
    }
}
//it would be a good idea to put some prefix of your own on this function name so it doesn't clash with any other plugin using function round
    roundPrice();
<span class="price"><span class="currency">$</span>1234567,89</span>
<span class="notprice"><span class="currency">$</span>9999,01</span>
<span class="price and some"><span class="currency">$</span>888</span>
<span class="price"><span class="currency">$</span>123,45</span>
<span class="price"><span class="currency">$</span>123</span>

第二部分是如何最好地将这个 JS 链接到相关的 Wordpress 页面。在我们最终决定之前,响应 cmets 需要一些进一步的信息,但要记住的重要一点是它必须在 DOM 加载后运行。

注意:虽然上面的代码回答了设置编码的问题,但可以说这不是实现这一目标的最佳方式。像这样“即时”更改 HTML 可能会导致其他问题——尤其是如果插件开发人员更改了 HTML 的格式。有没有办法改变插件,例如通过选择一些货币设置?

【讨论】:

  • 完美运行!我将 enqueue 调用的 'load in footer' 参数设置为 TRUE 并且逗号已经是一个点(对此感到抱歉),所以我没有使用该行。我的 JS 知识很基础,我无法想出这样的解决方案,所以这是一个很好的教训。谢谢!不幸的是,没有这个设置。插件制造商使用四舍五入的数字,但收到了很多包含美分的价格请求,所以他们改变了它。幸运的是我会自己维护这个网站,所以我可以密切关注它。
猜你喜欢
  • 2012-09-10
  • 2021-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多