当没有可用的运输方法(由基于规则的插件设置或 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.';
}
本文位于
WC_Checkout
类内部validate_checkout()
方法中。没有过滤器可以对其进行更改,但您可以使用 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 翻译这样的插件,这样可以更轻松地完成您的工作..但是您可以尝试此代码,类似于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;
}
您必须使用 2 个挂钩,并且运输默认消息将会更改。
function custom_shipping_message( $message ) {
return __( 'We are sorry, zip code is outside of our delivery radius.' );
}
add_filter( 'woocommerce_no_shipping_available_html', 'custom_shipping_message' );
function change_checkout_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 = 'We are sorry, zip code is outside of our delivery radius.';
if ( $text === $original_text ) {
$translated_text = $new_text;
}
}
return $translated_text;
}
add_filter( 'gettext', 'change_checkout_shipping_method_text', 10, 3 );