commercetools

How to get CartDiscounts for particular cart?


I recently work with Commercetools platform and I have such a question.

How can I get a CartDiscounts for some particular cart? Now I have method for get Cart by Id and I wanna get all CartDiscount from Cart. How I can do that? Or CartDiscount applies to all Cart and I can do something like CartDiscountsQuery.of()?


Solution

  • Discounts are stored on the line items, custom line items and the shipping method of a cart.

    For the shipping method you can get the discounts like so:

    final Cart fetchedCart = client.executeBlocking(CartByIdGet.of(id));
    final List<DiscountedLineItemPortion> includedDiscounts = fetchedCart.getShippingInfo().getDiscountedPrice().getIncludedDiscounts();
    

    Please consult the API reference docs for further information. https://docs.commercetools.com/http-api-projects-carts.html#shippinginfo