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

时间:2015-01-19 21:20:58

标签: php wordpress woocommerce

我想将购物车重量添加到购物车旁边的标题中。只是购物车的总重量。我到目前为止:

<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>

此代码输出购物车的重量,但我无法弄清楚如何将其添加到标题中。

任何帮助都会非常感激

1 个答案:

答案 0 :(得分:1)

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

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
}