You can add this snippet to your child theme’s functions.php file
add_filter( 'woocommerce_format_weight', 'custom_format_weight', 90, 2 );
function custom_format_weight( $weight_string, $weight ){
$weight_string = intval( $weight );
if ( ! empty( $weight_string ) ) {
$weight_string .= ' ' . get_option( 'woocommerce_weight_unit' );
} else {
$weight_string = __( 'N/A', 'woocommerce' );
}
return $weight_string;
}
If that works, please click “resolved” if it doesn’t let me know.
Thanks
-
This reply was modified 3 years, 1 month ago by Stef.
Hi,
I add the code but now the weight is empty: https://ibb.co/KX8s6Hj (Μ/Δ means N/A)
Hi there @danis1984,
I tried @serafinnyc’s snippet on my personal site, and it worked well. For a product with weight of 6.666 oz, after adding the snippet it displayed as 6 oz
Please send over a screenshot of your product’s Edit Product > Product Data > Shipping tab as well as your settings under WooCommerce > Settings > Products. I recommend https://snipboard.io for easily sharing screenshots – please follow the instructions on the page, then paste the URL in your reply here.
Also, the URL to the product on the frontend of the site
Hi and thanks for your response.
Here are is photo from my product settings: https://snipboard.io/zLGrbE.jpg
Here are the settings from shipping options inside the product page: https://snipboard.io/BKhJ7k.jpg
Thanks!
Thank you for sending over those screenshots!
So, it looks like it was because your product weight was 0.10000000
which was being converted to 0
as an integer, which then was not passing the check for being empty
.
Please try changing it to this instead:
// Remove decimals from product weight (convert to integer)
add_filter( 'woocommerce_format_weight', 'custom_format_weight', 90, 2 );
function custom_format_weight( $weight_string, $weight ){
$weight_string = intval( $weight );
if ( ! is_null( $weight_string ) ) {
$weight_string .= ' ' . get_option( 'woocommerce_weight_unit' );
} else {
$weight_string = __( 'N/A', 'woocommerce' );
}
return $weight_string;
}
Hi, and thanks for your response.
I apply the new code but now it show me 0kg in the product.
Check here: https://ibb.co/tL7Y0Wb
Hi there,
Yes, since your product weight is 0.10000
, removing the decimals would end up with 0
.
To clarify, were you just trying to remove all the trailing 0’s at the end? Not completely remove the decimals? If so, that would need something entirely different.
The snippet provided is meant to convert the weights into integers (i.e. 10
, 4
, 1
instead of 10.123
, 4.165
, 1.794
), thus removing the decimals. Please confirm your goal, then we can look into that further.
Thanks again for your response.
I basically want to remove the most decimals. For example instead of 0.50000000 to be 0.50 or 0.5 so leave only 1 or 2 decimals.
Sorry if I was unclear.