【问题标题】:How to get WooCommerce variation prices to have two decimals (trailing zero)?如何让 WooCommerce 变化价格具有两位小数(尾随零)?
【发布时间】:2019-08-18 22:53:30
【问题描述】:

我有一个带有变体的产品,它由 templates/single-product/add-to-cart/variation.php 模板显示,该模板使用基于 JavaScript 的模板 {{{ data.variation.display_price }}}。当我的价格以零结尾时,例如 12.50 欧元,前端的价格将显示为 12.5 欧元(不含零)。我希望价格包含尾随零。

我尝试了以下过滤器,但它不起作用。

add_filter( 'woocommerce_price_trim_zeros', 'wc_hide_trailing_zeros', 10, 1 );
function wc_hide_trailing_zeros( $trim ) {
    // set to false to show trailing zeros
    return false;
}

【问题讨论】:

  • 您在小数点的常规选项卡上使用了什么设置?好像没必要用代码来设置。
  • 默认设置为 2。这就是为什么我觉得奇怪的是它不将该设置应用于变化价格。
  • 我从来没有遇到过变化价格的问题,但我没有使用 € 。也许一个主题或其他插件正在删除尾随零。

标签: woocommerce decimal price


【解决方案1】:

我已经通过检查当价格有一位小数时添加一个零来修复它。

// https://stackoverflow.com/a/2430214/3689325
function numberOfDecimals( $value ) {
    if ( (int) $value == $value ) {
        return 0;
    }
    else if ( ! is_numeric( $value ) ) {
        return false;
    }

    return strlen( $value ) - strrpos( $value, '.' ) - 1;
}

/**
 * Make sure prices have two decimals.
 */
add_filter( 'woocommerce_get_price_including_tax', 'price_two_decimals', 10, 1 );
add_filter( 'woocommerce_get_price_excluding_tax', 'price_two_decimals', 10, 1 );
function price_two_decimals( $price ) {
    if ( numberOfDecimals( $price ) === 1 ) {
        $price = number_format( $price, 2 );
        return $price;
    }

    return $price;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    相关资源
    最近更新 更多