您可以通过在主题函数php文件中添加以下代码来实现这一点。
add_action( \'pre_get_posts\', \'iconic_hide_out_of_stock_products\' );
function iconic_hide_out_of_stock_products( $q ) {
if ( ! $q->is_main_query() || is_admin() ) {
return;
}
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 );
}
remove_action( \'pre_get_posts\', \'iconic_hide_out_of_stock_products\' );
}
这段代码在字体端进行了测试,但很容易适应后端
更多信息可在此链接中找到https://iconicwp.com/hide-stock-products-woocommerce-catalog-pages/