• Resolved sumpson

    (@sumpson)


    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:

    1. 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)
  • Hi

    // Hide out-of-stock products on category pages
    function hide_out_of_stock_products_on_category_pages( $query ) {
    if ( $query->is_main_query() && $query->is_tax( 'product_cat' ) ) {
    $query->set( 'meta_query', array(
    array(
    'key' => '_stock_status',
    'value' => 'instock',
    'compare' => '='
    )
    ) );
    }
    }
    add_action( 'pre_get_posts', 'hide_out_of_stock_products_on_category_pages' );

    This code uses the pre_get_posts action hook to modify the main query for category pages (product_cat taxonomy) and includes a meta query to filter out products with the _stock_status meta key set to ‘instock’. This will hide any out-of-stock products from being displayed on category pages.

    Remember to save the changes and clear any caching plugins or server caches for the modifications to take effect.

    • This reply was modified 1 year, 4 months ago by Kathryn Presner.
    • This reply was modified 1 year, 4 months ago by Kathryn Presner. Reason: put the function in a code block

    Hi @sumpson

    Thanks for reaching out!

    This is a bit of a complicated topic that would need some customization to address. Unfortunately, custom coding is not something we can assist with directly. However, I’ll keep this thread open for a bit to see if anyone from the community can lend a hand.

    If you have any other questions related to development or custom coding, don’t hesitate to reach out to some of the great resources we have available for support. The WooCommerce community is filled with talented open-source developers, and many of them are active on the channels listed below:

    Hope this helps!

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.