Woocommerce – Converting grams to kilograms ( product weights)

If your using product weights in Woocommerce, you might find you want to change from grams to kilograms at some stage.

There are 2 parts to this:

Part 1:

Firstly in wp-admin you need to change a setting on the Woocommerce (below).

However this doesn’t automatically convert grams to kilograms.

convert from grams to kilograms setting

Part 2:

Converting existing products grams to kilograms.

For this you’ll need to use a little script, I created this one as a page template.

<?php
/*
 *
 * Template name: list products
 */
get_header();
?>




<?php

$args = array( 'post_type' => 'product', 'posts_per_page' => 2500  );

$loop = new WP_Query( $args );

while ( $loop->have_posts() ) : $loop->the_post(); 
global $product; 



$weight= get_post_meta( get_the_ID(), '_weight', true);

if ( ! empty( $weight ) ) {
    
    echo "id = ".get_the_ID(). " wieght = ".$weight; 
    
    $new_weight = $weight / 1000 ; 
    
    echo "<br>new weight = ".$new_weight."  ********************";
    
    // id , key , value 
    $result = update_post_meta(get_the_ID(), '_weight', $new_weight);
    echo "result = ".$result;
    
    
}else{
    echo "<br>NO update for: ".get_the_ID();
}



endwhile; 


wp_reset_query(); 


?>




</div>

<?php get_footer(); ?>

 

 

5 thoughts on “Woocommerce – Converting grams to kilograms ( product weights)”

  1. That helped me so much my friend!! You saved me so much time. I was searching for something like this for some hours now.

    Great work!! Thank you so much!!

    Reply

Leave a Comment