我经营一个 woocommerce 网站,重量以磅为单位,我需要将它们转换为公斤。
我从网站下载了这个脚本,它完成了这项工作,但它设置了带有很多小数的新权重,不知道如何四舍五入。
我试图使用round($new_weight, 2);
但它没有接受或不确定将其放置在哪里或如何放置。
NOTE:我对php一无所知。该脚本是一个页面模板。当你打开它时它就会发生变化。
<?php
/*
*
* Template name: wconvertor
*/
get_header();
$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(). " weight = ".$weight;
$new_weight = $weight / 2.2 ;
round($new_weight,2);
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(); ?>
阅读此参考资料:
http://php.net/manual/en/function.round.php http://php.net/manual/en/function.round.php
Example:
<?php
echo round(1.95583, 2); // 1.96
echo round(5.045, 2); // 5.05
echo round(5.055, 2); // 5.06
?>
将您的代码编辑为:
$new_weight = $weight / 2.2 ;
$new_weight = round($new_weight,2);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)