问题描述
在 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:
只有所有的估计日期都不一样……
Only all the estimated dates are different…
我的问题是我似乎无法针对特定实例.我只能选择整个方法(统一费率),我检查了我的方法实例 ID,因为它们是唯一的:
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:
但它仅在我将 0
作为案例放入 php switch 方法时才有效.2、3、4、5、7 不起作用.
But it only works when I put 0
as a case in php switch method. 2,3,4,5,7 do not work.
这是我的代码:
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.
谢谢!
我正在使用这个:
案例'flat_rate':$label .= 'Lieferzeit: 2-3 Tage flat';休息;案例free_shipping":$label .= 'Lieferzeit: 2-3 Tage free';休息;案例'international_delivery':$label .= 'Lieferzeit: 4-5 Tage Inter';休息;默认:$label .= 'Lieferzeit: 2-3 Tage 默认';
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';
international_delivery 未显示Lieferzeit: 4-5 Tage Inter".我想我不需要将它称为international_delivery",而是其他的东西.我试过flat_rate7"也不起作用.
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.
我为德国设置了 2 个航运区,另一个称为 Euro,包含欧洲其他地区
I have set 2 shipping Zones 1 for Germany and the other is called Euro and contains the rest of european counties
推荐答案
这是实现您期望的正确方法(您应该只需要更改文本以获得正确的标签):
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>';
}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
经过测试并有效
这篇关于向 Woocommerce 运输方式添加不同的自定义标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!