从 WooCommerce 结帐通知中更改“未选择运输方式……”

问题描述

当没有可用的送货方式时,我试图通过重命名下面的认消息来解决问题,无论是通过基于规则的插件设置还是在 WooCommerce 中根本没有设置任何方法

No shipping method has been selected. Please double check your address,or contact us if you need any help.

我已经通过代码插件使用了以下功能,我可以确认它更改了购物车中的消息并在发货标签下结帐。

但是,如果有人随后尝试结帐,他们会在屏幕顶部收到一条红色的 WooCommerce 错误消息,这仍然是上面的认消息。

我该如何更改此错误消息?我想将其更改为与我下面的代码显示内容不同的内容,以便我可以灵活地在购物车总计框中放置一条短消息,以及一条更长的信息性消息,说明为什么没有可用的方法

我的功能

add_filter( 'woocommerce_cart_no_shipping_available_html','no_shipping_available_html' );
add_filter( 'woocommerce_no_shipping_available_html','no_shipping_available_html' );

function no_shipping_available_html( $message ) {
    $country = WC()->customer->get_shipping_country();
    if ( !empty( $country ) ) {
        $all_countries  = WC()->countries->get_countries(); 
        return sprintf( 'Orders delivered into the EU are limited to €150 (inc shipping) or a max weight of 2kg. The items in you cart exceed this limit. Please remove an item or reduce the quantity required to bring the order value/weight within the permitted values.',$all_countries[ $country ] );
    }
    return 'Orders delivered into the EU are limited to €150 (inc shipping) or a max weight of 2kg. The items in you cart exceed this limit. Please remove an item or reduce the quantity required to bring the order value/weight within the permitted values.';
}

Error message at the top which remains unaffected by the function provided.

解决方法

此文本位于 WC_Checkoutinside validate_checkout() method。没有过滤器可以对其进行更改,但您可以使用 WordPress gettext 过滤器进行更改,如下所示:

add_filter(  'gettext','change_checkout_no_shipping_method_text',10,3 );
function change_checkout_no_shipping_method_text( $translated_text,$text,$domain ) {
    if ( is_checkout() && ! is_wc_endpoint_url() ) {
        $original_text = 'No shipping method has been selected. Please double check your address,or contact us if you need any help.';
        $new_text      = 'Here your own text replacement.';
        
        if ( $text === $original_text ) {
            $translated_text = $new_text;
        }
    }
    return $translated_text;
}

代码位于活动子主题(或活动主题)的functions.php 文件中。经测试有效。

,

如果你愿意,你可以使用像 Loco translate 这样的插件,这样可以更容易地完成你的工作..但是你可以试试这个代码,类似于functions.php中的@LoicTheAztec(在你的主题或子主题文件中)

    add_filter( 'gettext','ds_translate_woocommerce_strings',999,3 );
function ds_translate_woocommerce_strings( $translated,$untranslated,$domain ) {
    if ( ! is_admin() ) {
        switch ($translated) {
        case 'the message you want to be translated exactly as you see it in WP':
                $translated = 'your new message';
                break;
        }
    }
    return $translated;
}