Hide product stock for certain products on single product pages in WooCommerce

I try to add a option to hide the product stock info but only on certain single product pages.

We do not want to hide this info for every product. We want to have a option in the product edit view where we can select if this should be the case for that current product.

Unfortunately my code below is not fully working. No fatal error but the code does not hide the stock info for a product when the checkbox is selected.

This is what I have so far:

// Add checkbox
function action_woocommerce_hide_product_stock_info() {
    // Checkbox
    woocommerce_wp_checkbox( array( 
        'id'             => '_hide_stock_status', // Required, it's the meta_key for storing the value (is checked or not)
        'label'          => __( 'Hide product stock info', 'woocommerce' ), // Text in the editor label
        'desc_tip'       => false, // true or false, show description directly or as tooltip
        'description'    => __( 'Dont show product stock info on product page', 'woocommerce' ) // Provide something useful here
    ) );
}
add_action( 'woocommerce_product_options_inventory_product_data', 'action_woocommerce_hide_product_stock_info', 10, 0 );


// Save Field
function action_woocommerce_hide_product_stock_info_object( $product ) {
    // Isset, yes or no
    $checkbox = isset( $_POST['_hide_stock_status'] ) ? 'yes' : 'no';

    // Update meta
    $product->update_meta_data( '_hide_stock_status', $checkbox );
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_hide_product_stock_info_object', 10, 1 );


// Hide stock info on product page
function filter_woocommerce_hide_product_stock( $html, $text, $product ) {
    
    // Get meta
    $hide_product_stock_info = $product->get_meta( '_hide_stock_status' );
    
    // Compare
    if ( $hide_product_stock_info == 'yes' ) {
        
        // Hide product stock info
        if ( isset( $availability['class'] ) && 'in-stock' === $availability['class'] ) {
        
            return '';
        }   

            return $html;
        }
    }

add_filter( 'woocommerce_stock_html', 'filter_woocommerce_hide_product_stock', 10, 2 );

Any advice?

Solutions

Your code contains some mistakes

  • The woocommerce_stock_html filter is deprecated. Use woocommerce_get_stock_html instead
  • $availability is not defined
  • Too few arguments to function filter_woocommerce_hide_product_stock(), 2 passed in ... and exactly 3 expected

So replace the 3rd part of your code with:

// Hide stock info on product page
function filter_woocommerce_get_stock_html( $html, $product ) {
    // Get meta
    $hide_product_stock_info = $product->get_meta( '_hide_stock_status' );
    
    // Compare
    if ( $hide_product_stock_info == 'yes' ) {
        $html = ''; 
    }

    return $html;
}
add_filter( 'woocommerce_get_stock_html', 'filter_woocommerce_get_stock_html', 10, 2 );

Optional: not mentioned in your question but if you still want to extend the function based on $availability, you can add something like:

// Get availability
$availability = $product->get_availability();

// Condition
if ( ! empty( $availability['availability'] ) && $availability['class'] == 'in-stock' ) {
    $html = '';
}
  // Compare
if ( $hide_product_stock_info == 'yes' ) {        

I'm not quite sure how WordPress saves checkbox values, but I can imagine it would rather be 'true' or 'false' instead of 'yes'. Which means you could omit the comparison and just use:

      // Compare
if ( $hide_product_stock_info) {   

Also, I'm sure you've got some containing function around the code you've shared, but you're currently working with an additional closing accolade '}' after return $html;.

Similar questions

filter products by attribute and hide out of stock items of variable products
I want to make a filter by attribute for variable products but the problem is that the filter shows out of stock product, and I have to hide the out of stock products checked, but it didn't solve the issue, thanks in advance
Hide out of stock products only on shop archive pages in Woocommerce
I am trying to hide out of stock products only from Shop page, but keep them on the separate category page. Woocommerce settings allows only to choose either to show out of stock products or to hide them completely. How to hide out of stock products only on shop archive pages in Woocommerce?
Hide Related Products from specific products single pages in Woocommerce
Okay so I am trying to hide the 'Related Products' section on the product page, BUT only for a specific product not all products. I have found the following documentation to hide the section for all product pages: https://docs.woocommerce.com/document/remove-related-posts-output/ But as I mentioned this is how to hide the 'Related Products' section...
Exclude products with a stock lower than the low stock threshold from the shop loop
On our Shop page, we have various badges (things like "Series 1") that, when clicked, link to their respective product tag pages. However, we want a "low stock" badge as well. The problem is that we need to dynamically figure out whether something is low stock. We can't just tag it. I'm making the low stock badge link to its own page with Woocommer...
Using tax_query to filter posts to all products that are in stock and those out of stock of a particular category
Here is the current code that I have tested: Im not sure if im using the $tax_query right, I assumed by using the 'or' relation I should be getting a return of all in stock products and out of stock products of the specified category. I also have tried using 'and' as the relation with no better results.
Display custom stock status based on stock quantity in admin products list table
I have added a custom stock status "Available Soon" for WooCommerce products. I want to change this status when the product stock quantity equals to 1. I tried with a hook on the thank you page and it updated the status in database using query but when I see in WooCommmerce product dashboard there is still "In Stock" status for ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.