douduikai0562 2017-10-05 19:38
浏览 23
已采纳

为Woocommerce运输方式添加不同的自定义标签

In WooCommerce, I am trying to add "shipping estimates" to my shipping methods (all of them are flat rate type), so it looks like this: Image

Only all the estimated dates are different…

My problem is that I can't seem to target specific instances. I can only select entire method (flat rate), I checked for my methods instance ID's, since those are unique:

screenshot

But it only works when I put 0 as a case in php switch method. 2,3,4,5,7 do not work.

Here is my code:

function sv_shipping_method_estimate_label($label, $method) {
    $label. = '<br /><small>';
    switch ($method - > instance_id) {
        case 0:
            $label. = 'Est delivery: 2-400 days';
            break;
    }

    $label. = '</small>';
    return $label;
}
add_filter('woocommerce_cart_shipping_method_full_label', 'sv_shipping_method_estimate_label', 10, 2);

The code obviously results in all the same estimates for all my shipping methods.


thanks!

im using this:

case 'flat_rate': $label .= 'Lieferzeit: 2-3 Tage flat'; break; case 'free_shipping': $label .= 'Lieferzeit: 2-3 Tage free'; break; case 'international_delivery': $label .= 'Lieferzeit: 4-5 Tage Inter'; break; default: $label .= 'Lieferzeit: 2-3 Tage default';

the international_delivery is not displaying 'Lieferzeit: 4-5 Tage Inter'. I gues I need to refer to it not as 'international_delivery' but something else. I tried 'flat_rate7' also not working.

I have set 2 shipping Zones 1 for Germany and the other is called Euro and contains the rest of european counties

  • 写回答

1条回答 默认 最新

  • doupingpeng7567 2017-10-05 20:46
    关注

    Here is the correct way to do what you are expecting (you should only need to change the texts to get your correct labels):

    add_filter('woocommerce_cart_shipping_method_full_label', 'custom_shipping_method_label', 10, 2);
    function custom_shipping_method_label( $label, $method ){
        $rate_id = $method->id; // The Method rate ID (Method Id + ':' + Instance ID)
    
        // Continue only if it is "flat rate"
        if( $method->method_id !== 'flat_rate' ) return $label;
    
        switch ( $method->instance_id ) {
            case '3':
                $txt = __('Est delivery: 2-5 days'); // <= Additional text
                break;
            case '4':
                $txt =  __('Est delivery: 1 day'); // <= Additional text
                break;
            case '5':
                $txt =  __('Est delivery: 2-3 days'); // <= Additional text
                break;
             // for case '2' and others 'flat rates' (in case of)
            default:
                $txt =  __('Est delivery: 2-400 days'); // <= Additional text
        }
        return $label . '<br /><small>' . $txt . '</small>';
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Tested and works

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

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致