php - Display and save formated dimensions of the product everywhere

one text

Solution:

First the method get_dimensions() has an option to display formatted dimensions, so you can change dimension formatting using the following, to add dimension labels as desired:

add_filter( 'woocommerce_format_dimensions', 'custom_formated_product_dimentions', 10, 2 );
function custom_formated_product_dimentions( $dimension_string, $dimensions ){
    $text_domain    = 'woocommerce';
    $dimension_unit = get_option('woocommerce_dimensions_unit');

    if ( empty( $dimension_string ) )
        return __( 'N/A', $text_domain );

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

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

    // Loop through new dimensions array adding spanish labels 
    foreach( $dimensions as $key => $dimention ){
        $dimensions[$key] = ucfirst($key) . ' ' . $dimention. ' ' . $dimension_unit;
    }

    return implode( ' × ',  $dimensions);
}

For Spanish language, you can translate strings with Loco plugin or you can replace:

$new_dimensions = array(
    __('width', $text_domain)  => $dimensions['width'],
    __('length', $text_domain) => $dimensions['length'],
    __('height', $text_domain) => $dimensions['height'],
);

by:

$new_dimensions = array(
    __('longitud', $text_domain) => $dimensions['width'],
    __('ancho', $text_domain)    => $dimensions['length'],
    __('alto', $text_domain)     => $dimensions['height'],
);

Now on your code you will use get_dimensions( true ) method to display formatted dimensions as follow:

// Display the cart item dimensions in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data_dimensions', 10, 2 );
function display_custom_item_data_dimensions( $cart_item_data, $cart_item ) {
    $product =  $cart_item['data'];

    if ( $product->has_dimensions() ){
        $cart_item_data[] = array(
            'name' => __( 'Dimensions', 'woocommerce' ),
            'value' => $product->get_dimensions( true ),
        );
    }
    return $cart_item_data;
}

// Save and Display the order item dimensions (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data_dimensions', 10, 4 );
function display_order_item_data_dimensions( $item, $cart_item_key, $values, $order ) {
    $product =  $values['data'];

    if ( $product->has_dimensions() ){
        $item->update_meta_data( __( 'Dimensions', 'woocommerce' ), $product->get_dimensions( true ) );
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Note: As has_dimensions() is a conditional method returning a boolean value, it doesns't need anything else as a condition.

Similar: Rename Length to Diameter in WooCommerce formatted product dimensions output

enter image description here

Source