如何显示 WooCommerce 产品重量值的格式化重量
How to show the formatted weight from WooCommerce product weight value
我有以下代码,效果很好,它仅在订单的元字段有值时才显示文本 (CARRIER),否则不显示文本:
<?php
$text_order_meta = get_post_meta($order->get_order_number(), 'carrier_name', true);
if( ! empty($text_order_meta) )
{ ?> <p> <?php printf( '<b>CARRIER:</b> ' . esc_html( '%s', 'woocommerce' ), esc_html($text_order_meta) );?>
</p> <?php
}
?>
我想做类似显示订单商品重量的事情。
我知道如何获取每个产品的重量,并且只有当它有值时才显示它:
<?php $product; $attributes = $product->get_attributes(); if ( $product->has_weight() ) { echo $product->get_weight(); } ?>
如何在 WooCommerce 中获取和显示订单商品重量?
您需要使用wc_format_weight()
函数来获取格式化的重量,所以只需替换:
echo $product->get_weight();
与:
echo wc_format_weight($product->get_weight());
我有以下代码,效果很好,它仅在订单的元字段有值时才显示文本 (CARRIER),否则不显示文本:
<?php
$text_order_meta = get_post_meta($order->get_order_number(), 'carrier_name', true);
if( ! empty($text_order_meta) )
{ ?> <p> <?php printf( '<b>CARRIER:</b> ' . esc_html( '%s', 'woocommerce' ), esc_html($text_order_meta) );?>
</p> <?php
}
?>
我想做类似显示订单商品重量的事情。
我知道如何获取每个产品的重量,并且只有当它有值时才显示它:
<?php $product; $attributes = $product->get_attributes(); if ( $product->has_weight() ) { echo $product->get_weight(); } ?>
如何在 WooCommerce 中获取和显示订单商品重量?
您需要使用wc_format_weight()
函数来获取格式化的重量,所以只需替换:
echo $product->get_weight();
与:
echo wc_format_weight($product->get_weight());