0

I'm looking for small help. I'm using this function in function.php. When I use a shortcode [total-product] I'll get for example "We have a total of 1000 products in stock".

Using Get the count of all "In stock" products in WooCommerce code, takes a number of products from all categories, but I need to get number just only from specific categories - not from all together.

I don't know how to change the SQL query to select from specific categories, eg by ID.

Is there any chance of counting the number of product "in stock" according to specific categories? Something like (I have no idea about Database structure of WP tables):

AND category_id LIKE '100;101;...' // one or more category IDs
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
RoMeiRo
  • 1
  • 1
  • You want to use the `wp_terms` and `wp_termmeta` tables. There are functions such as [get_term_by](https://developer.wordpress.org/reference/functions/get_term_by/) that will give you the category ID if you don't already know that. – hostingutilities.com Jan 16 '21 at 01:02
  • Without SQL, the WordPress way of doing what you want is to use https://generatewp.com/wp_query/ with a type of `product` and a taxonomy query for the categories you want and then under custom fields you would add a meta query to only get the in stock products. After doing that, you would get the count using `$query->found_posts;`. It might be more work doing things that way though since you already have the majority of your SQL query figured out. – hostingutilities.com Jan 16 '21 at 01:06

0 Answers0