I have a WooCommerce webshop with different kind of beers from all over the world. If you return a empty bottle you get a deposit of 0,10 or something like that. I made an ACF (Advanced Custom Field) where I can add the deposit price for an empty bottle. I managed to add this to my cart page and display it under the single price of a bottle (0,10) and under the subtotal if there are more bottles (0,40).
This is working in cart and checkout but I want to make a sum off the total deposit money if you return the bottles to the store.
How can I create a function that calculate the sum of all bottles in the cart that has a deposit?
I managed to get the first this working with this functions:
add_filter( 'woocommerce_cart_item_price', 'add_deposit_value_to_cart_single' , 10, 3 );
add_filter( 'woocommerce_cart_item_subtotal', 'add_deposit_value_to_cart_total', 10, 3 );
/**
* Statiegeld toevoegen onder single prijs product winkelwagen
*/
function add_deposit_value_to_cart_single( $product_price, $values ) {
$statiegeld = get_field( "statiegeld", $values['product_id']);
$dep_total = $statiegeld;
if( ! empty( $dep_total ) )
return $product_price . '<br /><small class="deposit_label">' . sprintf( __( 'statiegeld %s' ), wc_price( $dep_total ) ) . '</small>';
return $product_price;
}
/**
* Statiegeld toevoegen aan subtotaal in winkelwagen
*/
function add_deposit_value_to_cart_total( $product_price, $values ) {
$statiegeld = get_field( "statiegeld", $values['product_id']);
$dep_total = $statiegeld * $values['quantity'];
if( ! empty( $dep_total ) )
return $product_price . '<br /><small class="deposit_label">' . sprintf( __( 'statiegeld totaal %s' ), wc_price( $dep_total ) ) . '</small>';
return $product_price;
}
If someone has an idea I am very grateful because I can’t figure it out.
2
Answers
I changed my whole code to this and this is working. This wil show a deposit in the cart summary under the price and a total deposit if it are more items under the subtotal.
Also in the cart total summary it is displaying the depost total of all items and in checkout too.
There are some errors with the arguments of both functions.
Try this:
I have tested the code and it works fine. Here is the result: