I want to display 'Out of Stock' items on one category archive page: 'Sold Items'.
All other categories need to hide its Out of Stock items.
'Hide out of stock items from the catalog', within the WC settings is not ticked.
I have the below code, which successfully hides out of stock items, but I cannot get the has_term() function to work correctly and filter out the 'Sold Items' page.
I believe it may be because I'm hooking into 'pre_get_posts' and perhaps this fires before the 'Sold Items' term has been added.
Which is the best action to hook into? Or do I need to split this over two hooks?
add_action( 'pre_get_posts', 'VG_hide_out_of_stock_products' );
function VG_hide_out_of_stock_products( $q ) {
if ( ! $q->is_main_query() || is_admin() ) {
return;
}
global $post;
if ( !has_term( 'Sold Items', 'product_cat', $post->ID ) ) {
if ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) ) {
$tax_query = (array) $q->get('tax_query');
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'term_taxonomy_id',
'terms' => array( $outofstock_term->term_taxonomy_id ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
}
}