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>'; }