Automattic\WooCommerce\Admin\API\Reports
DataStore::get_products_by_cat_ids
Returns an array of products belonging to given categories.
Method of the class: DataStore{}
No Hooks.
Returns
array|stdClass.
Usage
// protected - for code of main (parent) or child class $result = $this->get_products_by_cat_ids( $categories );
- $categories(array) (required)
- List of categories IDs.
DataStore::get_products_by_cat_ids() DataStore::get products by cat ids code WC 11.0.1
protected function get_products_by_cat_ids( $categories ) {
$terms = get_terms(
array(
'taxonomy' => 'product_cat',
'include' => $categories,
)
);
if ( is_wp_error( $terms ) || empty( $terms ) ) {
return array();
}
$args = array(
'category' => wc_list_pluck( $terms, 'slug' ),
'limit' => -1,
'return' => 'ids',
);
return wc_get_products( $args );
}