WordPress.org

Forums

WooCommerce - excelling eCommerce
[resolved] Show the order's total weight on the admin edit/view order page (2 posts)

  1. Gravuj Miklos Henrich
    Member
    Posted 1 year ago #

    I am trying to add a new custom field on the admin edit order page after the "Payment Method" field. The custom field must be the total weight of the order.

    function order_custom_field_function_name( $order ) {
        echo "<p><strong>Total Weight:</strong>" .  . " kg</p>";
    }
    
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'order_custom_field_function_name', 10, 1 );

    With the above code I am displaying the "Total Weight" in the desired place, but I don't know how to get the "total weight" value of the order.

    Any indication would be helpful. Thanks.

    https://wordpress.org/plugins/woocommerce/

  2. Gravuj Miklos Henrich
    Member
    Posted 1 year ago #

    It did the following code:

    add_action( 'woocommerce_checkout_update_order_meta', 'add_cart_weight' );
    
    function order_custom_field_function_name( $order ) {
    	$order_id = $order->id;
    	$order = new WC_Order( $order_id );
    	$items = $order->get_items();
    	$total_weight = 0;
    	foreach( $items as $item ) {
    		/*echo '<pre>';
    		print_r($item);
    		echo '</pre>';*/
    		$item_metas = get_post_meta( $item['product_id'] );
    		$weight = $item_metas['_weight']['0'];
    		$quantity = $item['qty'];
    		$item_weight = ( $weight * $quantity );
    		$total_weight += $item_weight;
    	}
    	echo "<p><strong>Total Weight: <span style='color:green;'>" . $total_weight . "</span></strong> kg</p>";
    }
    
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'order_custom_field_function_name', 10, 1 );

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WooCommerce - excelling eCommerce
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic