• Resolved danis1984

    (@danis1984)


    Hi,

    How is possible to remove the decimals from the product weight?
    I have around 2600 products currently that i migrate and is impossible to edit them one by one.

    Here is how it looks like now: https://ibb.co/fnkgXfj

    I guess there will be somehow a way to do it.

    Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Stef

    (@serafinnyc)

    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.
    Thread Starter danis1984

    (@danis1984)

    Hi,

    I add the code but now the weight is empty: https://ibb.co/KX8s6Hj (Μ/Δ means N/A)

    Plugin Support kellymetal a11n

    (@kellymetal)

    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

    Thread Starter danis1984

    (@danis1984)

    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!

    Plugin Support kellymetal a11n

    (@kellymetal)

    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;
    }
    
    Thread Starter danis1984

    (@danis1984)

    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

    Plugin Support kellymetal a11n

    (@kellymetal)

    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.

    Thread Starter danis1984

    (@danis1984)

    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.

    Thread Starter danis1984

    (@danis1984)

    Issue resolved.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to remove decimals from product weight’ is closed to new replies.