1
我使用commercetools JVM SDK並查詢ProductProjections,如果應用絕對產品折扣,如何獲取信息?commercetools:我如何獲得產品的產品折扣類型?
我使用commercetools JVM SDK並查詢ProductProjections,如果應用絕對產品折扣,如何獲取信息?commercetools:我如何獲得產品的產品折扣類型?
你需要擴大產品的價格範圍內的產品折扣才能夠得到這個信息,然後使用與ProductDiscountValue一個instanceof檢查:
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();