Java 如何通过他的variantKey找到ProductVariant?
在我的项目中,我现在需要从variantKey获取产品的变体,但是我在JVMSDK中没有找到任何方法来实现这一点。 我尝试使用ProductByKeyGet方法来实现,但我只在值对应于产品的根键时获取产品,但如果值对应于variantKey,它不会向我返回任何信息 有人知道从VariantKey获取变体的方法吗 提前谢谢。Java 如何通过他的variantKey找到ProductVariant?,java,commercetools,sphere.io,Java,Commercetools,Sphere.io,在我的项目中,我现在需要从variantKey获取产品的变体,但是我在JVMSDK中没有找到任何方法来实现这一点。 我尝试使用ProductByKeyGet方法来实现,但我只在值对应于产品的根键时获取产品,但如果值对应于variantKey,它不会向我返回任何信息 有人知道从VariantKey获取变体的方法吗 提前谢谢。 Miguel de la Hoz为此,您需要使用端点,在这里您可以查询具有所需密钥的“变体”或“主变体”的产品。通过JVM SDK,您可以通过执行以下操作来实现这一点: 为所
Miguel de la Hoz为此,您需要使用端点,在这里您可以查询具有所需密钥的“变体”或“主变体”的产品。通过JVM SDK,您可以通过执行以下操作来实现这一点:
QueryPredicate
:
final String myKey = "foo";
final QueryPredicate<EmbeddedProductVariantQueryModel> queryPredicate =
QueryPredicate.of("key=\"" + myKey + "\"");
final PagedQueryResult<ProductProjection> requestStage = sphereClient.executeBlocking(query);
final PagedQueryResult requestStage=sphereClient.ExecuteBlock(查询);
final Optional<ProductProjection> optionalProductProjection = requestStage.head();
final optionalProductProjection=requestStage.head();
final Optional<ProductVariant> optionalVariant = optionalProductProjection.flatMap(
productProjection -> productProjection.getAllVariants().stream()
.filter(productVariant -> myKey.equals(productVariant.getKey()))
.findFirst());
final 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);
最后一个字符串myKey=“foo”;
最终查询预测产品项目查询预测=查询预测
.of(“主变量(key=\”“+myKey+”\)或变量(key=\”“+myKey+”\”);
final ProductProjectionQuery query=ProductProjectionQuery.Ofstage().With谓词(
productProjectionQueryPredicate);
为此,您需要使用端点,在端点中,您可以查询具有所需密钥的“变体”或“主变体”的产品。通过JVM SDK,您可以通过执行以下操作来实现这一点:
QueryPredicate
:
final String myKey = "foo";
final QueryPredicate<EmbeddedProductVariantQueryModel> queryPredicate =
QueryPredicate.of("key=\"" + myKey + "\"");
final PagedQueryResult<ProductProjection> requestStage = sphereClient.executeBlocking(query);
final PagedQueryResult requestStage=sphereClient.ExecuteBlock(查询);
final Optional<ProductProjection> optionalProductProjection = requestStage.head();
final optionalProductProjection=requestStage.head();
final Optional<ProductVariant> optionalVariant = optionalProductProjection.flatMap(
productProjection -> productProjection.getAllVariants().stream()
.filter(productVariant -> myKey.equals(productVariant.getKey()))
.findFirst());
final 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);
最后一个字符串myKey=“foo”;
最终查询预测产品项目查询预测=查询预测
.of(“主变量(key=\”“+myKey+”\)或变量(key=\”“+myKey+”\”);
final ProductProjectionQuery query=ProductProjectionQuery.Ofstage().With谓词(
productProjectionQueryPredicate);
今天,我们发布了JVM SDK的1.29.0版-在该版本中,我们添加了缺少的对按键查询产品变体的支持(请参阅)。
使用此版本,您可以以类型安全的方式编写查询:
String myKey = "foo";
ProductProjectionType projectionType = ProductProjectionType.CURRENT;
ProductProjectionQuery query =
ProductProjectionQuery.of(projectionType)
.withPredicates(product -> product.allVariants()
.where(variant -> variant.key().is(myKey)));
希望这有帮助 今天,我们发布了JVMSDK的1.29.0版,其中添加了对按键查询产品变体的缺少的支持(请参阅)。 使用此版本,您可以以类型安全的方式编写查询:
String myKey = "foo";
ProductProjectionType projectionType = ProductProjectionType.CURRENT;
ProductProjectionQuery query =
ProductProjectionQuery.of(projectionType)
.withPredicates(product -> product.allVariants()
.where(variant -> variant.key().is(myKey)));
希望这有帮助 搜索端点应用于与产品变体相关的产品发现,这就是为什么查询端点的DSL不支持它。使用instead@Dalek搜索端点也可以用于此目的。但我认为这取决于用例。需要注意的是,Product Projection Search endpoint提供了一种保证,这意味着它不应用于数据同步或产品显示页面中,用户需要将产品添加到购物车中,因为数据可能不是最新的。谢谢@Dalek。我已经使用了您的代码,但是使用了Product对象而不是ProductProjection,因为我需要的是获取产品以在以后更新它。我不需要在任何时候展示产品。再次感谢。搜索端点应用于产品变体相关的产品发现,这就是为什么查询端点的DSL不支持它。使用instead@Dalek搜索端点也可以用于此目的。但我认为这取决于用例。需要注意的是,Product Projection Search endpoint提供了一种保证,这意味着它不应用于数据同步或产品显示页面中,用户需要将产品添加到购物车中,因为数据可能不是最新的。谢谢@Dalek。我已经使用了您的代码,但是使用了Product对象而不是ProductProjection,因为我需要的是获取产品以在以后更新它。我不需要在任何时候展示产品。再次感谢