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

时间:2018-01-29 15:34:24

标签: php wordpress woocommerce product dimensions

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

是的,最后是测量单位。

示例: 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);
}

1 个答案:

答案 0 :(得分:1)

您需要的代码自定义是:

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 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 ) );
    foreach( $dimensions as $key => $dimension ){
        $new_dimensions[$key] = $dimension; //  . ' ' . strtoupper( substr($key, 0, 1) );
    }

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

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

经过测试和工作。

这会输出类似200x600x200mm 的内容(最后只有mm一次)