WooCommerce 在购物车中显示商品的总重量

WooCommerce 每个商品都会有一个重量参数,在有些WooCommerce电子商务站点上,购物的运费是要根据重量计算的,这种情况下,在购物车中显示一个添加到购物车中所有商品的总重量是非常重要的。WooCommerce的订单是有一个总重量字段的,只不过在默认情况下没显示出来。我们只需要把这个参数调出来就可以了。

add_action('woocommerce_cart_collaterals', 'myprefix_cart_extra_info');
function myprefix_cart_extra_info() {
    global $woocommerce;
    echo '<div class="cart-extra-info">';
    echo '<p class="total-weight">' . __('Total Weight:', 'woocommerce');
    echo ' ' . $woocommerce->cart->cart_contents_weight . ' ' . get_option('woocommerce_weight_unit');
    echo '</p>';
    echo '</div>';
}
add_action( 'woocommerce_review_order_before_shipping', 'pft_checkout_weight_info' );
function pft_checkout_weight_info() {
    global $woocommerce;
    echo '<tr>';
    echo '<th>Total Weight</th>';
    echo '<td>' . $woocommerce->cart->cart_contents_weight . ' ' . get_option('woocommerce_weight_unit') . '</td>';
    echo '</tr>';
}