获取woocommerce购物车中最高价的值

Get the value of the highest price in woocommerce cart

我的 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 的价值吗?

感谢您的帮助。

如果我理解正确,请尝试以下操作,您将获得购物车中最高的产品价格并收取相应的费用:

public function add_cart_versicherung_fee() {
    session_start();

    if ( isset( $_SESSION['versicherung_one'] ) ) {

    $fee_rates = [
        'ja-mit-sb' => [
            14 => [0, 300],
            18 => [300, 400],
            22 => [400, 500],
            28 => [500, 600],
            34 => [600, 800],
            39 => [800, 1000],
        ],
        'ja-ohne-sb' => [
            26 => [0, 300],
            29 => [300, 400],
            36 => [400, 500],
            39 => [500, 600],
            44 => [600, 800],
            49 => [800, 1000],
        ],
       'ja-mit-sb-jahr' => [
            29 => [0, 750],
            34 => [750, 1000],

        ],
        'ja-ohne-sb-jahr' => [
            49 => [0, 750],
            59 => [750, 1000],
        ],
        'nein' => 0,
    ];

    $texts = [
        'ja-ohne-sb'        => 'Versicherung (ohne Selbstbeteiligung)',
        'ja-ohne-sb-jahr'   => 'Jahresversicherung (ohne Selbstbeteiligung)',
        'ja-mit-sb'         => 'Versicherung (mit Selbstbeteiligung)',
        'ja-mit-sb-jahr'    => 'Jahresversicherung (mit Selbstbeteiligung)',
        'nein'              => 'Keine Versicherung',
    ];

        $product_prices = [];
        $fee = 0;

        // Loop Through cart items - Collect product prices
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            $product_prices[] = $cart_item['data']->get_price();
        }

        // Sorting prices DESC and keep highest price
        rsort($product_prices); // Sorting prices (Desc)
        $highest_price = reset($product_prices);

        // Loop through versicherung session array
        foreach ( $_SESSION['versicherung_one'] as $value ) {
            if ( isset( $fee_rates[$value]) ) {

                // Loop through fee rates multi array to get the fee
                foreach ( $fee_rates[$value] as $rate => $range ) {
                    if( $value == 'nein' ) break;

                    if ( $highest_price > $range[0] && $highest_price <= $range[1]) {
                        $fee = $rate;
                        break;
                    }
                }

                WC()->cart->add_fee( '1. Teilnehmer: '.$texts[$value], $fee );
                break;
            }
        }
    }
}

如果需要,对于含税或不含税的产品价格,您可以替换此行:

$product_prices[] = $cart_item['data']->get_price();

1) 产品含税价格:

$product_prices[] = wc_get_price_including_tax( $cart_item['data'] );

2) 产品不含税价格:

$product_prices[] = wc_get_price_excluding_tax( $cart_item['data'] );