不同的自定义标签添加到Woocommerce送货方式

问题描述:

在WooCommerce,我想“航运估计”添加到我的送货方法(它们都是扁平率型),所以它看起来是这样的: Image 不同的自定义标签添加到Woocommerce送货方式

只有所有的估计日期不同...

我的问题是,我似乎无法定位特定的实例。我只能选择整个方法(扁率),我检查了我的方法实例ID的,因为这些都是唯一的:

screenshot

但只有当我把0在PHP中切换方法的情况下工作。 2,3,4,5,7不起作用。

这里是我的代码:

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

代码明显地导致了我所有的运输方式都是一样的估计。

这里是做什么你期望(你应该只需要更改文本,让您的正确的标签)的正确方法:

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 rate ID 
    $method_id = $method->method_id; // The method slug 
    $instance_id = str_replace($method_id.':', '', $rate_id); // The instance ID 

    // Continue only if it is "flat rate" 
    if($method_id != 'flat_rate') return $label; 

    switch ($instance_id) { 
     case '3': 
      $txt = __('Est delivery: 2-5 days'); // <= text to customize 
      break; 
     case '4': 
      $txt = __('Est delivery: 1 day'); // <= text to customize 
      break; 
     case '5': 
      $txt = __('Est delivery: 2-3 days'); // <= text to customize 
      break; 
     // for case '2' and others 'flat rates' (in case of) 
     default: 
      $txt = __('Est delivery: 2-400 days'); 
    } 
    return $label . '<br /><small>' . $txt . '</small>'; 
} 

代码放在的function.php文件您活跃的儿童主题(或主题)或任何插件文件。

经测试和工程