WooCommerce cart custom field total calculation

I’ve added a custom field to the WooCommerce products called ‘Unit Volume’ and have overridden the WooCommerce ‘cart.php’ template using a child theme. To the cart table I’ve added a ‘Unit Volume’ column (volume of each individual product) and a ‘Volume Subtotal’ column (unit volume multiplied by the quantity in the cart).

PROBLEM: In the separate ‘cart-totals.php’ template I’m trying to add up the total volume using the ‘Volume Subtotals’ column, and display it under the cart table with the price totals. It should just be a case of adding up the ‘Volume Subtotals’ column?

Read More

Here’s my working code for the ‘Volume Subtotal’ column that I want to add up to get the total:

<td class="product-volume-subtotal">
<span class="amount"><?php
    global $woocommerce;
    // custom field value - unit volume
    $product_volume = get_post_meta( $product_id, 'unit_volume', true );
    // multiply by quantity
    $product_volume_subtotal = $product_volume * $cart_item['quantity'];
    // output sum total
    print ($product_volume_subtotal);
?>m<sup>3</sup></span>
</td>

I’ve tried using the first few lines from this, but I can’t get anything to work: woocommerce add fee to cart based on quantity

There must be a way to pull the $product_volume_subtotal from each row and add them up??

I guess starting with something like this?

function custom_volume_total(){ 
global $woocommerce;
// Get Cart Contents
$cart = $woocommerce->cart->get_cart();
// get $product_volume_subtotal and add it up here?
}

Any help much appreciated, and apologies for my terrible php knowledge (I’m a designer!).

Related posts

Leave a Reply