从 Woocommerce 中的产品页面库存显示中删除 "can be backordered"

Remove "can be backordered" from product page stock display in Woocommerce

如果产品有库存,在 Woocommerce 产品页面上显示“(可以延期交货)”文本对我来说没有意义,因为这会让人们感到困惑,因为毕竟它有库存!

我找到了代码,可以在延期交货时更改消息,但没有找到如何在产品有库存时将其删除的代码,我已经在网上搜索了几个小时。

任何人都可以向我提供 functions.php 文件或其他地方所需的代码以全局更改它吗?

已更新

以下代码将从产品可用性文本中删除“(可以延期交货)”文本,当产品有库存并且允许延期交货时(通过客户通知):

add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2 );
function filter_product_availability_text( $availability, $product ) {

    if( $product->backorders_require_notification() ) {
        $availability = str_replace('(can be backordered)', '', $availability);
    }
    return $availability;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

如果要在库存数量等于或小于 "Low stock threshold" 上设置的值的产品上显示文本 "can be backordered":

add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2 );
function filter_product_availability_text( $availability, $product ) {

if( $product->get_stock_quantity () > get_option( 'woocommerce_notify_low_stock_amount' )) {
    $availability = str_replace('(can be backordered)', '', $availability);
 }
 return $availability;
}

您忘记了翻译字符串。它将是:

$availability = str_replace(__( '(can be backordered)', 'woocommerce' ), '', $availability);

你只需到这里

woocommerce/includes/wc-formatting-functions.php

并在第 1197 行

改变

if ( $product->backorders_allowed() && $product->backorders_require_notification() ) { $显示 .= ' ' . __( '(可以延期交货)', 'woocommerce' ); }

if ( $product->backorders_allowed() && $product->backorders_require_notification() ) { $显示 .= ' ' . __( '', 'woocommerce' ); }

希望对您有所帮助! :)

如果您想知道如何在任何语言中实现此功能,您应该使用带有正则表达式的 preg_replace 而不是 str_replace

像这样:

add_filter( 'woocommerce_get_availability_text', 'filter_product_availability_text', 10, 2 );
function filter_product_availability_text( $availability, $product ) {

if( $product->backorders_require_notification() ) {
    $availability = preg_replace( '/\(.*\)/', '', $availability );
}
return $availability;

}

这将删除这两个括号内的所有内容,包括括号本身。对使用 WPML 和其他多语言插件的人特别有用。