Snippets – Bulk Set WooCommerce Shipping Default Size, Width, Height, Weight in Functions.php

By February 8, 2020June 24th, 2020Start

Got a couple hundred products and they are all the same size, width and height? Set the default in the functions and forget about it.

You also can enter any size, width or height to override the default.


/**
* Snippet to set default weight and Dimension if it's not set for any product.
* Created at : 14 May 2018
* Updated at : 16 May 2018
* Xadapter Plugins : https://www.xadapter.com/shop/
* Gist Link : https://gist.github.com/xadapter/4fb8dbfc6c025630558e43488775eb7d
*/
// To set Default Length
add_filter( 'woocommerce_product_get_length', 'xa_product_default_length' );
add_filter( 'woocommerce_product_variation_get_length', 'xa_product_default_length' );  // For variable product variations
if( ! function_exists('xa_product_default_length') ) {
  function xa_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', 'xa_product_default_width');
add_filter( 'woocommerce_product_variation_get_width', 'xa_product_default_width' );  // For variable product variations
if( ! function_exists('xa_product_default_width') ) {
  function xa_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', 'xa_product_default_height');
add_filter( 'woocommerce_product_variation_get_height', 'xa_product_default_height' );  // For variable product variations
if( ! function_exists('xa_product_default_height')) {
  function xa_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', 'xa_product_default_weight' );
add_filter( 'woocommerce_product_variation_get_weight', 'xa_product_default_weight' );  // For variable product variations
if( ! function_exists('xa_product_default_weight') ) {
  function xa_product_default_weight( $weight) {
    $default_weight = 13;      // Provide default Weight
    if( empty($weight) ) {
      return $default_weight;
    }
    else {
      return $weight;
    }
  }
}