There are multiple ways you can query WooCommerce products in WordPress. Though generally you can use the native get_posts function, it is recommended to use the WooCommerce native wc_get_products function. Both the function takes an array of arguments which can be tweaked to get a list of desired values.
Before we dissect these to function, please note certain points.
Post Type: Both the blog post and WooCommerce product data gets stored on "wp_posts" database table. The only issue that separates them is the "post_type" column on the database table. For regular blog post the default value of "post_type" column is "post" and for WooCommerce product, it is "product" value. This simple column allows users to create as many post type as they want to store them on database. However, the post type value must be unique in order to identify them separately.
Meta Data: In similar fashion, all the post related meta data gets stored on "wp_postmeta" database table regardless of the post type. Only the post id recorded on "wp_posts" (unique number which automatically get generated by database) table gets used on "wp_postmeta" table using a key and value pair. This is being done to build a relationship between post and meta data. Though each meta data row has a unique number, it can have only one post id at any given point. So, basically you can store unlimited number of meta data for a single post which belongs to a certain post type and it only gets identified by the post id.
Now, let's see how we can use get_posts and wc_get_products functions to retrieve products.
This is the native WordPress function that query through all the post type stored on wp_posts table. This is how it works.
$products = get_posts( array( 'numberposts' => -1, 'post_type' => 'product', 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC') );
Did you notice that we passed the post_type value to be product as an argument? It has been done to run the query only for "product" post type which is generally used by WooCommerce product. If you have any other custom post type, you can use that value as well to query other post types.
Now, let's use the WooCommerce native wc_get_products function to query published products. This is as simple as it can get. Through there are so many more arguments that can be used to narrow down your result but this is pretty much a basic example of its usage.
$products = wc_get_products( array( 'limit' => -1, 'status' => 'publish', 'orderby' => 'date', 'order' => 'DESC') );
Both the query mentioned above will return the same result. However, as you can see from the naming of the function itself, wc_get_products is only focused towards the "product" post type by default and can not be used to query other post types. That's part of the reason why one should always use this function to query WooCommerce products holding the idea of WooCommerce plugin to be in active state.
Note: One must not underestimate the weight of get_posts functions either. Though this function is more of a general function to query posts and various post types, it is powerful enough to perform a wide range of query based on very complex arguments. Since this function is not dependent on WooCommerce plugin, it can be highly usable for a site that holds lots of WooCommerce product data yet WooCommerce plugin is not in active state for any reason.
Also note that, disabling or deactivating WooCommerce plugin does not necessarily mean you loose all of your product data. So, even if you decide to abandon the WooCommerce plugin for any reason, you can still retrieve product and product variations post types and relevant data using this single function. However, you should always use WC native functions if you have WooCommerce plugin activated.
Commenting is disabled.