I was kindly assisted with some code that helped to add the combined value of custom field attached to each product - in this case volume of whole order in m3.
I would like to display the m3 volume in the table below product list on the thankyou page - does anyone know the hook i should use. Below is the code that shows my total volume on the cart and checkout page.
add_action( 'woocommerce_cart_totals_before_shipping',
'display_cart_volume_total', 20 );
add_action( 'woocommerce_review_order_before_shipping',
'display_cart_volume_total', 20 );
function display_cart_volume_total() {
$total_volume = 0;
// Loop through cart items and calculate total volume
foreach( WC()->cart->get_cart() as $cart_item ){
$product_volume = (float) get_post_meta( $cart_item['product_id'],
'_item_volume', true );
$total_volume += $product_volume * $cart_item['quantity'];
}
if( $total_volume > 0 ){
// The Output
echo ' <tr class="cart-total-volume">
<th>' . __( "Total Shipping Volume", "woocommerce" ) . '</th>
<td data-title="total-volume">' . number_format($total_volume, 2) .
' m3</td>
</tr>';
}
}
Here is the way to display the Total Volume in "Order received" (thankyou) and "View Order" (my account) pages (in front-end):
// Front: Display Total Volume in "Order received" (thankyou) and "View Order" (my account) pages
add_action( 'woocommerce_order_items_table', 'display_order_volume_total', 20 );
function display_order_volume_total() {
global $wp;
// Only in thankyou "Order-received" and My account "Order view" pages
if( is_wc_endpoint_url( 'order-received' ))
$endpoint = 'order-received';
elseif( is_wc_endpoint_url( 'view-order' ))
$endpoint = 'view-order';
else
return; // Exit
$order_id = absint( $wp->query_vars[$endpoint] ); // Get Order ID
$order_id > 0 ? $order = wc_get_order($order_id) : exit(); // Get the WC_Order object
$total_volume = 0;
echo '</tbody><tbody>';
// Loop through cart items and calculate total volume
foreach( $order->get_items() as $item ){
$product_volume = (float) get_post_meta( $item->get_product_id(), '_item_volume', true );
$total_volume += $product_volume * $item->get_quantity();
}
if( $total_volume > 0 ){
// The Output
echo '<tr>
<th scope="row">' . __( "Total Shipping Volume", "woocommerce" ) . '</th>
<td data-title="total-volume">' . number_format($total_volume, 2) . ' m3</td>
</tr>';
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.