Sta*_*tok 3 php wordpress cart woocommerce hook-woocommerce
我正在使用 Woocommerce,我正在尝试在购物车页面上显示每个产品的产品重量。
我用过这个:
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>';
}
Run Code Online (Sandbox Code Playgroud)
它显示总购物车重量。我还想显示购物车中每件商品的重量。
如何在购物车页面显示每件商品的产品重量?
谢谢。
有多种方法可以做到。您可以使用woocommerce_get_item_data过滤器挂钩中挂钩的自定义函数来显示每个购物车项目的产品重量:
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;
}
Run Code Online (Sandbox Code Playgroud)
*代码位于活动子主题(或活动主题)的 function.php 文件中。测试和工作。
要获得总购物车订单项重量(产品重量 x 产品数量),只需替换:
$item_weight = $cart_item['data']->get_weight();
Run Code Online (Sandbox Code Playgroud)
经过:
$item_weight = $cart_item['data']->get_weight() * $cart_item['quantity'];
Run Code Online (Sandbox Code Playgroud)