• Resolved kiyik

    (@kiyik)


    Hello, sorry for my bad English.

    I want to hide out of stock items from the catalog and I have found default Woocommerce option for this (WooCommerce → Settings → Products, Out of stock visibility?checkbox). But if I use this option, products really hide from catalog (this is what I need), but besides out of stock variations hide from single product page.

    Is there a way to hide out of stock products from the catalog, but not hide out of stock variations from the single product page?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Add the following code to your theme’s functions.php file or in a custom plugin:

    // Display out-of-stock variations on single product page
    add_filter('woocommerce_available_variation', 'show_out_of_stock_variations_single_product', 10, 3);
    
    function show_out_of_stock_variations_single_product($variation_data, $product, $variation) {
        $variation_data['is_visible'] = true;
        return $variation_data;
    }
    
    // Hide out-of-stock products from the catalog
    add_action('woocommerce_product_query', 'hide_out_of_stock_products_catalog');
    
    function hide_out_of_stock_products_catalog($q) {
        if (!$q->is_main_query() || is_admin()) {
            return;
        }
    
        $q->set('meta_query', array(
            array(
                'key' => '_stock_status',
                'value' => 'instock',
                'compare' => '=',
            ),
        ));
    }
    

    Save your changes and check the results:

    Out-of-stock variations should now be visible on the single product page.
    Out-of-stock products will be hidden from the catalog.

    Thanks
    Ahir Hemant

    Hi @kiyik

    Thanks for reaching out!

    Is there a way to hide out of stock products from the catalog, but not hide out of stock variations from the single product page?

    This is something that can’t be done within the core functionalities of WooCommerce. You would need some custom code to achieve this.

    I did some research and found this article could be a good starting point: Disable Out of Stock Variations in WooCommerce

    Hope this helps!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Hide out of stock items from the shop page’ is closed to new replies.