How to hide out of stock products on category pages
-
I’ve searched the web to find a solution for my problem but can’t find the answer which works for me. I’ve tried the following solutions:
- WooCommerce → Settings → Products. From the Products tab, select the Out of stock visibility checkbox
2. adding to themes functions.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' ); }
3. adding to themes functions.php:
add_filter( 'woocommerce_product_query_meta_query', 'shop_only_instock_products', 10, 2 ); function shop_only_instock_products( $meta_query, $query ) { // Only on shop archive pages if( is_admin() || is_search() || ( !is_shop() && !is_product_category() ) ) return $meta_query; $meta_query[] = array( 'key' => '_stock_status', 'value' => 'outofstock', 'compare' => '!=' ); return $meta_query; }
I am using Ruper theme with a child theme for customization’s. Including allot of caching / script managing stuff to speed up the site. When loading the page at first you can see at category the count at 17 then when page is loaded it jumps to 30. (that’s including sold items).
Hope someone is able to help me out here.
The page I need help with: [log in to see the link]
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘How to hide out of stock products on category pages’ is closed to new replies.