在 Woocommerce 中附加其他显示的尺寸和重量单位

Append other displayed dimensions and weight units in Woocommerce

有人可以分享功能代码以在 Woocommerce 中除了当前的重量和尺寸单位之外还有另一个单位吗?

示例如下图

更新 2(2019 年 3 月)

以下代码将根据需要附加到默认显示的尺寸和重量的额外换算单位值:

add_filter( 'woocommerce_format_weight', 'custom_format_weight', 20, 2 );
function custom_format_weight( $weight_string, $weight ){
    // Format decimals from default weight value
    $weight_string  = wc_format_localized_decimal( $weight );
    // Format decimals from custom converted weight value
    $weight_string2 = wc_format_localized_decimal( round($weight * 0.45359237, 2) );

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

    return $weight_string;
}

add_filter( 'woocommerce_format_dimensions', 'custom_format_dimensions', 10, 2 );
function custom_format_dimensions( $dimension_string, $dimensions ){
    // Initializing variable
    $dimentions2 = array();

    // Loop though dimensions array (and set custom converted formatted decimals values in a new array)
    foreach( $dimensions as $key => $value ){
        if( ! empty($value) && $value != 0 )
            $dimentions2[$key] = wc_format_localized_decimal( round($value * 2.54, 2) );
    }

    // Format default dimentions in a string
    $dimension_string  = implode( ' x ', array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) ) );

    if ( ! empty( $dimension_string ) ) {
        // Format custom converted array in a string and append it to default formatted dimensions string
        $dimension_string2 = ' ( ' . implode( ' x ', $dimentions2 ) . ' ' . __( 'cm', 'woocommerce' ) . ' )';
        $dimension_string .= ' ' . get_option( 'woocommerce_dimension_unit' ) . $dimension_string2;
    } else {
        $dimension_string = __( 'N/A', 'woocommerce' );
    }

    return $dimension_string;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

我找到了用逗号显示产品重量的解决方案。比方说 1.2 公斤。 将此添加到您的 functions.php

add_filter( 'woocommerce_format_weight', 'custom_format_weight', 20, 2 );
function custom_format_weight( $weight_string, $weight ){
    // Format decimals from default weight value
    $weight_string  = wc_format_localized_decimal( $weight );
    // Format decimals from custom converted weight value
    $x = floor($weight_string);
    $y = $weight_string - floor($weight_string);
    $y = str_replace("0.", "", $y);

    $kila = $x.", ".$y." kg";

    if ( ! empty( $weight_string ) ) {
        $weight_string = $kila;
    } else  {
        $weight_string = __( 'N/A', 'woocommerce' );
    }
    return $weight_string;
}