【问题标题】:How to find out the cheapest shipping method and change the shipping label in woocommerce?如何在 woocommerce 中找到最便宜的运输方式并更改运输标签?
【发布时间】:2021-12-10 22:13:22
【问题描述】:

所以这就是我需要做的:找出最便宜的运输方式并将“免费送货”方式标签更改为“免费送货”+“最便宜的方式标签”。这样客户就可以知道免费送货所使用的送货方式。

首先我想到了全局变量。然后使用了 wp_options,但我不确定 wp_options 是用户共享数据还是用户特定数据。

您对以下代码有什么好的想法或更正吗?顺便说一句,这不起作用?谢谢!

add_filter( 'woocommerce_shipping_chosen_method', 'wf_default_shipping_method', 10 );
function wf_default_shipping_method( $method ) {

    $the_cheapest_cost = 1000000;

    $packages = WC()->shipping()->get_packages()[0]['rates'];

    foreach ( array_keys( $packages ) as $key ) {
        
        if ( ( $packages[$key]->cost > 0 ) && ( $packages[$key]->cost < $the_cheapest_cost ) ) {
            $the_cheapest_cost = $packages[$key]->cost;
            $transport_label   = $packages[$key]->label;    
            $method_id         = $packages[$key]->id;
        }
    }

    if(get_option('atransport')){
        update_option('atransport', $transport_label);
    }else{
        add_option('atransport', $transport_label);
    }

    return $method_id;

}  

add_filter( 'woocommerce_package_rates', 'change_shipping_methods_label_names', 20, 2 ); 
function change_shipping_methods_label_names( $rates, $package ) {

    $transport = get_option('atransport');
    foreach( $rates as $rate_key => $rate ) {
        if ( __( 'Free Shipping', 'woocommerce' ) == $rate->label ){ 
            $rates[$rate_key]->label = __( 'FreeShipping -' . $transport, 'woocommerce' ); // New label name
        }
    }
    delete_option('atransport');
    return $rates;
    
}

【问题讨论】:

    标签: wordpress woocommerce code-snippets


    【解决方案1】:

    您可以迭代$rates 的循环并获得最便宜的方法。然后根据找到的最便宜的方法,您可以更改标签。代码将进入您的活动主题 functions.php 文件。

    add_filter( 'woocommerce_package_rates', 'hide_other_shipping_when_free_is_available', 100, 1 );
    function hide_other_shipping_when_free_is_available( $rates ) {
    
        $cheapest_method = '';
    
        // Loop through shipping rates
        if ( is_array( $rates ) ) {
            foreach ( $rates as $key => $rate ) {
                // Set variables when the rate is cheaper than the one saved
                if ( empty( $cheapest_method ) || $rate->cost < $cheapest_method->cost ) {
                    $cheapest_method = $rate;
                }
            }
        }
    
        // Return the cheapest rate when possible
        if ( ! empty( $cheapest_method ) ) {
    
            $free = array();
            foreach ( $rates as $rate_id => $rate ) {
                if ( $rate->id === $cheapest_method->id ) {
    
                    if ( __( 'Free Shipping', 'woocommerce' ) == $rate->label ){
    
                        $rates[ $rate_id ] = $rate;
                        // Here we append the labbelled saved price formated display
                        $rates[ $rate_id ]->label = 'Free Shipping '.$rates[ $rate_id ]->label;
    
                        break; // stop the loop
                    }
                }
            }
            return  $rates;
        }
    
        return  $rates;
    
    }
    

    经过测试且有效

    【讨论】:

    • 嗨,Bhautik!感谢您的回答。如果我仍想显示其他可用的运输方式怎么办?我想向用户展示如果他选择免费送货将使用哪种送货方式,但有些客户可能希望选择特定的运输公司或更快的服务...... =)
    • 是的,您可以看到其他运输方式也可用。
    猜你喜欢
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    • 2021-04-21
    • 2021-01-24
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多