【问题标题】:Get the value of the highest price in woocommerce cart获取woocommerce购物车中最高价格的价值
【发布时间】:2018-05-05 16:41:40
【问题描述】:

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

感谢您的帮助。

【问题讨论】:

    标签: php wordpress woocommerce cart fee


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢您的承诺,这太棒了。到目前为止,这看起来不错,但是根据用户的选择,有 4 种不同的(保险)案例,我认为这样的费用都被视为相同。当用户选择 $value == 'ja-ohne-sb' 时,将应用从 26 到 49 的费用,但如果他选择其他保险,如 ''ja-ohne-sb-jahr' 或 'ja-mit-sb'收费不同。我已经编辑了我的条目帖子,向您展示了完整的代码。我也试过自己弄清楚,但我被你的代码困在 elseif 案例中。你有没有可能再次帮助我?
    • @JohnnyKermode 好的,试试看……我已经更新了我的代码。
    • 你摇滚!非常感谢你!我只需要修正 2-3 个错别字('ja-ohne-sb-jahr' 是两次),它就像一个魅力! PS:我已经通过您的联系表向您发送了一条消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 2013-02-25
    • 2017-04-06
    • 2020-03-11
    • 2016-04-04
    • 1970-01-01
    相关资源
    最近更新 更多