skip to Main Content

Using Reorder and customize product dimensions formatted output in WooCommerce answer code il would like to make the output display as:

Size: D40 x W45 x H60 (cm)

Any help is appreciated.

2

Answers


  1. Just add the follows code snippet in your active theme’s functions.php to achieve the above –

    function woocommerce_display_product_attributes( $product_attributes, $product ){
        if( !isset( $product_attributes['dimensions'] ) ) return $product_attributes;
    
        $modified_dimensions = array();
        foreach ( $product->get_dimensions( false ) as $key => $value ) {
            if( $key == 'length' )
                $modified_dimensions[$key] = 'D'.$value;
            if( $key == 'width' )
                $modified_dimensions[$key] = 'W'.$value;
            if( $key == 'height' )
                $modified_dimensions[$key] = 'H'.$value;
        }
        $dimension_string = implode( ' × ', array_filter( array_map( 'wc_format_localized_decimal', $modified_dimensions ) ) );
    
        if ( ! empty( $dimension_string ) ) {
            $dimension_string .= ' (' . get_option( 'woocommerce_dimension_unit' ) . ')';
        } else {
            $dimension_string = __( 'N/A', 'woocommerce' );
        }
        // change dimensions label & value.
        $product_attributes['dimensions']['label']  = __( 'Size', 'text-domain' );
        $product_attributes['dimensions']['value']  = $dimension_string;
        return $product_attributes;
    }
    add_filter( 'woocommerce_display_product_attributes', 'woocommerce_display_product_attributes', 99, 2 );
    
    Login or Signup to reply.
  2. Welcome to StackOverflow!
    Try a simpler code (tested):

    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' );
    
        $dimensions = array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) );
    
        return 'D'.$dimensions['length'].' x W'.$dimensions['width'].' x H'.$dimensions['height'].' ('.get_option( 'woocommerce_dimension_unit' ).')';
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search