This post was prompted by a recent question on the blog regarding how best to add custom units to the WooCommerce dimensions or weight options. While not a difficult thing to do, adding units is not trivial either if you don’t have much experience with PHP, WordPress or WooCommerce. This would be useful for adding, for instance, the foot as a dimension measurement to WooCommerce, which seems to be the most glaring omission. I’m guessing the foot will be added to WooCommerce core at some point, though who knows, perhaps your business relies on the smoot and so there will still be need for this article.

Dimension and weight units can be configured in WooCommerce by going to WooCommerce > Settings > Catalog – Product Data. Out of the box, the dimension units include: m, cm, mm, in, yd:

Adding new units is a matter of hooking into the woocommerce_catalog_settings filter and modifying the core catalog settings, as shown in the example code below, which should be added to either your theme’s functions.php, or if you’re feeling ambitious, in a custom site plugin. In the following code example we’ll be adding the foot and the mile:

add_filter( 'woocommerce_catalog_settings', 'add_woocommerce_dimension_units' );

function add_woocommerce_dimension_units( $settings ) {
  foreach ( $settings as &$setting ) {

    if ( $setting['id'] == 'woocommerce_dimension_unit' ) {
      $options = array();

      foreach ( $setting['options'] as $key => $value ) {
        if ( $key == 'in' ) {
          // safely add foot and mile to the dimensions units, in the correct order
          $options[ $key ] = $value;

          if ( ! isset( $setting['options']['ft'] ) ) $options['ft'] = __( 'ft' );  // foot
          if ( ! isset( $options['yd'] ) )            $options['yd'] = __( 'yd' );  // yard (correct order)
          if ( ! isset( $setting['options']['mi'] ) ) $options['mi'] = __( 'mi' );  // mile

        } else {
          // maintain all other existing dimensions
          if ( ! isset( $options[ $key ] ) ) $options[ $key ] = $value;
        }
      }
      $setting['options'] = $options;
    }
  }

  return $settings;
}

The above code is a bit more complex than it strictly needs to be, due to the fact that I place the new units in proper ascending order along with the other English units, giving us: in, ft, yd, mi. I figure if a thing like this is worth doing at all, it’s worth doing right. Still, for completeness, here’s the training-wheels version that just tacks the new units onto the end of the list if you want to start off a little more slowly:

add_filter( 'woocommerce_catalog_settings', 'add_woocommerce_dimension_units' );

function add_woocommerce_dimension_units( $settings ) {
  foreach ( $settings as &$setting ) {

    if ( $setting['id'] == 'woocommerce_dimension_unit' ) {

      $setting['options']['ft'] = __( 'ft' );  // foot
      $setting['options']['mi'] = __( 'mi' );  // mile

    }
  }

  return $settings;
}

With this code in place, and one of your new dimensions selected, you are now free to define your product dimensions within that new unit:

Published by Justin Stern

Justin is one of our co-founders, and is our resident overengineer. He likes to write developer tutorials and make black magic happen in our plugins. He thinks that writing code is a lot easier than writing words.

19 Comments

  1. Hi is there a way where you can add a unit per product like “1 ea” or “1 pair” or “1 set” ?

    Cheers

    • Well, you can certainly create a unit named “ea” or “pair” or “set”, and make use of it.

      • Unit? Per product? How? lets say I want product A to say 1 set and Product B as 1 pair..so how? 🙂

        • ok I see what you’re saying, yeah there’s no real way of doing that by default, so you’d have to do something custom. Or, don’t forget you can always just create some arbitrary product attribute, add it to the product and set it to display in the Attributes tab. So you could for instance create a text attribute named ‘Amount’ and make the value for product A ‘1 set’, and the value for product B ‘1 pair’. It all depends on where and how you want this information displayed

  2. Hi there, what code would be best to use to modify text of ‘weight’ and ‘dimensions’, for example, if I want to say ‘measurements’ rather than ‘dimension’? Thanks

  3. Hello!
    Can you help me delete indent (&nbsp) wich is located right of number.

    Now is like: “16 mm”
    But I want like: “16mm”

    Thank you. Sorry for my english.

  4. just a great tip, thank you very much!!!!
    I’ve made it empty and -none-, so now clients can enter whatever unit they want 🙂

  5. I’m trying to use 2 Dimension Units with WooCommerce Multilingual for seperate languages any way to do this ?

  6. In case you’ve updated to WC2.1+, “If you use WooCommerce 2.1+ use the woocommerce_product_settings hook instead of woocommerce_catalog_settings.” This is per WCs support docs site. Thanks for the easy-to-follow help!

  7. With the new woocommerce upgrade it doesn’t work anymore including “woocommerce_product_settings” hook.

  8. Hi, I have been using this bit of code successfully (thanks!) – but, recently discovered that some of my users are encountering an error upon checkout to Paypal – they get a “Your Cart is Empty” error…this type of error generally happens when something is missing when passing cart data to Paypal…so, I ran a theme check, and it called out this bit of the add-dimension code:

    RECOMMENDED: Text domain problems in functions.php. You have not included a text domain!
    Line 143: if ( ! isset( $setting[‘options’][‘ft’] ) ) $options[‘ft’] = __(‘ft’); // foot
    Line 144: if ( ! isset( $options[‘yd’] ) ) $options[‘yd’] = __(‘yd’); // yard (correct order)
    Line 145: if ( ! isset( $setting[‘options’][‘mi’] ) ) $options[‘mi’] = __(‘mi’); // mile

    I’m now a little suspicious that this is what is causing the errors for some of my users on checkout…other than just removing it, does anyone know how I might fix this “text domain” problem within the code I added to the theme functions.php per the above?

    Thanks.

    • Hey Christian, for each part of the code that looks like this: __( 'ft' );, try to replace it with something like this: __( 'ft', 'mytextdomain' );. Alternatively, you don’t have to translate it, and could just remove the translation function wrapping each unit.

      • Thanks much. I removed the code to see if that would fix the real problem and it didn’t…so, I don’t think this addon code was really the issue after all, but still, good information to know and understand.

        I’m really stumped with this problem of Checkout/Proceed to Paypal generating an “Empty Cart” Paypal page error for certain users (not all). I myself can’t replicate the issue, but it seems either session or cookie related, however, even with all cookies allowed, some users have issues.

        sigh.

Hmm, looks like this article is quite old! Its content may be outdated, so comments are now closed.