-2

If you enter a dimension in the WooCommerce product page it will display that in the Additional Info tab, but it doesn't state which plain it is, whether length, width, or height. How can I get it to display this? In my product, in most cases, I will only enter a length and need that to be clear.

Edit: Received an answer in the Wordpress forum, noted below incase it helps anyone else..

add_filter('woocommerce_format_dimensions', 'my_custom_dimensions', 10, 2);
function my_custom_dimensions( $dim_string, $dimensions ) {
    $dim_string = 'Length: ' . $dimensions['length'] . ' ' . get_option( 'woocommerce_dimension_unit' );
    return $dim_string;
}
Ashley
  • 131
  • 11
  • 1
    One way is to edit https://github.com/woocommerce/woocommerce/blob/master/templates/single-product/product-attributes.php If you can't figure it out, adjust your question and show where you're getting stuck on. I think you know by now that the intention on stackoverflow is to show what you have tried so far – 7uc1f3r May 06 '20 at 17:09

1 Answers1

1
add_filter( 'woocommerce_format_dimensions', 'woocommerce_format_dimensions', 10, 2 );

function woocommerce_format_dimensions( $dimension_string, $dimensions ) {


    if ( !empty( $dimension_string ) ) {
        $dimension_string    = '';
        $dimension_unit      = ' ' . get_option( 'woocommerce_dimension_unit' );
        foreach ( $dimensions as $dimension => $value ) {
            $dimension_string .= ucwords( $dimension ) . ": " . $value . $dimension_unit . " ";
        }
    } else {
        $dimension_string = __( 'N/A', 'woocommerce' );
    }
    return $dimension_string;
}
mujuonly
  • 11,370
  • 5
  • 45
  • 75