问题
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:
- 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"
My code attempt:
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 );
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)?
回答1:
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>';
}
Code goes on function.php file of your active child theme (or active theme). Tested and works.
来源:https://stackoverflow.com/questions/55287041/display-a-delivery-day-range-based-on-shipping-country-in-woocommerce