Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@sevenkader sevenkader commented Sep 24, 2021

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

@saleebm

This comment has been minimized.

Copy link

@saleebm saleebm commented Oct 14, 2021

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

@shanomurphy

This comment has been minimized.

Copy link

@shanomurphy shanomurphy commented Oct 23, 2021

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.

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