2017-10-05 129 views
1

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

代碼明顯地導致了我所有的運輸方式都是一樣的估計。

回答

0

這裏是做什麼你期望(你應該只需要更改文本,讓您的正確的標籤)的正確方法:

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文件您活躍的兒童主題(或主題)或任何插件文件。

經測試和工程

相關問題