在 Woocommerce 中根据发货国家/地区显示交货日期范围 [英] Display a delivery day range based on shipping country in Woocommerce
问题描述
在 Woocommerce 中,我尝试在购物车和结帐页面上添加预计的交货日期范围.
我设置了 2 个送货区域:德国和其他欧洲国家(德国以外),称为DHL 欧洲".我需要为德国送货国家/地区显示与其他欧洲送货国家/地区不同的送货日期范围:
In Woocommerce, I'm trying to add an estimated delivery day range on cart and checkout pages.
I have set 2 shipping Zones: Germany and other European countries (outside Germany) called "DHL Europe". I need to display a different delivery day range for Germany shipping country than other European shipping countries:
- 德国发货国家/地区将显示Lieferzeit 3-5 Werktage"(并且在没有运费时).
- 其他欧洲运输国家/地区将显示Lieferzeit 5-7 Werktage"
- Germany shipping country will display "Lieferzeit 3-5 Werktage" (and when there is no shipping costs).
- Other European shipping countries will display "Lieferzeit 5-7 Werktage"
我的代码尝试:
function sv_shipping_method_estimate_label( $label, $method ) {
$label .= '<br /><small class="subtotal-tax">';
switch ( $method->method_id ) {
case 'flat_rate':
$label .= 'Lieferzeit 3-5 Werktage';
break;
case 'free_shipping':
$label .= 'Lieferzeit 3-5 Werktage';
break;
case 'international_delivery':
$label .= 'Lieferzeit 5-7 Werktage';
}
$label .= '</small>';
return $label;
}
add_filter( 'woocommerce_cart_shipping_method_full_label', 'sv_shipping_method_estimate_label', 10, 2 );
它适用于 free_shipping
和 flat_rate
运输方式,但不适用于欧洲交付(德国以外).
It works with the free_shipping
and flat_rate
shipping methods, but not for European deliveries (outside Germany).
我做错了什么?
如何为欧洲国家(德国以外)显示正确的日期范围?
What am I doing wrong?
How to display the correct date range for European countries (outside Germany)?
推荐答案
您实际上不需要针对您的运输方式,而是针对客户的运输国家/地区:
You don't really need to target your shipping methods, but instead customer shipping country:
add_filter( 'woocommerce_cart_shipping_method_full_label', 'cart_shipping_method_full_label_filter', 10, 2 );
function cart_shipping_method_full_label_filter( $label, $method ) {
// The targeted country code
$targeted_country_code = 'DE';
if( WC()->customer->get_shipping_country() !== $targeted_country_code ){
$days_range = '5-7'; // International
} else {
$days_range = '3-5'; // Germany
}
return $label . '<br /><small class="subtotal-tax">' . sprintf( __("Lieferzeit %s Werktage"), $days_range ) . '</small>';
}
代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.
Code goes on function.php file of your active child theme (or active theme). Tested and works.
这篇关于在 Woocommerce 中根据发货国家/地区显示交货日期范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!