Skip to content
Advertisement

Display “In Stock” notice for WooCommerce variations with no Managed Stock

I need help for a particular situation. In WooCommerce, if “Manage Stock” is enabled for a simple product or variation, then a notification is being displayed in the product page => such as [this example][1]

However, if “Manage Stock” is not enabled, then there is no notification which I find it a pity because I still want to inform my customers that it’s precisely in stock even if I don’t manage the stock quantities.

I’ve found the below code. For simple products, it works without any problem. However, for variable product, this message is being displayed even before that a variation is selected. This is of course not okay, this code should be displayed only after that a variation is selected.

Can someone help me to fix this? For variable products, this message should only be displayed after that a particular variation is selected.

I’ve made a video capture to be a bit more illustrative : https://sgevcen.tinytake.com/tt/NDQzNTU2OF8xNDAyNTU2NA

function mycustom_shop_display_stock() {

    global $product;

    if ( !$product->get_manage_stock() && $product->is_in_stock() ) {
        echo '<p class="stock in-stock">In Stock</p>';
    }
}
add_action( 'woocommerce_before_add_to_cart_button', 'mycustom_shop_display_stock', 11 );


  [1]: https://i.stack.imgur.com/aFnN1.png

Advertisement

Answer

Try the following instead that should allow to display your custom stock availability only for the variations of a variable product (and also on simple products):

add_filter( 'woocommerce_get_stock_html', 'filter_wc_get_stock_html', 10, 2 );
function filter_wc_get_stock_html( $html, $product ) {
    if ( ! $product->is_type('variable') && ! $product->get_manage_stock() && $product->is_in_stock() ) {
        $html = '<p class="stock in-stock">' . __( "In Stock", "woocommerce" ) . '</p>';
    }

    return $html;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


To exclude some product categories use the following (related to your comment):

add_filter( 'woocommerce_get_stock_html', 'filter_wc_get_stock_html', 10, 2 );
function filter_wc_get_stock_html( $html, $product ) {
    // Here define the product categories to be excluded (can be term Ids, slugs or names)
    $terms_excl = array('hoodies', 'albums');

    $product_id = $product->is_type('variation') ? $product->get_parent_id() : $product->get_id();

    if ( ! $product->is_type('variable') && ! $product->get_manage_stock() && $product->is_in_stock()
    && ! has_term( $terms_excl, 'product_cat', $product_id ) ) {
        $html = '<p class="stock in-stock">' . __( "In Stock", "woocommerce" ) . '</p>';
    }

    return $html;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


Related thread: Display custom stock message if “Manage Stock” is not enabled in WooCommerce

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement