【问题标题】:How to remove or hide decimals on weight values in Woocommerce如何在 Woocommerce 中删除或隐藏重量值的小数点
【发布时间】:2019-12-20 18:53:33
【问题描述】:

我想隐藏小数,因为用 g 表示重量并且显示小数似乎没有必要。 woocommerce的wc-formatting-functions.php的功能:

/**
 * Format a weight for display.
 *
 * @since  3.0.0
 * @param  float $weight Weight.
 * @return string
 */
function wc_format_weight( $weight ) {
    $weight_string = wc_format_localized_decimal( $weight );

    if ( ! empty( $weight_string ) ) {
        $weight_string .= ' ' . get_option( 'woocommerce_weight_unit' );
    } else {
        $weight_string = __( 'N/A', 'woocommerce' );
    }

    return apply_filters( 'woocommerce_format_weight', $weight_string, $weight );
}

关于如何更改重量显示方式的任何想法?也许我应该通过 CSS 做到这一点?

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    尝试以下方法来更改 WooCommerce 中的格式化重量,去除小数:

    add_filter( 'woocommerce_format_weight', 'custom_format_weight', 90, 2 );
    function custom_format_weight( $weight_string, $weight ){
    
        $weight_string  = intval( $weight );
    
        if ( ! empty( $weight_string ) ) {
            $weight_string .= ' ' . get_option( 'woocommerce_weight_unit' );
        } else {
            $weight_string = __( 'N/A', 'woocommerce' );
        }
    
        return $weight_string;
    }
    

    代码在您的活动子主题(或活动主题)的functions.php 文件中。

    【讨论】:

      猜你喜欢
      • 2018-09-10
      • 1970-01-01
      • 2017-04-01
      • 2019-12-30
      • 2017-09-30
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多