首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Commercetools:如何获取产品中的产品折扣类型?

Commercetools:如何获取产品中的产品折扣类型?
EN

Stack Overflow用户
提问于 2016-05-31 00:22:29
回答 1查看 105关注 0票数 1

我使用commercetools SDK和查询ProductProjections,如果产品有绝对折扣,如何获取信息?

EN

回答 1

Stack Overflow用户

发布于 2016-05-31 00:22:29

您需要在产品价格内扩展产品折扣,以便能够获得此信息,然后使用ProductDiscountValue的实例检查:

代码语言:javascript
复制
final ProductProjectionQuery query = ProductProjectionQuery.ofCurrent()
    //your query predicate
    .withPredicates(m -> m.id().is(productId))
    //asks to expand in every variant the discount information
    .withExpansionPaths(m -> m.allVariants().prices().discounted().discount());
final Optional<ProductProjection> loadedProduct = client().executeBlocking(query).head();
assertThat(loadedProduct.isPresent()).isTrue();
final ProductProjection productProjection = loadedProduct.get();
final List<Price> prices = productProjection.getMasterVariant().getPrices();
//here you need to have a look if it is the right price, maybe http://commercetools.github.io/commercetools-jvm-sdk/apidocs/io/sphere/sdk/products/search/ProductVariantFilterSearchModel.html#scopedPrice-- is a better option for you           
final Price price = prices.get(0);
assertThat(price.getDiscounted()).isNotNull();
final ProductDiscount referenceExpandedProductDiscount = price.getDiscounted().getDiscount().getObj();
assertThat(referenceExpandedProductDiscount).isNotNull();
assertThat(referenceExpandedProductDiscount.getValue() instanceof AbsoluteProductDiscountValue)
        .as("this is how you check if the discount is absolute")
        .isTrue();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37530094

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档