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.
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(); ?>
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!!
great, glad it helped George
May I learn where did you insert this script ?
Hi how do you get rid of the 2 decimal places I have? So a product might say 900.00g
TIA
Hello,
Where should we insert the script? in functions.php?
Thank you