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

时间:2015-01-19 12:11:39

标签: php wordpress woocommerce

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

我到目前为止:

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

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

任何帮助将不胜感激。参考网址:Link [请添加少量产品]

2 个答案:

答案 0 :(得分:2)

试试这个:

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

答案 1 :(得分:0)

请尝试以下代码。 未经过测试

global $woocommerce;

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

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

        }