skip to Main Content

If all variations are out of stock I want to hide a product.

In WooCommerce if you click the box "hideout of stock items from the catalog" it hides specific variations that are out of stock. It looks weird when a shirt is just showing sizes Small and Large. This is not what I want.

I want to show all variations unless ALL variations are out of stock – then I want to hide the product.

2

Answers


  1. You can use the woocommerce_product_query action hook to remove a product if all variation is outofstock. code will go in your active theme functions.php file.

    function hide_product_if_all_variation_and_simple_product_is_outofstock( $q ) {
    
        global $wpdb;
    
        // Get ids of products which you want to hide
        $array_of_product_id = array();
    
        // get all variable product Ids
        $variable_products_ids = wc_get_products( array(
            'limit'  => -1,
            'status' => 'publish',
            'type'   => 'variable',
            'return' => 'ids',
        ));
    
        // Loop through variable products
        foreach( $variable_products_ids as $variable_id ) {
            $count = $wpdb->get_var( $wpdb->prepare( "
                SELECT COUNT(ID)
                FROM {$wpdb->posts} p
                INNER JOIN {$wpdb->postmeta} pm
                    ON p.ID           =  pm.post_id
                WHERE p.post_type     =  'product_variation'
                    AND p.post_status =  'publish'
                    AND p.post_parent =  %d
                    AND pm.meta_key   =  '_stock_status'
                    AND pm.meta_value != 'outofstock'
            ", $variable_id ) );
    
            $count = $count > 0 ? true : false;         
    
            if( !$count ){
                $array_of_product_id[] = $variable_id;
            }
        }
    
        // get all simple product Ids
        $simple_products_ids = wc_get_products( array(
            'limit'  => -1,
            'status' => 'publish',
            'type'   => 'simple',
            'return' => 'ids',
        ));
    
        // Loop through variable products
        foreach( $simple_products_ids as $simple_id ) {
            $count = $wpdb->get_var( $wpdb->prepare( "
                SELECT COUNT(ID)
                FROM {$wpdb->posts} p
                INNER JOIN {$wpdb->postmeta} pm
                    ON p.ID           =  pm.post_id
                WHERE p.post_type     =  'product'
                    AND p.post_status =  'publish'
                    AND p.ID =  %d
                    AND pm.meta_key   =  '_stock_status'
                    AND pm.meta_value != 'outofstock'
            ", $simple_id ) );
            
            $count = $count > 0 ? true : false;         
    
            if( !$count ){
                $array_of_product_id[] = $simple_id;
            }
        }
    
        if( !empty( $array_of_product_id ) ){
            $q->set( 'post__not_in', $array_of_product_id );
        }
    
    }
    add_action( 'woocommerce_product_query', 'hide_product_if_all_variation_and_simple_product_is_outofstock' );
    
    Login or Signup to reply.
  2. Few easy steps to Hide Out of Stock Products

    1. Go to WooCommerce -> Settings submenu in the WordPress dashboard.
    2. Click on the Products Tab > Inventory sub-tab Check the option
    3. Out Of Stock Visibility that hides the out of stock products

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search