Search code examples
phpwordpresswoocommercehook-woocommerceproduct-variations

Change "out of stock" message in WooCommerce variable products


We have a WooCommerce store with lots of variable products, and have added custom texts for stock statuses. I have managed to change all of the various stock statuses, when selecting attributes, but not the status when there are no variables at all. Meaning the "This product is currently out of stock and unavailable" text.

https://sykleriet.no/produkt/specialized-turbo-levo-comp-alloy/

I have added a snippet to control these statues; In Stock, Out of Stock, and Backorder, thanks to the help from here, and I have also tried various other fixes to change this last one only as a stand alone snippet, with the one we use for the others - deactivated. For now, it seems that I can not change this text.

Would it be possible to add a string to this code, or create a separate code for this purpose only?

This is the code we use for the others:

add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
    
    // Change In Stock Text
    if ( $_product->is_in_stock() ) {
        $availability['availability'] = __('På lager', 'woocommerce');
    }
    // Change Out of Stock Text
    if ( ! $_product->is_in_stock() ) {
        $availability['availability'] = __('Utsolgt', 'woocommerce');
        
    }
    // Change Backorder Text
    if ( $_product->is_on_backorder() ) {
        $availability['availability'] = __('Bestillingsvare 10-15 dager', 'woocommerce');
    }
    return $availability;
}

Solution

  • The code that you need to customize "This product is currently out of stock and unavailable" text is the following:

    add_filter('woocommerce_out_of_stock_message', 'custom_wc_out_of_stock_message');
    function custom_wc_out_of_stock_message() {
        return __('The product is out of stock and unavailable for instance. Contact us to get the upcoming availiability.', 'woocommerce');
    }
    

    The code goes on functions.php file of your active theme (or in a plugin).

    The woocommerce_out_of_stock_message filter hook location in WooCommerce code