Skip to content

Instantly share code, notes, and snippets.

@xadapter
Last active January 26, 2022 21:06
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save xadapter/4fb8dbfc6c025630558e43488775eb7d to your computer and use it in GitHub Desktop.
Save xadapter/4fb8dbfc6c025630558e43488775eb7d to your computer and use it in GitHub Desktop.
Set a default weight and dimensions for the products in your WooCommerce store. Useful for WooCommerce shipping plugins like https://www.pluginhive.com/product-category/woocommerce-plugin/woocommerce-shipping/ Supports ELEX Shipping Plugins https://elextensions.com/product-category/shipping/
/**
* 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
* PluginHive Plugins : https://www.pluginhive.com/product-category/woocommerce-plugin/
* 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;
}
}
}
@sevenkader
Copy link

This changes the default, but what if a value has already been set, does this affect that value that was set?

@saleebm
Copy link

saleebm commented Oct 14, 2021

@sevenkader it doesn't affect simple products with values already set

@shanomurphy
Copy link

Warning this appears to cause problems with variable products where you want the variations to inherit the shipping dimensions/weight from the parent.

By default if you set the shipping dimensions on the parent the variations will inherit them. However if you are using the above filters the variations will not inherit their parent shipping dimensions and will use the defaults you set using the filters instead.

This means you have to manually enter the shipping dimensions on every variation which is a pain if they are all the same.

@eric1905
Copy link

@shanomurphy
Do you know if there exists a code snippet to fix this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment