Every WooCommerce product has a weight parameter. On some WooCommerce ecommerce sites, shipping costs are calculated based on weight. In that situation, displaying the total weight of all products that have been added to the cart is very important.
WooCommerce already stores a total weight value for the order, but it is not shown by default. During development, we only need to output that value manually.
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>';
}
In practice, WooCommerce development follows the same basic idea as other types of ecommerce site development: pull data from the database, render it through templates on the front end, and format it with CSS so it matches the design the user sees and clicks. The difference is that WooCommerce sits on top of WordPress, which provides a very powerful backend and makes long-term technical support much easier.
