Получить значение самой высокой цены в корзине woocommerce - PullRequest
0 голосов
/ 05 мая 2018

У меня есть несколько пользовательских расчетов в моем проекте 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 );

            }
        }
    }

Но в итоге требуется полная стоимость корзины. Я хочу, чтобы различные страховые взносы были ориентированы на самую дорогую цену продукта.

Какие-нибудь идеи, как я мог бы получить сумму в сумме, что мне нужно?

Спасибо за любую помощь.

1 Ответ

0 голосов
/ 05 мая 2018

Если я правильно понял, попробуйте следующее, где вы получите самую высокую цену товара в корзине и будет применена соответствующая комиссия:

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'] );
...