Woocommerce: Displaying product weight on cart pag

2019-04-13 21:55发布

I want to display product weight on cart page:

I've come so far:

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

This piece of code will display the total weight of cart but I want weight of individual product.

Any help would be appreciated. Reference URL: Link [plz add few products]

2条回答
仙女界的扛把子
2楼-- · 2019-04-13 22:29

Please try the below code. It is not tested.

global $woocommerce;

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

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

        }
查看更多
【Aperson】
3楼-- · 2019-04-13 22:37

Try out this :

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

Edited:

<td class="product-weight">
<?php
     echo apply_filters( 'woocommerce_cart_item_weight', $_product->get_weight());
?>
</td><!-- /.product-weight -->
查看更多
登录 后发表回答