我的 woocommerce 项目中有一些自定义计算,需要在我的购物车中获取最昂贵的产品以应用正确的(保险)费用。
到目前为止,它正在使用此代码:
/**
* 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 );
}
}
}
但它最终需要总购物车价格。我希望不同的保险费用以最昂贵的产品价格为导向。
有什么想法可以让我获得所需的 $total 的价值吗?
谢谢你的帮助。