woocommerce offtopic - How do I hide 'out of stock' products in the admin 'product' page?

admin2025-01-07  3

I have a large number of products that are out of stock. They have a status of 'out of stock' and a quatity of 0.

I would like these to be hidden from the admin 'products page'.

They do not appear in the shop. This is working correctly. I want them to be hidden from the admin pages.

How can I do this?

I am happy to write a piece of code to do this. What function is used to generate the list for the product page?

Thanks, George

I have a large number of products that are out of stock. They have a status of 'out of stock' and a quatity of 0.

I would like these to be hidden from the admin 'products page'.

They do not appear in the shop. This is working correctly. I want them to be hidden from the admin pages.

How can I do this?

I am happy to write a piece of code to do this. What function is used to generate the list for the product page?

Thanks, George

Share Improve this question asked Jul 24, 2017 at 10:19 gck303gck303 111 bronze badge 2
  • 1 How do you get to edit them if they are hidden, you will surely need a toggle so you can see them or hide them. – Chris Pink Commented Jul 24, 2017 at 14:10
  • @ChrisPink I think is a key question... how do you go back and re-add stock to these products if they’re now hidden? – Tony Djukic Commented Apr 10, 2020 at 20:36
Add a comment  | 

1 Answer 1

Reset to default 0

You can do that by adding the code below on your theme functions php file.

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' );

}

This code was tested on the font end but it can easily be adapted to the back end

More info can be found here on this link https://iconicwp.com/hide-stock-products-woocommerce-catalog-pages/

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736257705a458.html

最新回复(0)