Skip to content
Advertisement

WP_Query issue when trying to get out of stock products in WooCommerce

I’m using shortcode to display all sold out WooCommerce items on a WordPress page. It works fine, except for throwing an error:

JavaScript

I figure that this means array_filter() expects an array, but is given null from the following code in my functions.php:

JavaScript

Shortcode: [out_of_stock_products]

Here’s the WooCommerce function that expects the array but is given null:

JavaScript

I’m not sure how to fix this. Maybe there is a better way to display sold out products altogether, though I like this version.

Could anyone help me fix this?

Source: https://www.businessbloomer.com/woocommerce-display-stock-products-shortcode/

Edit:

As @LoicTheAztec suggests, another bit of code than the ones posted above is responsible:

JavaScript

I am using this to hide sold products from WooCommerce.

Advertisement

Answer

Update 2

To get out of stock products, you can also use instead:

  1. A WC_Query using wc_get_products() function as follows:
JavaScript

2). A WP_Query with a tax query like:

JavaScript

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Related: Hide “out of stock” products with custom meta data In Woocommerce


Addition: The problem comes from your edit (last code).

Also since WooCommerce 3, you should use instead the following:

JavaScript

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement