在 WooCommerce 中重新排序和自定义产品尺寸格式化输出

Reorder and customize product dimensions formatted output in WooCommerce

首先感谢LoicTheAztec的回答。我正在寻找非常相似的定制,但不是 100% 相同,我想要实现的是:WxLxHmm

是的,最后有度量单位。

示例:200x600x200mm

根据提供的答案,我设法得到 200mmx600mmx200mm

如果我只想在最后使用 "mm" 单元怎么办?

下面是我编辑的代码版本

add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimensions', 10, 2 );
function custom_formated_product_dimensions( $dimension_string, $dimensions ){
if ( empty( $dimension_string ) )
return __( 'N/A', 'woocommerce' );

// Set here your new array of dimensions based on existing keys/values
$new_dimensions = array( 
'width'  => $dimensions['width'],
'length' => $dimensions['length'],
'height' => $dimensions['height']
);
$dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimensions ) );

foreach( $dimensions as $key => $dimension ){
$label_with_dimensions[$key] = $dimension . '' . get_option( 'woocommerce_dimension_unit' ). '';
}

return implode( 'x',  $label_with_dimensions);
}

要以不同的方式重新排序维度输出,您需要对代码进行自定义:

add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimentions', 10, 2 );
function custom_formated_product_dimentions( $dimension_string, $dimensions ){
    if ( empty( $dimension_string ) )
        return __( 'N/A', 'woocommerce' );

    // Set here your new reordered array of dimensions based on existing keys/values
    $new_dimentions = array(
        'width'  => $dimensions['width'],
        'length' => $dimensions['length'],
        'height' => $dimensions['height']
    );

    $dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimentions ) );

    return implode( 'x',  $dimensions) . get_option( 'woocommerce_dimension_unit' );
}

代码进入活动子主题(或活动主题)的 function.php 文件。

已测试并有效。

这将输出类似于 200x600x200mm (最后 mm 一次).