Quantcast
Channel: Fox Run Software » justin
Viewing all articles
Browse latest Browse all 10

How to Add Dimension Units to WooCommerce

$
0
0


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:

Disclaimer: this post does contain some affiliate links. I would have linked to them anyway though, please read our affiliate programs policy for further details.

The post How to Add Dimension Units to WooCommerce appeared first on Fox Run Software.


Viewing all articles
Browse latest Browse all 10

Trending Articles