I have some custom calculation in my woocommerce project and need to get the most expensive product in my cart to apply the correct (insurance) fee.
It's working so far with this code:
/**
* use woocommerce fee api
* calculate insurances and assign to checkout
*/
public function add_cart_versicherung_fee() {
global $woocommerce;
session_start();
if ( isset( $_SESSION['versicherung_one'] ) ) {
foreach ( $_SESSION['versicherung_one'] as $key => $value ) {
if ( $value == 'ja-ohne-sb' ) {
$fee = [];
$prices = [
300 => 26,
400 => 29,
500 => 36,
600 => 39,
800 => 44,
1000 => 49
];
$total = WC()->cart->cart_contents_total;
foreach ( $prices as $amount => $fee_value ) {
if ( $total < $amount ) {
array_push( $fee, $amount );
}
}
$current_fee = $prices[ $fee[0] ];
WC()->cart->add_fee( '1. Teilnehmer: Versicherung (ohne Selbstbeteiligung)', $current_fee );
} elseif ( $value == 'ja-ohne-sb-jahr' ) {
$fee = [];
$prices = [
750 => 49,
1000 => 59
];
$total = WC()->cart->cart_contents_total;
foreach ( $prices as $amount => $fee_value ) {
if ( $total < $amount ) {
array_push( $fee, $amount );
}
}
$current_fee = $prices[ $fee[0] ];
WC()->cart->add_fee( '1. Teilnehmer: Jahresversicherung (ohne Selbstbeteiligung)', $current_fee );
} elseif ( $value == 'ja-mit-sb' ) {
$fee = [];
$prices = [
300 => 14,
400 => 18,
500 => 22,
600 => 28,
800 => 34,
1000 => 39
];
$total = WC()->cart->cart_contents_total;
foreach ( $prices as $amount => $fee_value ) {
if ( $total < $amount ) {
array_push( $fee, $amount );
}
}
$current_fee = $prices[ $fee[0] ];
WC()->cart->add_fee( '1. Teilnehmer: Versicherung (mit Selbstbeteiligung)', $current_fee );
} elseif ( $value == 'ja-mit-sb-jahr' ) {
$fee = [];
$prices = [
750 => 29,
1000 => 34
];
$total = WC()->cart->cart_contents_total;
foreach ( $prices as $amount => $fee_value ) {
if ( $total < $amount ) {
array_push( $fee, $amount );
}
}
$current_fee = $prices[ $fee[0] ];
WC()->cart->add_fee( '1. Teilnehmer: Jahresversicherung (mit Selbstbeteiligung) ', $current_fee );
} elseif ( $value == 'nein' ) {
WC()->cart->add_fee( '1. Teilnehmer: Keine Versicherung', 0 );
}
}
}
But it takes the total cart price at the end. I want that the different insurances fees to orientate towards the most expensive product price.
Any ideas how I could get the value for $total of what I need?
Thanks for any help.