[EDIT: it actually works when adding the code in functions.php and omitting the changes in code WC files. Important: it only works when ONE attribute exists. However when th
Just adding my answer as I hunted a long time for it and none of these were working for me. no matter what $variation->is_in_stock() would ALWAYS return true.
My solution was to modify the function by changing $variation->is_in_stock() to $variation->get_stock_quantity().
$variation->get_stock_quantity() actually returns a number that you can use which is the remaining stock. so rewrite the function based on the parameters you like, say when there are 5 left you want it marked out of stock (or 0 if you want 0 to be out of stock) the function would look like so:
add_filter( 'woocommerce_variation_is_active', 'my_jazzy_function', 10, 2 );
function my_jazzy_function( $active, $variation ) {
$var_stock_count = $variation->get_stock_quantity();
// if there are 5 or less, disable the variant, could always just set to 0.
if( $var_stock_count <= 6 ) {
return false;
}
else {
return true;
}
}