2
votes

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?

1
- Did you use WordPress REST API inside componentDidMount lifecycle to get the data via fetch request? You can also look into the WooCommerce default blocks to get the idea how those blocks are requesting the data.Mehmood Ahmad

1 Answers

1
votes

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);
});