3

是否可以使用 Gutenberg 获得所有产品类别getEntityRecords()

我找到了获取帖子类别的代码,如下所示

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

我可以更改上述代码以获取所有 woocommerce 产品类别吗?

4

2 回答 2

2

我也搜索过同样的东西。但最后我决定使用 apiFetch 来完成这项任务(在 woocommerce-gutenberg-products-block 插件之后)。

例如一个示例用例:

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);
});
于 2020-11-28T12:10:57.490 回答
1

你实际上已经很接近getEntityRecords上班了。唯一的问题是产品类别与帖子类别的分类不同。

useSelect这是一个使用钩子的完整示例:

import { useSelect } from "@wordpress/data";

const { productCategories, isSearching } = useSelect((select) => {
    const { getEntityRecords, isResolving } = select("core");
    const query = { per_page: 100 };

    return {
        productCategories: getEntityRecords("taxonomy", "product_cat", query),
        isSearching: isResolving("getEntityRecords", [
            "taxonomy",
            "product_cat",
            query,
        ]),
    };
});
于 2021-11-04T04:07:59.913 回答