Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide 'out of stock' products in Woocommerce

Under "Products" and "Inventory" I have checked the following setting: "Hide out of stock items from the catalog"

Now all sold out products are hidden in the archive/category view. So far so good.

The problem is that the hidden (out of stock) products are counted per page. So if there are 3 products that are sold out on the first page, only the ones in stock are showing (6).

It also seems that these "hidden" products still are searchable as well, and visible through the different widgets.

Any ideas how to fix this? I mean to REALLY hide products that are out of stock. Or do I need to manuallly remove them?

like image 502
janlindso Avatar asked Jun 29 '14 22:06

janlindso


People also ask

How do I hide out of stock text in WooCommerce?

To change the WooCommerce "Out of Stock" text, add the following PHP snippet: function my_woo_outofstock_text( $text ) { $text = __( 'MY CUSTOM TEXT', 'oceanwp' ); return $text; } add_filter( 'ocean_woo_outofstock_text', 'my_woo_outofstock_text', 20 ); Replace MY CUSTOM TEXT with the text you want to display.

Can you hide products on WooCommerce?

From the admin panel, go to WooCommerce > Product Visibility > Global visibility tab and select the product and category you want to hide. This will hide the product and/or category from guests and all registered customers irrespective of their role.

Why product is showing out of stock in WooCommerce?

One cause of this problem is that the stock status may be recorded as "outofstock" in the database, even while manage inventory is disabled. Show activity on this post. Good suggestion. I did try reinstalling with a fresh copy of WooCommerce with no luck (I'm going to test the 2.2 update that just came out too).

How do I limit stocks in WooCommerce?

Enter the Stock Quantity, and WooCommerce auto-manages inventory and auto-updates Stock Status as Stock, Out of Stock or On Backorder. Select whether to Allow Backorders. Low stock threshold – Enter a number upon which you are notified. Tick the Sold Individually box to limit the product to one per order.


5 Answers

You can try adding this to your theme's functions.php file:

add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() ) {


$q->set( 'meta_query', array(array(
    'key'       => '_stock_status',
    'value'     => 'outofstock',
    'compare'   => 'NOT IN'
)));

}

remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

I modified the code from this URL: http://www.wptaskforce.com/how-to-exclude-one-or-more-category-in-woocommerce-shop-page/

Saved here again just in case that site goes offline: (this code excludes certain product categories)

add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() ) {

$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'PUT YOUR CATEGORY HERE' ), // Don't display products in the membership category on the shop page . For multiple category , separate it with comma.
'operator' => 'NOT IN'
)));

}



remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

}
like image 87
patrickzdb Avatar answered Oct 19 '22 20:10

patrickzdb


Note to self: Always read the changelog from developer.

Found the answer here: http://develop.woothemes.com/woocommerce/2014/02/solving-common-issues-after-updating-to-woocommerce-2-1/#category-counts-incorrect

In case the product counts for categories are showing a too high or too low number, after updating to WooCommerce 2.1 there is an easy workaround.

Go to the ‘Tools’ tab inside the WooCommerce > System Status of your WordPress administration panel. Here you first use the ‘Recount terms’ button and after that use the ‘Clear transients’ button. This will force the system to recount all the products the next time a category is loaded.

Update: Also remember that it is not enough to change stock quantity to 0. You must also set "Stock status" to "Out of stock". If not the product will be counted in the shop, even if there are no products in stock.

like image 41
janlindso Avatar answered Oct 19 '22 21:10

janlindso


I found easier way, if anybody is still looking for hiding out of stock products in woocommerce, follow these easy steps without editing html !

  1. Go to WooCommerce -> Settings
  2. Go to Inventory
  3. There's a checkbox that says something about our problem, however it goes in english :-) you'll find what you need
  4. Save
like image 23
klaudia Avatar answered Oct 19 '22 21:10

klaudia


that will only work if you are using the official woocommerce shortcodes , but if you creating a page with visual composer and using customized plugins or 3rd party plugins or shortcodes , the first step is to for the query that run from the loop then you modify it to something like this

$params = array(
        'posts_per_page' => 5,
        'post_type' => array('product', 'product_variation'),
        'meta_query' => array(
            array(
                'key' => '_stock_status',
                'value' => 'instock'
            )
        )
);

the most important part that you have to be sure of is

    'meta_query' => array(
        array(
            'key' => '_stock_status',
            'value' => 'instock'
        )
    )
like image 4
albaiti Avatar answered Oct 19 '22 19:10

albaiti


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
  3. Check the option Out Of Stock Visibility that hides the out of stock products

enter image description here

like image 4
NJENGAH Avatar answered Oct 19 '22 19:10

NJENGAH