如何通过他的 variantKey 找到 ProductVariant?
How can I find a ProductVariant by his variantKey?
在我的项目中,我现在需要从 variantKey 中获取产品的变体,但我没有在 JVM SDK 中找到任何方法来执行此操作。
我尝试使用 ProductByKeyGet 方法来做到这一点,但如果值对应于产品的根键,我只会得到产品,但如果值对应于 variantKey,它对我来说 return 没有任何意义。
有谁知道从 VariantKey 获取变体的方法吗?
提前致谢。
米格尔·德拉霍兹
为此,您将需要使用 Product Projection 端点,您可以在其中查询具有带有所需密钥的变体 "OR" 主变体的产品。通过 JVM SDK,您可以通过执行以下操作来实现:
为您想要的密钥构建一个 QueryPredicate<EmbeddedProductVariantQueryModel>
:
final String myKey = "foo";
final QueryPredicate<EmbeddedProductVariantQueryModel> queryPredicate =
QueryPredicate.of("key=\"" + myKey + "\"");
构建一个Function<ProductProjectionQueryModel, QueryPredicate<ProductProjection>>
查询主变体:
final Function<ProductProjectionQueryModel, QueryPredicate<ProductProjection>> mvPredicateFunction = productQueryModel ->
productQueryModel.masterVariant().where(queryPredicate);
构建一个 Function<ProductProjectionQueryModel, QueryPredicate<ProductProjection>>
来查询其余变体:
final Function<ProductProjectionQueryModel, QueryPredicate<ProductProjection>> variantsPredicateFunction = productQueryModel ->
productQueryModel.variants().where(queryPredicate);
将两个谓词与语义 OR 运算符组合以构建 ProductProjectionQuery
(在本例中为 staged
投影):
final ProductProjectionQuery query = ProductProjectionQuery.ofStaged()
.withPredicates(productQueryModel -> mvPredicateFunction
.apply(productQueryModel)
.or(variantsPredicateFunction.apply(productQueryModel)));
执行请求:
final PagedQueryResult<ProductProjection> requestStage = sphereClient.executeBlocking(query);
由于变体键是唯一的,您应该期望产生一个结果产品投影,如果有的话:
final Optional<ProductProjection> optionalProductProjection = requestStage.head();
遍历生成的产品投影的所有变体(包括主变体)以获取具有此类键的匹配变体:
final Optional<ProductVariant> optionalVariant = optionalProductProjection.flatMap(
productProjection -> productProjection.getAllVariants().stream()
.filter(productVariant -> myKey.equals(productVariant.getKey()))
.findFirst());
更新:
步骤1-4也可以简化为:
final String myKey = "foo";
final QueryPredicate<ProductProjection> productProjectionQueryPredicate = QueryPredicate
.of("masterVariant(key = \"" + myKey + "\") OR variants(key = \"" + myKey + "\")");
final ProductProjectionQuery query = ProductProjectionQuery.ofStaged().withPredicates(
productProjectionQueryPredicate);
今天我们发布了 JVM SDK 1.29.0 版 - 我们在其中添加了对按键查询产品变体的缺失支持(请参阅 https://github.com/commercetools/commercetools-jvm-sdk/issues/1679)。
使用此版本,您可以以类型安全的方式编写查询:
String myKey = "foo";
ProductProjectionType projectionType = ProductProjectionType.CURRENT;
ProductProjectionQuery query =
ProductProjectionQuery.of(projectionType)
.withPredicates(product -> product.allVariants()
.where(variant -> variant.key().is(myKey)));
希望对您有所帮助!
在我的项目中,我现在需要从 variantKey 中获取产品的变体,但我没有在 JVM SDK 中找到任何方法来执行此操作。 我尝试使用 ProductByKeyGet 方法来做到这一点,但如果值对应于产品的根键,我只会得到产品,但如果值对应于 variantKey,它对我来说 return 没有任何意义。
有谁知道从 VariantKey 获取变体的方法吗?
提前致谢。 米格尔·德拉霍兹
为此,您将需要使用 Product Projection 端点,您可以在其中查询具有带有所需密钥的变体 "OR" 主变体的产品。通过 JVM SDK,您可以通过执行以下操作来实现:
为您想要的密钥构建一个
QueryPredicate<EmbeddedProductVariantQueryModel>
:final String myKey = "foo"; final QueryPredicate<EmbeddedProductVariantQueryModel> queryPredicate = QueryPredicate.of("key=\"" + myKey + "\"");
构建一个
Function<ProductProjectionQueryModel, QueryPredicate<ProductProjection>>
查询主变体:final Function<ProductProjectionQueryModel, QueryPredicate<ProductProjection>> mvPredicateFunction = productQueryModel -> productQueryModel.masterVariant().where(queryPredicate);
构建一个
Function<ProductProjectionQueryModel, QueryPredicate<ProductProjection>>
来查询其余变体:final Function<ProductProjectionQueryModel, QueryPredicate<ProductProjection>> variantsPredicateFunction = productQueryModel -> productQueryModel.variants().where(queryPredicate);
将两个谓词与语义 OR 运算符组合以构建
ProductProjectionQuery
(在本例中为staged
投影):final ProductProjectionQuery query = ProductProjectionQuery.ofStaged() .withPredicates(productQueryModel -> mvPredicateFunction .apply(productQueryModel) .or(variantsPredicateFunction.apply(productQueryModel)));
执行请求:
final PagedQueryResult<ProductProjection> requestStage = sphereClient.executeBlocking(query);
由于变体键是唯一的,您应该期望产生一个结果产品投影,如果有的话:
final Optional<ProductProjection> optionalProductProjection = requestStage.head();
遍历生成的产品投影的所有变体(包括主变体)以获取具有此类键的匹配变体:
final Optional<ProductVariant> optionalVariant = optionalProductProjection.flatMap( productProjection -> productProjection.getAllVariants().stream() .filter(productVariant -> myKey.equals(productVariant.getKey())) .findFirst());
更新: 步骤1-4也可以简化为:
final String myKey = "foo";
final QueryPredicate<ProductProjection> productProjectionQueryPredicate = QueryPredicate
.of("masterVariant(key = \"" + myKey + "\") OR variants(key = \"" + myKey + "\")");
final ProductProjectionQuery query = ProductProjectionQuery.ofStaged().withPredicates(
productProjectionQueryPredicate);
今天我们发布了 JVM SDK 1.29.0 版 - 我们在其中添加了对按键查询产品变体的缺失支持(请参阅 https://github.com/commercetools/commercetools-jvm-sdk/issues/1679)。 使用此版本,您可以以类型安全的方式编写查询:
String myKey = "foo";
ProductProjectionType projectionType = ProductProjectionType.CURRENT;
ProductProjectionQuery query =
ProductProjectionQuery.of(projectionType)
.withPredicates(product -> product.allVariants()
.where(variant -> variant.key().is(myKey)));
希望对您有所帮助!