我阅读并尝试了以下链接:
Display the weight of each cart items in WooCommerce
它不起作用。我需要在购物车和结帐时输入每件物品的总重量;例如,一件商品的重量为500克,如果我将其添加到他的商品的购物车3中,它应该显示为1500克。
我已经将某些产品的重量配置为0.2千克(因为wordpress系统以千克为单位配置),但是如果我尝试在上一个链接中的代码,在产品名称下显示“ 0 ”。
我使用了以下代码:
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() * $product_qty;
$item_data[] = array(
'key' => __('Weight', 'woocommerce'),
'value' => $item_weight,
'display' => $item_weight . ' ' . get_option('woocommerce_weight_unit')
);
return $item_data;
}
然后,我尝试了另一个代码来获得购物车的总重量,并且它可以完美地工作,对于我在购物车中进行的每次更改,重量都是正确的。我使用的代码是:
if ( WC()->cart->needs_shipping() ) : ?>
<tr class="shipping">
<th><?php _e( 'Peso', 'woocommerce-cart-weight' ); ?></th>
<td><span class="label"><?php echo $woocommerce->cart->cart_contents_weight . ' ' . get_option( 'woocommerce_weight_unit' ); ?></span></td>
</tr>
<?php endif;
}
add_action( 'woocommerce_cart_totals_after_order_total', 'wcw_cart' );
add_action( 'woocommerce_review_order_after_order_total', 'wcw_cart' );
我绝对需要每个项目行的总重量(而不是单个产品的重量): 结果应为item_weight * item_quantity(0.2 x 6 = 1.2)
是否可以在单个产品页面中包含此信息?当用户更改要添加到购物车的数量的值时,系统还应显示总重量,例如:如果用户将值从1更改为2,则产品重量为0.2 kg,如果用户将其更改为0.4 kg该值为3,因此总重量应为0.6,然后用户单击添加到购物车。
我很乐意使用下面的代码来更改数量上的总价格:
add_action( 'woocommerce_single_product_summary',
'woocommerce_total_product_price', 31 );
function woocommerce_total_product_price() {
global $woocommerce, $product;
// let's setup our divs
echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s
%s</div>',__('Totale Prodotto:','woocommerce'),'<span
class="price">'.$product->get_price().'</span>');
?>
<script>
jQuery(function($){
var price = <?php echo $product->get_price(); ?>,
currency = '<?php echo get_woocommerce_currency_symbol();
?>';
$('[name=quantity]').change(function(){
if (!(this.value < 1)) {
var product_total = parseFloat(price * this.value);
$('#product_total_price .price').html( currency +
product_total.toFixed(2));
}
});
});
</script>
<?php
}
答案 0 :(得分:1)
在您的代码$product_qty
中未定义
因此,要在WooCommerce中的购物车和结帐页面上显示每个项目的总重量,可以使用:
function displaying_cart_items_weight( $item_data, $cart_item ) {
// Product quantity
$product_qty = $cart_item['quantity'];
// Calculate total item weight
$item_weight = $cart_item['data']->get_weight() * $product_qty;
$item_data[] = array(
'key' => __('Weight', 'woocommerce'),
'value' => $item_weight,
'display' => $item_weight . ' ' . get_option('woocommerce_weight_unit')
);
return $item_data;
}
add_filter( 'woocommerce_get_item_data', 'displaying_cart_items_weight', 10, 2 );
function wcw_cart() {
global $woocommerce;
if ( WC()->cart->needs_shipping() ) : ?>
<tr class="shipping">
<th><?php _e( 'Peso', 'woocommerce-cart-weight' ); ?></th>
<td><span class="label"><?php echo $woocommerce->cart->cart_contents_weight . ' ' . get_option( 'woocommerce_weight_unit' ); ?></span></td>
</tr>
<?php endif;
}
add_action( 'woocommerce_cart_totals_after_order_total', 'wcw_cart' );
add_action( 'woocommerce_review_order_after_order_total', 'wcw_cart' );
要基于数量调整重量和价格,请在单个产品页面上使用:
function woocommerce_total_product_price() {
global $woocommerce, $product;
// let's setup our divs
echo sprintf('<div id="product_total_price" style="margin-bottom:20px;">%s %s</div>', __('Totale Prodotto:','woocommerce'),'<span class="price">' . $product->get_price() . '</span>');
echo sprintf('<div id="product_total_weight" style="margin-bottom:20px;">%s %s</div>', __('Totale Peso:','woocommerce'),'<span class="weight">' . $product->get_weight() . '</span>');
?>
<script>
jQuery(function($) {
var price = <?php echo $product->get_price(); ?>,
currency = '<?php echo get_woocommerce_currency_symbol(); ?>';
var weight = <?php echo $product->get_weight(); ?>;
$('[name=quantity]').change(function() {
if (!(this.value < 1)) {
var product_total = parseFloat(price * this.value);
$('#product_total_price .price').html( currency + product_total.toFixed(2) );
var weight_total = parseFloat(weight * this.value);
$('#product_total_weight .weight').html( weight_total.toFixed(2) + ' kg');
}
});
});
</script>
<?php
}
add_action( 'woocommerce_single_product_summary', 'woocommerce_total_product_price', 31 );