3
votes

I'm using Woocommerce and I'm trying to display the product weight for each product on cart page.

I have used this:

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

It display the total cart weight. I would like to also show the weight of each item in the cart as well.

How to display the product weight of each item on cart page?

Thanks.

1
@nemus Not a duplicate …LoicTheAztec

1 Answers

4
votes

There is multiple ways to do it. You can use the custom function hooked in woocommerce_get_item_data filter hook to display the product weight of each cart items:

add_filter( 'woocommerce_get_item_data', 'displaying_cart_items_weight', 10, 2 );
function displaying_cart_items_weight( $item_data, $cart_item ) {
    $item_weight = $cart_item['data']->get_weight();
    $item_data[] = array(
        'key'       => __('Weight', 'woocommerce'),
        'value'     => $item_weight,
        'display'   => $item_weight . ' ' . get_option('woocommerce_weight_unit')
    );

    return $item_data;
}

*The Code goes in function.php file of your active child theme (or active theme). Tested and works.


To get the total cart item weight:

To get the total cart line item weight (product weight x product quantity) simply replace:

$item_weight = $cart_item['data']->get_weight();

by:

$item_weight = $cart_item['data']->get_weight() * $cart_item['quantity'];

Reference: Class WC_Product::get_weight()