1
votes

I read and tried the one at this link:

Display the weight of each cart items in WooCommerce

It does not work. I need to have the total weight of each item at cart and checkout; for example the weight of an article is 500 gr and if I add to the cart 3 of his article, it should show 1500 gr.

I have configured the weight for some products at 0.2 kg (becouse the wordpress system is configured in Kg) but if I try that code in the previous link, under the name of the product , it shows me "weight 0".

I have used this code:

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

Then I tried another code to have the total cart weight and it works perfectly , for each change I make in the cart, the weight is correct. The code I used is:

    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' );

I need absolutely the total weight for each item line (not the weight for a single product only) : the result should be given from item_weight * item_quantity (0.2 x 6 = 1.2)


Is it possible to have this information also in single product page? when a user change the value of the quantity to add to cart, the system should show also the total weight, example: a product weight is 0.2 kg if the user change the value from 1 to 2 , the weight showed should be 0.4 , if the value is 3 so the total weight should be 0.6 and then the user click on add to cart.

I am usic this code below to change price total on quantity change:

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

}
1
To answer the 2nd part of your question, you can do it with jQuery, but it doesn't make sense. Example: if you now change the quantity, the price does not change, it continues to show the price for 1 product. So then you should adjust this too I guess7uc1f3r
Yes sire. It is correct. But i already have a code that change price on quantità change. Any suggestion for the query code for this fiction?Vanity Island
What code do you already use for this? can you edit your question and add it? the weight code will be similar7uc1f3r
I published the code to change price on quantity changeVanity Island

1 Answers

2
votes

In your code $product_qty is undefined

So to display the total weight of each item at cart and checkout page in WooCommerce you can use:

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' );

To adjust the weight and price based on the quantity, on the single product page, you can use:

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