Search code examples
phpwordpresswoocommerceproductcustom-dimensions

How to set a non existing dimensions measurement unit in Woocommerce 3


In WooCommerce I would like to change cm to ft my products display in height, width and length,

You can see the products with their title below:

enter image description here


Solution

  • Try the following code that will change the displayed dimensions unit in front end:

    add_filter( 'woocommerce_format_dimensions', 'custom_dimention_unit', 20, 2 );
    function custom_dimention_unit( $dimension_string, $dimensions ){
        $dimension_string = implode( ' x ', array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) ) );
    
        if ( ! empty( $dimension_string ) ) {
            $dimension_string .= ' ' . __('ft');
        } else {
            $dimension_string = __( 'N/A', 'woocommerce' );
        }
    
        return $dimension_string;
    }
    

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


    Update: Make dimension unit "ft" (feet) globally (everywhere)

    Woocommerce dimension available units are "m", "cm", "mm", "in" and "yd".

    But "ft" is not available in the Woocommerce > Settings > Products > Mesurements section.

    What you can do is the following:

    1. Paste the following line in your function.php file and save:

       update_option( 'woocommerce_dimension_unit', 'ft' );
      
    2. Browse any page of your web site.

    3. Remove or comment the line of code from your function.php file and save.

    4. You can also remove the first code snippet as it's not needed anymore.

    5. You are done.

    You will get something like:

    enter image description here

    And in backend:

    enter image description here


    For info, to change the dimensions unit in Woocommerce > Settings > Products:

    enter image description here