简体   繁体   English

如何从Commercetools平台中的唯一ID获取类别名称?

[英]How to get category name from unique ID in Commercetools platform?

I am attempting to get the names of all categories that a product belongs to in Commercetools platform. 我正在尝试获取Commercetools平台中产品所属的所有类别的名称。

I can get the unique ID of each category tied to a product through the following call: 我可以通过以下调用获得与产品绑定的每个类别的唯一ID:

final ProductProjectionQuery query = ProductProjectionQuery.ofCurrent();
    ProductProjectionQuery q = query.withLimit(450);

    try {
        PagedQueryResult<io.sphere.sdk.products.ProductProjection> pagedQueryResult = client.execute(q).toCompletableFuture().get();
        List<io.sphere.sdk.products.ProductProjection> products = pagedQueryResult.getResults();

        //createDocument(products.get(0).getMasterVariant(), request);

        for (io.sphere.sdk.products.ProductProjection product : products) {
            String categoryId = product.getCategories().iterator().next().getId();
            //createDocument(product.getMasterVariant(), request);
        }
    }

Though once I have the categoryId, I am unsure of how to access the category name. 尽管一旦有了categoryId,我不确定如何访问类别名称。 I thought that the obj property might allow me to drill down into the category, but the obj variable always seems to be null . 我以为obj属性可以允许我深入到类别中,但是obj变量始终似乎为null

The obj variable is null because you have not expanded the reference. obj变量为null,因为尚未扩展引用。 Unless you explicitly request it, all references will be empty to improve performance. 除非您明确要求,否则所有引用都将为空以提高性能。 In order to expand it, you can use this code: 为了扩展它,您可以使用以下代码:

// Query products
final ProductProjectionQuery query = ProductProjectionQuery.ofCurrent()
        .withExpansionPaths(product -> product.categories()) // Request to expand categories
        .withLimit(450);
final List<ProductProjection> products = client.execute(query).toCompletableFuture().join().getResults();

for (ProductProjection product : products) {
    final List<LocalizedString> categoryLocalizedNames = product.getCategories().stream()
            .map(categoryRef -> categoryRef.getObj().getName())
            .collect(toList());
    // Do something with categoryLocalizedNames
}

But I strongly recommend you to cache the categories in a CategoryTree instance and grab the name from it, because otherwise the performance will be quite affected by expanding all categories of each product. 但是我强烈建议您将类别缓存在CategoryTree实例中并从中获取名称,因为否则,性能会受到扩展每个产品的所有类别的影响。 Here is the code: 这是代码:

// Query all categories and put them in a CategoryTree
final CategoryQuery queryAllCategories = CategoryQuery.of().withLimit(500);
final List<Category> allCategories = client.execute(queryAllCategories).toCompletableFuture().join().getResults();
final CategoryTree categoryTree = CategoryTree.of(allCategories);

// Query products
final ProductProjectionQuery query = ProductProjectionQuery.ofCurrent().withLimit(500);
final List<ProductProjection> products = client.execute(query).toCompletableFuture().join().getResults();

for (ProductProjection product : products) {
    final List<LocalizedString> categoryLocalizedNames = new ArrayList<>();
    product.getCategories().forEach(categoryRef -> {
        final Optional<Category> categoryOpt = categoryTree.findById(categoryRef.getId());
        if (categoryOpt.isPresent()) {
            categoryLocalizedNames.add(categoryOpt.get().getName());
        }
    });
    // Do something with categoryLocalizedNames
}

Of course that means you also have to find a solution to invalidate the cached categories when they change. 当然,这意味着您还必须找到一种解决方案,以在缓存的类别更改时使它们无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM