Warm tip: This article is reproduced from serverfault.com, please click

Get woocommerce product category using Gutenberg getEntityRecords

发布于 2019-02-27 07:52:02

Is it possible to get all product categories using Gutenberg getEntityRecords()?

I have found a code for getting post category as follows

var query = {per_page: 100}
categoriesList =  getEntityRecords( 'taxonomy', 'category', query );

Can I alter the above code to get all woocommerce products category?

Questioner
TechUser
Viewed
0
21k 2020-11-28 20:16:22

I have also searched the same thing. But at last I have decided to use the apiFetch for this tasks (following woocommerce-gutenberg-products-block plugin).

for example a sample use case :

const apiFetch = wp.apiFetch;
const { addQueryArgs } = wp.url;

const productCategories = (queryArgs) => {
    return apiFetch({
        path: addQueryArgs(`wc/store/products/categories`, {
            per_page: 0,
            ...queryArgs,
        }),
    });
};

productCategories().then((categories) => {
        console.log(categories);
});