Update for Woocommerce 3
Since Woocommerce 3, product stock status is not anymore set as product meta data.
It's now handle by product_visibility
custom taxonomy under outofstock
term.
So you will need to use a Tax query instead, to hide out of stock products:
add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function action_product_query( $q, $query ) {
// Get any existing Tax query
$tax_query = $q->get( 'tax_query');
// Define an additional tax query
$tax_query = array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => array('outofstock'),
'compare' => 'NOT IN',
);
// Set the new merged tax query
$q->set( 'tax_query', $tax_query );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
You can use any WooCommerce conditional tag in an if statement to target for example specific product category or product tag archive pages.
For products containing specific meta data, you will use:
add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function action_product_query( $q, $query ) {
// Get any existing Tax query
$tax_query = $q->get( 'tax_query');
// Get any existing meta query
$meta_query = $q->get( 'meta_query');
// Define an additional tax query
$tax_query = array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => array('outofstock'),
'compare' => 'NOT IN',
);
// Define an additional meta query
$meta_query = array(
'key' => 'external_stock',
'value' => '0', // <=== Set here your desired value (if needed)
'compare' => '>', // <=== Set Here the correct compare argument (if needed)
);
// Set the new merged tax query
$q->set( 'tax_query', $tax_query );
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
Original answer:
You could try this custom function hooked in woocommerce_product_query
action hook:
add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function action_product_query( $q, $query ) {
// Get any existing meta query
$meta_query = $q->get( 'meta_query');
// Define an additional meta query
$q->set( 'meta_query', array( array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT LIKE',
) ) );
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Code is tested and works.
It will remove all "out of stock" products from shop and archives pages. But it will not hide "out of stock" variations in single product pages for variable products.
For your custom meta_key
external_stock
, you will have to add it this way:
add_action( 'woocommerce_product_query', 'action_product_query', 10, 2 );
function action_product_query( $q, $query ) {
// Get any existing meta query
$meta_query = $q->get( 'meta_query');
$meta_query = array(
'relation' => 'AND', // can be also 'OR'
array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT LIKE',
),
array(
'key' => 'external_stock',
'value' => '0', // <=== Set here your desired value (if needed)
'compare' => '>', // <=== Set Here the correct compare argument (if needed)
) );
// Set the new merged meta query
$q->set( 'meta_query', $meta_query );
}
This is untested and need to be set and tested by you
Official documentation: WordPress Class Reference WP_Query - Custom Field Parameters