显示WooCommerce中每个购物车物品的重量 [英] Display the weight of each cart items in WooCommerce

查看:109
本文介绍了显示WooCommerce中每个购物车物品的重量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Woocommerce,并且试图在购物车页面上显示每种产品的产品重量。

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

我已经使用过:

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?

谢谢。

推荐答案

有多种方法可以做到这一点。您可以使用挂钩在 woocommerce_get_item_data 过滤器挂钩中的自定义功能来显示每个购物车商品的产品重量:

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

*该代码进入活动子主题的function.php文件中(或活动主题)。经过测试并有效。

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

要获取订单项的总重量(产品重量x产品数量),请替换:

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

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

通过:

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






参考:类别WC_Product :: get_weight()

这篇关于显示WooCommerce中每个购物车物品的重量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆