问题描述
在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.
谢谢!
我使用以下方法:
case'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个运输区域设置为德国的1个区域,另一个称为欧元区域,其中包含欧洲其他县区
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送货方式添加不同的自定义标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!