Home WooCommerce cart custom field total calculation
Reply: 0

WooCommerce cart custom field total calculation

user1823
1#
user1823 Published in June 19, 2018, 4:11 pm

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?

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!).

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301756 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO