Rename Length to Diameter in WooCommerce formatted product dimensions output

那年仲夏 提交于 2019-12-06 16:02:56

You just need to set the array keys/values as you want them in your function (renaming one key and reordering your array), this way:

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( 
        'height' => $dimensions['height'],
        'width'  => $dimensions['width'],
        'diameter' => $dimensions['length']
    );

    $dimensions = array_filter( array_map( 'wc_format_localized_decimal', $new_dimentions ) );
    foreach( $dimensions as $key => $dimention ){
        $label_with_dimensions[$key] = $dimention . ' ' . strtoupper( substr($key, 0, 1) ) . ' ' . get_option( 'woocommerce_dimension_unit' ) . '.';
    }

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

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested on WooCommerce versions 3+ and works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!