Quantcast
Channel: WordPress.org Forums » All Topics
Viewing all articles
Browse latest Browse all 130709

My solution for backend total weight function in order view

$
0
0

Replies: 0

My below function works and adds an entry to the backend order screen. If anyone has a better, best-practice method, please feel free to comment.

function order_custom_field_function_name($order)
{
    $order_id     = $order->id;
    $items        = $order->get_items();
    $total_weight = 0;
    foreach ($items as $item) {
        $item_metas  = get_post_meta($item['product_id']);
        $weight      = $item_metas['_weight']['0'];
        $quantity    = $item['qty'];
        $item_weight = ($weight * $quantity);
        $total_weight += $item_weight;
    }
    $total_weight = $total_weight / 1000;
    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);

Viewing all articles
Browse latest Browse all 130709

Trending Articles