skip to Main Content

I’ve been trying to get rid of our exact stock numbers on product pages on our wordpress/woocommerce website. Ideally it shows In stock, Only 1 or 2 left in stock when theres only 1 or 2 available, or just sold out. I’ve tried many of the code snippets found here on stack overflow but none of them seem to change anything. Also the standard woocommerce settings at Products > Inventory to don’t show any stock numbers doesn’t do anything. On the front end it keeps showing Availability: Exact stock amount. Can anyone help me out?

Adding these kind of snippets to my child theme’s functions.php or even in a code snippet plugin doesn’t seem to do anything for me:

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'] = __('Available!', 'woocommerce');
    }
    // Change Out of Stock Text
    if ( ! $_product->is_in_stock() ) {
        $availability['availability'] = __('Sold Out', 'woocommerce');
    }
    return $availability;
}

2

Answers


  1. A better way is to use woocommerce_get_availability_text instead of woocommerce_get_availability. This filters only the availability text and is called after woocommerce_get_availability.

    add_filter( 'woocommerce_get_availability_text', 'set_custom_availability_text', 10, 2 );
    function set_custom_availability_text( $availability, $product ) {
        if ( $product->is_in_stock() ) {
            $availability = __( 'Available!', 'woocommerce' );
        } elseif ( ! $product->is_in_stock() ) {
            $availability = __( 'Out of stock', 'woocommerce' );
        }
        return $availability;
    }
    

    If the above code doesn’t do anything, test if the filter is being called by using the die() function. This will stop the execution of any code that comes after it and prints whatever string you put in between the parentheses:

    add_filter( 'woocommerce_get_availability_text', 'set_custom_availability_text_test', 10, 2 );
    function set_custom_availability_text_test( $availability, $product ) {
        die('Filter is called');
    }
    

    So if the filter is called correctly your page should render up to the availabitly text and display ‘Filter is called’ where the availabilty text should be. If nothing happens this means the filter isn’t called. So then check if your functions.php is being loaded, or use a plugin like Code Snippets.

    Login or Signup to reply.
  2. This snippet help you to change text for all product or particular one:

      <?php
        add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability',1,2);
        function wcs_custom_get_availability($availability,$_product ) {
           global $product;
            switch($_product->slug){
                // Enter product slug for case
                case "test-1":
                    // text for in stock mode 
                    if($_product->manage_stock = true){
                            $availability['availability'] = str_replace($availability['availability'],"Enter your custom text here $_product->stock_quantity",$availability['availability']);
                    }
                    // text for out of stock mode 
                    if ( ! $_product->is_in_stock() ) {
                             $availability['availability'] = __('Enter your custom text here', 'woocommerce');
                    }
        
                    break;
                    
                case "test2":
                    if($_product->manage_stock = true){
                            $availability['availability'] = str_replace($availability['availability'],"Enter your custom text here $_product->stock_quantity",$availability['availability']);
                    }
                    if ( ! $_product->is_in_stock() ) {
                             $availability['availability'] = __(' Enter your custom text here', 'woocommerce');
                    }
                    break;
                    
                case "test3":
                    if($_product->manage_stock = true){
                            $availability['availability'] = str_replace($availability['availability'],"Enter your custom text here $_product->stock_quantity نفر",$availability['availability']);
                    }
                    if ( ! $_product->is_in_stock() ) {
                             $availability['availability'] = __(' Enter your custom text here', 'woocommerce');
                    }
                    break;
            }
        
        
            return $availability;
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search