Php 获取商业购物车中最高价格的值

Php 获取商业购物车中最高价格的值,php,wordpress,woocommerce,cart,fee,Php,Wordpress,Woocommerce,Cart,Fee,我在我的woocommerce项目中有一些自定义计算,需要在我的购物车中获得最昂贵的产品以申请正确的(保险)费用 到目前为止,它正在使用以下代码: /** * use woocommerce fee api * calculate insurances and assign to checkout */ public function add_cart_versicherung_fee() { global $woocommerce; session_start();

我在我的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 );

            }
        }
    }
/**
*使用商业收费api
*计算保险并分配到结帐
*/
公共功能添加购物车版本费用(){
全球商业;
会话_start();
如果(isset($_会话['versicherung_one'])){
foreach($\会话['versicherung\u one']作为$key=>$value){
如果($value='ja ohne sb'){
$fee=[];
$prices=[
300  => 26,
400  => 29,
500  => 36,
600  => 39,
800  => 44,
1000 => 49
];
$total=WC()->cart->cart\u contents\u total;
foreach(价格为$amount=>$fee\u值){
如果($总额<$金额){
阵列推送($fee,$amount);
}
}
$current_fee=$prices[$fee[0]];
WC()->cart->add_fee('1.Teilnehmer:Versicherung(ohne Selbstbeteiligung)',$current_fee);
}其他($value='ja ohne sb jahr'){
$fee=[];
$prices=[
750  => 49,
1000 => 59
];
$total=WC()->cart->cart\u contents\u total;
foreach(价格为$amount=>$fee\u值){
如果($总额<$金额){
阵列推送($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\u contents\u total;
foreach(价格为$amount=>$fee\u值){
如果($总额<$金额){
阵列推送($fee,$amount);
}
}
$current_fee=$prices[$fee[0]];
WC()->cart->add_fee('1.Teilnehmer:Versicherung(mit Selbstbeteiligung)',$current_fee);
}elseif($value='jamitsbjahr'){
$fee=[];
$prices=[
750  => 29,
1000 => 34
];
$total=WC()->cart->cart\u contents\u total;
foreach(价格为$amount=>$fee\u值){
如果($总额<$金额){
阵列推送($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;
            }
        }
    }
}
1) 产品含税价格:

$product_prices[] = wc_get_price_including_tax( $cart_item['data'] );
$product_prices[] = wc_get_price_excluding_tax( $cart_item['data'] );
2) 产品价格(不含税):

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

如果我理解正确,请尝试以下方法,您将在购物车中获得最高的产品价格,并支付相应的费用:

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;
            }
        }
    }
}
1) 产品含税价格:

$product_prices[] = wc_get_price_including_tax( $cart_item['data'] );
$product_prices[] = wc_get_price_excluding_tax( $cart_item['data'] );
2) 产品价格(不含税):

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

非常感谢您的承诺,这太棒了。到目前为止,这看起来不错,但有4种不同的(保险)案例取决于用户选择,我认为这样的话,费用都被视为相同的。当用户选择$value='ja-ohne-sb'时,将应用26到49的费用,但如果他选择另一种保险,如'ja-ohne-sb-jahr'或'ja-mit-sb',费用是不同的。我编辑了我的入门文章,向您展示了完整的代码。我还尝试了我自己想弄明白,但是我被你的代码困在了其他的箱子里。你能再帮我一次吗?@JohnnyKermode好的,试试看……我已经更新了我的代码。你太棒了!非常感谢!我只需要修复2-3个拼写错误('ja-ohne-sb-jahr'是两次)我已经通过你的联系方式给你发了一条信息。非常感谢你的承诺,这太棒了。到目前为止这看起来不错,但是有4种不同的(保险)案例取决于用户选择,我认为这样的话,费用都被视为相同的。当用户选择$value='ja-ohne-sb'时,将应用26到49的费用,但如果他选择另一种保险,如'ja-ohne-sb-jahr'或'ja-mit-sb',费用是不同的。我编辑了我的入门文章,向您展示了完整的代码。我还尝试了我自己想弄明白,但是我被你的代码困在了其他的箱子里。你能再帮我一次吗?@JohnnyKermode好的,试试看……我已经更新了我的代码。你太棒了!非常感谢!我只需要修复2-3个拼写错误('ja-ohne-sb-jahr'是两次)这就像一个魔咒!PS:我已经通过你的联系方式给你发了一条信息。