Search code examples
phpwordpresswoocommerceattributeshook-woocommerce

How to display the product weight in kg, if more than 1000 grams


In Storefront theme, I use the code below that change the formatted weight from 1000g to 1kg:

add_action('woocommerce_after_shop_loop_item_title', 'show_weight', 10);
function show_weight()
{
    global $product;
    $attributes = $product->get_attributes();
    if ($product->has_weight()) {
        $weight = $product->get_weight();
        $weight_unit = 'grams';
        if ($weight >= 1000) {
            $weight = round(($weight / 1000), 2);
            $weight_unit = 'kg';
        }
        print '<p>Weight: ' . $weight . ' ' . $weight_unit . '</p>';
    }
}

How to display the same format in

  • Cart and checkout pages
  • Orders & e-mails

Below code is taken originally from "Display the weight of cart and order items in WooCommerce" answer thread and I have been advised to post here as a new question.

I need below code to be formated from weight

  • above 1000g to 1kg
  • and 500g as it is.
// Display the cart item weight in cart and checkout pages

add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
function display_custom_item_data( $cart_item_data, $cart_item ) {
    if ( $cart_item['data']->get_weight() > 0 ){
        $cart_item_data[] = array(
            'name' => __( 'Weight subtotal', 'woocommerce' ),
            'value' =>  ( $cart_item['quantity'] * $cart_item['data']->get_weight() )  . ' ' . get_option('woocommerce_weight_unit')
        );
    }
    return $cart_item_data;
}

// Save and Display the order item weight (everywhere)

add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data', 20, 4 );
function display_order_item_data( $item, $cart_item_key, $values, $order ) {
    if ( $values['data']->get_weight() > 0 ){
        $item->update_meta_data( __( 'Weight subtotal', 'woocommerce' ), ( $cart_item['quantity'] * $cart_item['data']->get_weight() )  . ' ' . get_option('woocommerce_weight_unit') );
    }
}

Solution

  • The answer to this question assumes that the weight unit is set to 'gram' via the WooCommerce settings

    weight unit settings


    So than you can use the following to display the cart item weight in cart and checkout pages.

    Comment with explanation added in the code

    // Display the cart item weight in cart and checkout pages
    function display_custom_item_data( $cart_item_data, $cart_item ) {
        // Get weight
        $weight = $cart_item['data']->get_weight();
        
        if ( $weight > 0 ) {
            // Set unit
            $weight_unit = 'grams';
            
            // Calculate
            $total_weight = $cart_item['quantity'] * $weight;
            
            if ( $total_weight >= 1000 ) {
                $total_weight = round(($total_weight / 1000), 2);
                $weight_unit = 'kg'; // Change unit
            }
            
            $cart_item_data[] = array(
                'name' => __( 'Weight subtotal', 'woocommerce' ),
                'value' =>  $total_weight . ' ' . $weight_unit,
            );
        }
        return $cart_item_data;
    }
    add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
    
    // Save and Display the order item weight (everywhere)
    function display_order_item_data( $item, $cart_item_key, $values, $order ) {
        // Get weight
        $weight = $values['data']->get_weight();
        
        if ( $weight > 0 ) {
            // Set unit
            $weight_unit = 'grams';
            
            // Calculate
            $total_weight = $item['quantity'] * $weight;
    
            if ( $total_weight >= 1000 ) {
                $total_weight = round(($total_weight / 1000), 2);
                $weight_unit = 'kg'; // Change unit
            }       
            
            $item->update_meta_data( __( 'Weight subtotal', 'woocommerce' ), ( $total_weight )  . ' ' . $weight_unit );
        }
    }
    add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data', 20, 4 );