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

Stylization of decimals as uppercase in WooCommerce frontend only(仅在 WooCommerce 前端将小数样式化为大写)
本文介绍了仅在 WooCommerce 前端将小数样式化为大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望您能就具体案例提供帮助.我能够在我的 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()

这篇关于仅在 WooCommerce 前端将小数样式化为大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)