WooCommerce – Bulk Length, Width, Height & Weight Using A Shipping Plugin

By March 15, 2019 March 20th, 2019 Web Development, Website Design

I found myself in a situation where I pre-loaded an entire WooCommerce store with products before the client decided on the shipping method. There I was with a little over 400 products, staring at the new weight, length and height fields that the plugin added to each product entry. Rather then going through every product and manually adding the information, and since every product was basically the same with only a few exceptions, I added the following code to my functions.php to add base length, height & weight so it could calculate shipping. I could then leave all the fields blank and only modify the few variations that were outside of this default.

Obviously, WooCommerce has a plugin to add a base shipping rate to UPS and FedEX, but it’s $79 bucks a year. (WHAT?!) This code however, is free. Work smarter, not harder – here’s the code:



// To set Default Length
add_filter( 'woocommerce_product_get_length', 'wd_product_default_length' );
add_filter( 'woocommerce_product_variation_get_length', 'wd_product_default_length' );  // For variable product variations
if( ! function_exists('wd_product_default_length') ) {
  function wd_product_default_length( $length) {
    $default_length = 10;      // Provide default Length
    if( empty($length) ) {
      return $default_length;
    }
    else {
      return $length;
    }
  }
}
// To set Default Width
add_filter( 'woocommerce_product_get_width', 'wd_product_default_width');
add_filter( 'woocommerce_product_variation_get_width', 'wd_product_default_width' );  // For variable product variations
if( ! function_exists('wd_product_default_width') ) {
  function wd_product_default_width( $width) {
    $default_width = 11;      // Provide default Width
    if( empty($width) ) {
      return $default_width;
    }
    else {
      return $width;
    }
  }
}
// To set Default Height
add_filter( 'woocommerce_product_get_height', 'wd_product_default_height');
add_filter( 'woocommerce_product_variation_get_height', 'wd_product_default_height' );  // For variable product variations
if( ! function_exists('wd_product_default_height')) {
  function wd_product_default_height( $height) {
    $default_height = 12;      // Provide default Height
    if( empty($height) ) {
      return $default_height;
    }
    else {
      return $height;
    }
  }
}
// To set Default Weight
add_filter( 'woocommerce_product_get_weight', 'wd_product_default_weight' );
add_filter( 'woocommerce_product_variation_get_weight', 'wd_product_default_weight' );  // For variable product variations
if( ! function_exists('wd_product_default_weight') ) {
  function wd_product_default_weight( $weight) {
    $default_weight = 13;      // Provide default Weight
    if( empty($weight) ) {
      return $default_weight;
    }
    else {
      return $weight;
    }
  }
}

Here’s another bonus link if you want to set bulk shipping rates by set weight groups too. It’s a little different from what I’m doing, but it’s another option.