Search code examples
javascriptwordpresswoocommercecustom-taxonomygutenberg-blocks

Get woocommerce product category using Gutenberg getEntityRecords


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?


Solution

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