Woocommerce:在标题中添加购物车重量


Woocommerce: Adding Cart weight in header

我想在购物车旁边的标题中添加购物车重量。只是推车的总重量。到目前为止,我已经走了:

<tr class="total-weight">
  <?php global $woocommerce; ?>
  <th><?php _e('Total Weight', 'woocommerce'); ?></strong></th>
  <td><span class="amount"><?php
    $total_weight = $woocommerce->cart->cart_contents_weight;
    $total_weight .= ' '.get_option('woocommerce_weight_unit');
    echo $total_weight;
   ?></span></td>
 </tr>

这段代码输出购物车的重量,但我无法弄清楚如何在标题中添加它。

任何帮助将不胜感激

您可以将代码保存为函数,只需在需要显示总权重的位置调用该函数即可。例如:

function display_weight(){
?>
<tr class="total-weight">
  <?php global $woocommerce; ?>
  <th><?php _e('Total Weight', 'woocommerce'); ?></strong></th>
  <td><span class="amount"><?php
    $total_weight = $woocommerce->cart->cart_contents_weight;
    $total_weight .= ' '.get_option('woocommerce_weight_unit');
    echo $total_weight;
  ?></span></td>
</tr>
<?php
}