dongtan2603 2019-01-02 15:16
浏览 27
已采纳

保持最高的统一运费和Woocommerce的本地提货

I'm trying to show the highest shipping costs in the cart. I've found a nice little snippet for that:

function only_show_most_expensive_shipping_rate( $rates, $package ) {
    $most_expensive_method = '';
    $new_rates = array();
    // Loop through shipping rates
    if ( is_array( $rates ) ) {
        foreach ( $rates as $key => $rate ) {   
         // Set variables when the rate is more expensive than the one saved
         if ( empty( $most_expensive_method ) || $rate->cost > $most_expensive_method->cost ){
            $most_expensive_method = $rate;
         }

        }
    }
    // Return the most expensive rate when possible
    if ( ! empty( $most_expensive_method ) ){
       /**  
        ** Keep local pickup if it's present.
        **/
        foreach ( $rates as $rate_id => $rate ) {
            if ('local_pickup' === $rate->method_id ) {
                $new_rates[ $rate_id ] = $rate;
                break;
            }
        }
        return array( $most_expensive_method->id => $most_expensive_method );
    }
    return $rates;
}
add_action('woocommerce_package_rates', 'only_show_most_expensive_shipping_rate', 10, 2); 

However this snippet also hides the "local pickup" shipping method.

Why does the above method doesn't work? Right now it only shows the highest shipping class/price and hide all others including the pickup method.

Is it because of the two arrays? I don't see any errors popping up.

Any help greatly appreciated!

  • 写回答

1条回答 默认 最新

  • douzhun5971 2019-01-02 21:58
    关注

    The following will keep the highest shipping Flat rate cost and the Local pickup shipping method:

    add_action('woocommerce_package_rates', 'keep_highest_flat_rate_cost', 10, 2);
    function keep_highest_flat_rate_cost( $rates, $package ) {
        $flat_rate_costs = [];
    
        // Loop through shipping methods rates
        foreach ( $rates as $key_rate => $rate ) {
            // Targeting only "Flat rate" type shipping methods
            if ( ! in_array( $rate->method_id, ['local_pickup', 'free_shipping'] ) ) {
                // Store the Rate ID keys with corresponding costs in an indexed array
                $flat_rate_costs[$key_rate] = $rate->cost;
            }
        }
        // Sorting "Flat rate" costs in DESC order
        arsort($flat_rate_costs);
    
        // Remove the highest cost from the array
        array_shift($flat_rate_costs);
    
        // Loop through remaining "Flat rate" shipping methods to remove them all
        foreach ( $flat_rate_costs as $key_rate => $cost){
            unset($rates[$key_rate]);
        }
        return $rates;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    You should need to refresh the shipping caches:
    1) First ensure that the code is already saved on your function.php file.
    2) In Shipping settings, enter in a Shipping Zone: Disable any Shipping Method and "save", then re-enable it and "save". You are done.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集