0

I have a site which needs to display the available stock left for each product, which is fine. However some products are print on demand (i.e. infinite stock) so I have these loaded with 99999999 in stock. Obviously I don't want this stock level to show on the front end of the site as there isn't really any physical stock.

I've found the below snippet - can this be edited to say, for example, if the current stock is more than 1000 then hide the stock message?

function my_wc_hide_in_stock_message( $html, $text, $product ) {
    $availability = $product->get_availability();

    if ( isset( $availability['class'] ) && 'in-stock' === $availability['class'] ) {
        return '';
    }

    return $html;
}

add_filter( 'woocommerce_stock_html', 'my_wc_hide_in_stock_message', 10, 3 );
1

Yep! You can check using the get_stock_quantity() method.

function my_wc_hide_in_stock_message( $html, $text, $product ) {
    $availability = $product->get_availability();
    $stock_qty = $product->get_stock_quantity();

    if ( isset( $availability['class'] ) && 'in-stock' === $availability['class'] && $stock_qty > 1000 ) {
        return '';
    }

    return $html;
}

add_filter( 'woocommerce_stock_html', 'my_wc_hide_in_stock_message', 10, 3 );
  • Thanks - I had to change 'in-stock' === $availability['class'] || $stock_qty > 1000 to 'in-stock' === $availability['class'] && $stock_qty > 1000 otherwise it removed the message from all in-stock products not just those with more than 1000, now it works perfectly! Thanks :) – Lyall Mar 26 at 8:16
  • @Lyall Oh yeah! You're right. I'll update the answer. – disinfor Mar 26 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.