如何在 WooCommerce 購物車中顯示商品的總重量

如何在 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>';
}
文章沒看懂?代碼不會用?需要幫助您可以去論壇提問自助服務台

作者園長

分享關於您的一些信息。可能會被公開。