【问题标题】:Change "No shipping method has been selected.…" from WooCommerce checkout notice从 WooCommerce 结帐通知中更改“未选择运输方式……”
【发布时间】:2021-02-04 15:34:43
【问题描述】:

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

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

我通过 sn-ps 插件使用了以下功能,我可以确认它会更改购物车中的消息并在运输标签下方结帐。

但是,如果有人随后尝试结帐,他们会在屏幕顶部收到一条红色的 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.';
}

【问题讨论】:

    标签: php wordpress woocommerce checkout gettext


    【解决方案1】:

    此文本位于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 文件中。经过测试并且可以工作。

    【讨论】:

    • 您好,感谢您的回答。这被放置为一个新的 sn-p 并创建一个网站范围的内部服务器错误。
    • @AlexBanks 抱歉刚刚测试了代码,没有问题,没有错误或相关问题。所以你的问题与其他事情有关。
    • 谢谢,工作完美。
    【解决方案2】:

    如果你愿意,你可以使用像 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-28
      • 2020-05-20
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-04
      • 2022-08-24
      相关资源
      最近更新 更多