Skip to content
Advertisement

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?

Advertisement

Answer

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 = '';
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement