仅在 WooCommerce 前端将小数样式化为大写

2022-01-19 00:00:00 frontend php wordpress woocommerce css

我希望您能就具体案例提供帮助.我能够在我的 WooCommerce 商店中完全按照我的意愿制作价格小数点的样式.

I'd like your help for a specific case. I was able to make the stylization of the price decimals exactly as I wished on my WooCommerce store.

但是,问题是现在这段代码也适用于我的 WooCommerce 后端.你知道我怎样才能让下面的代码不影响 WooCommerce 后端页面吗?

However, the problem is that now this code also applies for my WooCommerce backend. Do you know how I could make that the below code do not impact the WooCommerce backend pages?

/**
 * ADJUST THE DECIMALS STYLE AS UPPERCASE (COMPLEMENTARY TO CSS)
 */
add_filter( 'formatted_woocommerce_price', 'ts_woo_decimal_price', 10, 5 );
function ts_woo_decimal_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
    $unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
    $decimal = sprintf( '%02d', ( $price - intval( $price ) ) * 100 );
    return $unit . '<sup>' . $decimal . '</sup>';
}

/* GENERAL - ADJUST THE DECIMALS STYLE AS UPPERCASE (COMPLEMENTARY TO PHP) */
.price .amount {
  font-size: 120%;
}
.sub, sup {
    margin-left: 2px;
    margin-right: 2px;
    font-size: 60%;
}

推荐答案

新线程:小数的自定义样式会破坏默认的 WooCommerce 计算

更新 - 只需使用条件标签 is_admin() 如下:

Updated - Simply use the conditional tag is_admin() as follow:

add_filter( 'formatted_woocommerce_price', 'ts_woo_decimal_price', 10, 5 );
function ts_woo_decimal_price( $formatted_price, $price, $decimal_places, $decimal_separator, $thousand_separator ) {
    // Not on backend
    if ( ! is_admin() ) {
        $unit = number_format( intval( $price ), 0, $decimal_separator, $thousand_separator );
        $decimal = sprintf( '%02d', ( $price - intval( $price ) ) * 100 );
        return $unit . '<sup>' . $decimal . '</sup>';
    }
    return $formatted_price;
}

文档:WordPress 条件标签 is_admin()

相关文章