• Resolved roeljonker

    (@roeljonker)


    Hi,

    I noticed this yesterday and I don’t know when this started, but on the product page it used to say “in stock” if it was a product without inventory control and status “in stock”. But now it shows nothing! I have this problem on all our 4 different webshops. Did this chang in some update?

    What still works:
    – (still with inventory control off) set status to “out of stock” and it will show “out of stock” on the product page
    – turn on inventory control, set stock to 2 and it will show “2 in stock”

    I disabled ALL other plugins and switched to theme Storefront (and tried 2020,2019,2018,2017) with the same result.

    I found a piece of code to insert into the child theme’s functions.php to insert the text “in stock”, but this uses the inventory status of that product. And the products on our site that do use inventory control, no longer show the quantity, but this text is overwritten by the code to “in stock”. So not the solution to go for…

    Am I just missing a simple new setting?

    Regards,
    Roel

Viewing 5 replies - 1 through 5 (of 5 total)
  • linux4me2

    (@linux4me2)

    I don’t know when this happened either, but the culprit appears to be in the WooCommerce function get_availability_text():

    
    protected function get_availability_text() {
            if ( ! $this->is_in_stock() ) {
                $availability = __( 'Out of stock', 'woocommerce' );
            } elseif ( $this->managing_stock() && $this->is_on_backorder( 1 ) ) {
                $availability = $this->backorders_require_notification() ? __( 'Available on backorder', 'woocommerce' ) : '';
            } elseif ( $this->managing_stock() ) {
                $availability = wc_format_stock_for_display( $this );
            } else {
                $availability = '';
            }
            return apply_filters( 'woocommerce_get_availability_text', $availability, $this );
        }
    

    Notice how the “else” statement returns an empty string if the product is in stock and not under inventory control.

    Give the following a try and see if it works for you. I didn’t test it with inventory control, but the only thing I changed was to substitute “In Stock” for an availability with an empty string:

    
    add_filter('woocommerce_get_availability', 'custom_get_availability', 1, 2);
    function custom_get_availability($availability, $product) {
      if ($availability['availability'] == '') {
        $availability['availability'] = __('In Stock', 'woocommerce');
      }
      return $availability;
    }
    
    Thread Starter roeljonker

    (@roeljonker)

    @linux4me2 Thanks a lot! That is exactly what happens…..

    Your code is as simple as it is brilliant! It works like a charm and it doesn’t affect products with inventory control turned on (as was to be expected when you look what the code does).
    The nice thing is, that when this bug will be sorted in the future, this code won’t do any harm.

    linux4me2

    (@linux4me2)

    @roeljonker I’m glad that works for you!

    I don’t know if the WooCommerce developers would consider this a bug. I think it falls into the category of a “feature.” The code looks like they intended to leave “In Stock” out.

    It seems like adding an option in the Settings or in Appearance -> Customize -> WooCommerce to display the “In Stock” would be a nice thing to have. There is a WooCommerce Feature Request Forum that I believe is still active where you could suggest it, or maybe on their GitHub Page as an enhancement. If you do, please post the link here and I will vote for it. ??

    Glad to see this has been resolved. Since we haven’t heard back from you in a while, I’m going to mark this as resolved – if you have any further questions, you can start a new thread.

    – Joey

    I put the

    add_filter( 'woocommerce_get_availability', 'custom_get_availability', 1, 2);
      
    function custom_get_availability( $availability, $_product ) {
        //change text "In Stock' to 'SPECIAL ORDER'
        if ( $_product->is_in_stock() ) $availability['availability'] = __('AVAILABLE', 'woocommerce');
      
        //change text "Out of Stock' to 'SOLD OUT'
        if ( !$_product->is_in_stock() ) $availability['availability'] = __('SOLD OUT', 'woocommerce');
            return $availability;
        }

    in the functions.php of the child theme. But it’s not working…
    Can you help?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘“in stock” not showing’ is closed to new replies.