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

2 Answers 2

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 );
2
  • 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
    Commented Mar 26, 2020 at 8:16
  • @Lyall Oh yeah! You're right. I'll update the answer.
    – disinfor
    Commented Mar 26, 2020 at 13:45
0

You can do this with CSS.

.woocommerce .ribbons .ribbon.out-of-stock {
    display: none;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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