skip to Main Content

I want to add weight metadata to an order in the Frontend: My Account – Orders. I tried some things but it is not working.

I want to add is $order->get_weight(); as meta data to the order but I am getting an error.

I am already half way using this code to add a new column and show product description and quantity:

add_filter( 'woocommerce_my_account_my_orders_columns', 'additional_my_account_orders_column', 10, 1 );
function additional_my_account_orders_column( $columns ) {
    $new_columns = [];

    foreach ( $columns as $key => $name ) {
        $new_columns[ $key ] = $name;

        if ( 'order-status' === $key ) {
            $new_columns['order-items'] = __( 'DescripciĆ³n', 'woocommerce' );
        }
    }
    return $new_columns;
}

add_action( 'woocommerce_my_account_my_orders_column_order-items', 'additional_my_account_orders_column_content', 10, 1 );
function additional_my_account_orders_column_content( $order ) {
    $details = array();

    foreach( $order->get_items() as $item )
        $details[] = $item->get_name() . ' × ' . $item->get_quantity();

    echo count( $details ) > 0 ? implode( '<br>', $details ) : '&ndash;';
}

Hope someone can help me get in the right direction.

2

Answers


  1. Just came across this, have you tried it? https://gist.github.com/kloon/5299119?permalink_comment_id=1415838

    Here is a copy of the code in case the link eventually dies:

    <?php
    add_filter( 'manage_edit-shop_order_columns', 'woo_order_weight_column' );
    function woo_order_weight_column( $columns ) {
      $columns['total_weight'] = __( 'Weight', 'woocommerce' );
        return $columns;
    }
    
    add_action( 'manage_shop_order_posts_custom_column', 'woo_custom_order_weight_column', 2 );
    function woo_custom_order_weight_column( $column ) {
        global $post, $woocommerce, $the_order;
    
        if ( empty( $the_order ) || $the_order->get_id() !== $post->ID )
            $the_order = new WC_Order( $post->ID );
    
        if ( $column == 'total_weight' ) {
            $weight = 0;
            if ( sizeof( $the_order->get_items() ) > 0 ) {
                foreach( $the_order->get_items() as $item ) {
                    if ( $item['product_id'] > 0 ) {
                        $_product = $item->get_product();
                        if ( ! $_product->is_virtual() ) {
                            $weight += $_product->get_weight() * $item['qty'];
                        }
                    }
                }
            }
            if ( $weight > 0 ) {
                print $weight . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) );
            } else {
                print 'N/A';
            }
        }
    }
    ?>
    
    Login or Signup to reply.
  2. This snippet inserts a new, custom column in the table of orders shown in My Account > Orders populated with the total weight of the order so the customer is aware how heavy their order was.

    Specifically, this snippet has two blocks of code. The first block inserts the column. In this example, we have inserted this column between the Order Total and Order Actions column. This can be changed by changing the column key in the code. Your custom will appear after the column key you define.

    The second block of code is where the magic happens. It first loops through each item in the order and gets it weight and times this by the quantity of this product. It then adds this weight of each product to a variable we have called $total_weight. The total weight is then output to the new column followed by the weight unit you have defined in your store settings under WordPress Dashboard > WooCommerce > Settings > Products > General > Measurements > Weight Unit.

    /**
     * Snippet Name:    WooCommerce Show Order Weight Column In My Account Order View Table
     * Snippet Author:  ecommercehints.com
     */
    
    // First, create the new table column between Total and Actions columns
    add_filter( 'woocommerce_my_account_my_orders_columns', 'ecommercehints_weight_column_my_account_orders_table', 10, 1 );
    function ecommercehints_weight_column_my_account_orders_table( $columns ) {
        $weight_column = [];
        foreach ( $columns as $key => $name ) {
            $weight_column[ $key ] = $name;
            if ( 'order-total' === $key ) { // Insert new column after Total column
                $weight_column['order-items'] = __( 'Order Weight', 'woocommerce' );
            }
        }
        return $weight_column;
    }
    
    // Second, insert the data from the order into the new column
    add_action( 'woocommerce_my_account_my_orders_column_order-items', 'ecommercehints_get_order_weight', 10, 1 );
    function ecommercehints_get_order_weight( $order ) {
        $weight_unit = get_option('woocommerce_weight_unit');
        $total_weight = 0;
        foreach( $order->get_items() as $item_id => $item ){
            $quantity = $item->get_quantity();
            $product = $item->get_product();
            $product_weight = $product->get_weight();
            $total_weight += floatval( $product_weight * $quantity );
        }
        echo $total_weight . $weight_unit;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search