WC_Product_Data_Store_CPT::get_products PHP Method

get_products() public method

Returns an array of products.
public get_products ( array $args = [] ) : array
$args array @see wc_get_products
return array
    public function get_products($args = array())
    {
        /**
         * Generate WP_Query args.
         */
        $wp_query_args = array('post_type' => 'variation' === $args['type'] ? 'product_variation' : 'product', 'post_status' => $args['status'], 'posts_per_page' => $args['limit'], 'meta_query' => array(), 'orderby' => $args['orderby'], 'order' => $args['order'], 'tax_query' => array());
        // Do not load unneccessary post data if the user only wants IDs.
        if ('ids' === $args['return']) {
            $wp_query_args['fields'] = 'ids';
        }
        if ('variation' !== $args['type']) {
            $wp_query_args['tax_query'][] = array('taxonomy' => 'product_type', 'field' => 'slug', 'terms' => $args['type']);
        }
        if (!empty($args['sku'])) {
            $wp_query_args['meta_query'][] = array('key' => '_sku', 'value' => $args['sku'], 'compare' => 'LIKE');
        }
        if (!empty($args['category'])) {
            $wp_query_args['tax_query'][] = array('taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $args['category']);
        }
        if (!empty($args['tag'])) {
            $wp_query_args['tax_query'][] = array('taxonomy' => 'product_tag', 'field' => 'slug', 'terms' => $args['tag']);
        }
        if (!empty($args['shipping_class'])) {
            $wp_query_args['tax_query'][] = array('taxonomy' => 'product_shipping_class', 'field' => 'slug', 'terms' => $args['shipping_class']);
        }
        if (!is_null($args['parent'])) {
            $wp_query_args['post_parent'] = absint($args['parent']);
        }
        if (!is_null($args['offset'])) {
            $wp_query_args['offset'] = absint($args['offset']);
        } else {
            $wp_query_args['paged'] = absint($args['page']);
        }
        if (!empty($args['exclude'])) {
            $wp_query_args['post__not_in'] = array_map('absint', $args['exclude']);
        }
        if (!$args['paginate']) {
            $wp_query_args['no_found_rows'] = true;
        }
        // Get results.
        $products = new WP_Query($wp_query_args);
        if ('objects' === $args['return']) {
            $return = array_map('wc_get_product', $products->posts);
        } else {
            $return = $products->posts;
        }
        if ($args['paginate']) {
            return (object) array('products' => $return, 'total' => $products->found_posts, 'max_num_pages' => $products->max_num_pages);
        } else {
            return $return;
        }
    }