3

I have a woocommerce site set to use Kg and cm as dimension units in the backend settings. The site mainly sells to Europe so no worries. Now I have to sell to US market to and I am being asked to configure the site so that when users in the US-Client group login they see Imperial measurements either as well as or instead of Metric units.

Is there a way at least I can display the weight and dimensions in Imperial using some conversion etc and keep primary units as Kg and cm in the database?

I've looked everywhere for a plugin but cant find one that helps me.

0

1 Answer 1

3

For single product pages, Here is a concrete example that will convert measurement unit values and set the correct measurement unit label for countries that use Imperial units.

// For Weight
add_filter( 'woocommerce_format_weight', 'imperial_format_weight', 20, 2 );
function imperial_format_weight( $weight_string, $weight ) {
    $country = WC()->customer->get_shipping_country(); // Customer country
    $countries = array( 'US', 'LR', 'MM' ); // Imperial measurement countries

    if ( ! in_array( $country, $countries ) ) return $weight_string; // Exit

    $weight_unit = get_option( 'woocommerce_weight_unit' );

    $weight_string = wc_format_localized_decimal( $weight );
    if ( empty( $weight_string ) )
        return __( 'N/A', 'woocommerce' ); // No values

    if ( $weight_unit == 'kg' ) {
        // conversion rate for 'kg' to 'lbs'
        $rate = 2.20462;
        $label = ' lbs';
    } elseif ( $weight_unit == 'g' ) {
        // conversion rate for 'g' to 'oz'
        $rate = 0.035274;
        $label = ' oz';
    }

    return round( $weight * $rate, 2 ) . $label;
}

// For Dimensions
add_filter( 'woocommerce_format_dimensions', 'imperial_format_dimensions', 20, 2 );
function imperial_format_dimensions( $dimension_string, $dimensions ) {
    $country = WC()->customer->get_shipping_country(); // Customer country
    $countries = array( 'US', 'LR', 'MM' ); // Imperial measurement countries

    if ( ! in_array( $country, $countries ) ) return $dimension_string; // Exit

    $dimension_unit = get_option( 'woocommerce_dimension_unit' );

    $dimension_string = implode( ' x ', array_filter( array_map( 'wc_format_localized_decimal', $dimensions ) ) );
    if( empty( $dimension_string ) )
        return __( 'N/A', 'woocommerce' ); // No values

    if ( $dimension_unit == 'mm' ) {
        // conversion rate for 'mm' to 'inch'
        $rate = 0.0393701;
        $label = ' in';
    } elseif ( $dimension_unit == 'cm' ) {
        // conversion rate for 'cm' to 'inch'
        $rate = 0.393701;
        $label = ' in';
    } elseif ( $dimension_unit == 'm' ) {
        // conversion rate for 'm' to 'yard'
        $rate = 1.09361;
        $label = ' yd';
    }

    $new_dimentions = array();

    foreach( $dimensions as $key => $value ){
        $new_dimentions[$key] = round( $value * $rate, 2 );
    }

    return implode( ' x ', array_filter( array_map( 'wc_format_localized_decimal', $new_dimentions ) ) ) . $label;
}

This code goes on function.php file of your active child theme (or theme). Tested and works.

enter image description here

3
  • Hi there - Ok ive tested the code and works great when client shipping address is set to US. My problem now is that when UK or any clients that use metric visit the site the dimensions do not show up at all. Is there a way to show both imperial or metric depending on geo location of ccustomer? Mar 15, 2018 at 7:17
  • @JamesDonald Yes this is possible… If you don't know how to do it, you should ask a new question tagged "woocommerce" and notify me here please once done. Also there was a little mistake in my actual code that I have updated. Mar 15, 2018 at 10:26
  • understood about question asking - apologies - thx for amending code - everything working perfectly now. Mar 16, 2018 at 0:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.