Woocommerce:在购物车页面上显示产品重量

Woocommerce: Displaying product weight on cart page

我想在购物车页面上显示产品重量:

我已经走了这么远:

<td class="product-weight">
   <?php
     echo apply_filters( 'woocommerce_cart_item_weight', WC()->cart->cart_contents_weight);
   ?>
 </td><!-- /.product-weight -->

这段代码将显示购物车的总重量,但我想要单个产品的重量。

如有任何帮助,我们将不胜感激。参考 URL: Link [请添加一些产品]

请尝试以下代码。 未测试.

global $woocommerce;

foreach ( $woocommerce->cart->get_cart() as $item_id => $values ) {

        $_product = $values['data'];
        $weight = $_product->weight;

        }

试试这个:

global $woocommerce;

if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values )
    {
        $_product = $values['data'];
        $weight = $_product->weight; echo $weight;
    }
}

已编辑:

<td class="product-weight">
<?php
     echo apply_filters( 'woocommerce_cart_item_weight', $_product->get_weight());
?>
</td><!-- /.product-weight -->