在WooCommerce的购物车和结账页面上显示每个商品的总重量



我阅读并尝试了这个链接上的一个:

在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(得出结果


是否可以在单个产品页面中也包含此信息?当用户更改要添加到购物车的数量值时,系统还应显示总重量,例如:产品重量为0.2公斤,如果用户将值从1更改为2,则显示的重量应为0.4,如果值为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
}

在您的代码中,$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 );

最新更新