question

steve avatar image
steve asked steve commented

Is LineItem.getDiscountAmount() implemented ?

Hi, I have a reference to a LineItem and it returns a List<discount> when I call getDiscounts() on it. However when I call getDiscountAmount() on the same valid reference I get a NullPointerException. The description of this method in the JavaDocs is: "does the calculated flag actually do anything?" which gives the impression that the method is not implemented and is probably just throwing a NullPointerException. Could anyone verify if this is the case?

Thanks, Steve

10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Jacob Abrams avatar image
Jacob Abrams answered steve commented

Yes the getDiscountAmount() method is not implemented, it should be removed. What exactly are you looking for? A line item can have many discounts, there are also modifications and order level discounts that apply to all line items. If you want to compute the overall discount amount including order level discounts we have some provided functionality that can help. Here is some sample code:

OrderCalc calc = new OrderCalc(order);
Collection<LineItem> lineItem = Collections.singletonList(order.getLineItems().get(0));
Price itemSubtotal = new Price(calc.getLineSubtotal(lineItem));
Price itemSubtotalWithoutDiscounts = new Price(calc.getLineSubtotalWithoutDiscounts(lineItem));
Price lineItemComputedDiscountAmount = itemSubtotalWithoutDiscounts.subtract(itemSubtotal);

If you want to compute the line item discount amount excluding the order level discounts you will need to calculate that yourself using the discount values provided in the LineItem.

1 comment
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

steve avatar image steve commented ·

Thanks jacobabrams,

As you guessed I was trying to compute the line item discount amount excluding the order level discounts. I managed to get that working correctly. Thanks for the sample code - it will be useful for my next task.

Steve

0 Likes 0 ·

Welcome to the
Clover Developer Community