根据Woocommerce中的送货国家/地区显示送货日期范围

问题描述 投票:2回答:1

在Woocommerce中,我正在尝试在购物车和结帐页面上添加估计的交货日范围。 我设置了2个运输区:德国和其他欧洲国家(德国境外)称为“DHL欧洲”。我需要为德国航运国家显示与其他欧洲航运国家不同的交货日范围:

  • 德国航运国家将展示“Lieferzeit 3-5 Werktage”(当没有运费时)。
  • 其他欧洲航运国家将展出“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_shippingflat_rate运输方式,但不适用于欧洲交货(德国境外)。

我究竟做错了什么? 如何为欧洲国家(德国以外)显示正确的日期范围?

php wordpress woocommerce country shipping-method
1个回答
1
投票

您并不需要定位您的送货方式,而是客户送货国家/地区:

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文件中。经过测试和工作。

© www.soinside.com 2019 - 2024. All rights reserved.