Display a delivery day range based on shipping country in Woocommerce

末鹿安然 提交于 2019-12-23 01:13:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!