electeon
electeon

Reputation: 43

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

It doesn't make sense to me to have the '(can be backordered)' text on a Woocommerce product page if the product is in stock as that confuses people because, after all, it's in stock!

I have found code to change the message if it is backordered but not how to remove it if the product is in stock and I have searched on the net for hours.

Can anyone provide me with the code required in the functions.php file or elsewhere to change it globally?

Upvotes: 3

Views: 6180

Answers (5)

Tom F.
Tom F.

Reputation: 153

If you wonder how to make this work in any language you should use preg_replace with regular expression instead of str_replace.

Like this:

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;

}

This will remove everything within those two parentheses including the parentheses themselves. Useful especially for people using WPML and other multilingual plugins.

Upvotes: 1

all you have to do is to go here

woocommerce/includes/wc-formatting-functions.php

and at line 1197

change

if ( $product->backorders_allowed() && $product->backorders_require_notification() ) { $display .= ' ' . __( '(can be backordered)', 'woocommerce' ); }

to

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

hope that helps! :)

Upvotes: -1

Stieranka
Stieranka

Reputation: 82

You forgot the translation string. It will be:

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

Upvotes: -1

Ciprian Ilinca
Ciprian Ilinca

Reputation: 21

If you want to show the text "can be backordered", on products that have inventory quantity equal or less than value set on "Low stock threshold" :

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;
}

Upvotes: 2

LoicTheAztec
LoicTheAztec

Reputation: 254388

Updated

The following code will remove "(can be backordered)" text from the product availability text, when product is in stock and backorders are allowed (with a customer notification):

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;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Upvotes: 7

Related Questions