• Welcome to KonaKart Community Forum. Please login or sign up.
 

Discount module

Started by sashwill, April 24, 2009, 04:14:01 pm

Previous topic - Next topic

sashwill

Has anybody written a quantity discount module that changes the price at the line item level instead of the order total like the one that comes with Konakart?

To the kona experts:
Is there an easy way to modify the existing module?

julie

I haven't tried this, but what you can probably do is to modify the existing module to return null and to edit one or more of the OrderProduct objects of the Order object passed in to the getOrderTotal() method of the module. The Order contains an array of OrderProducts which describe the items of the order.

sashwill

I got this to work, almost as you suggested.
Here is the section out of ProductDiscount.java that I modified:

Quote// Loop through promotion products to determine whether to apply a discount
                boolean firstLoop = true;
                for (int j = 0; j < promotion.getApplicableProducts().length; j++) {
                    OrderProductIf op = promotion.getApplicableProducts()[j];
                    boolean anotherDiscount = false;
                    if (promArray.length > 1) {
                        for (int ii = i; ii < promArray.length; ii++) {
                            if (promotion == promArray[ii]) {
                                continue;
                            }
                            BigDecimal discountApplied2 = getCustomBigDecimal(promArray[ii].getCustom3(), 3);
                            int minProdQuantity2 = getCustomInt(promArray[ii].getCustom2(), 2);
                            if (discountApplied.compareTo(discountApplied2) != 1 && op.getQuantity() >= minProdQuantity2 ) {
                                anotherDiscount = true;
                            }


                        }
                    }
                    if (op != null && op.getQuantity() >= minProdQuantity && !anotherDiscount) {
                        System.out.println("calculating new price");
                        // Get the current //total// price of the product(s)
                        BigDecimal currentOrderPrice = null;
                        BigDecimal currentPrice = null;
                        if (applyBeforeTax) {
                            currentOrderPrice = op.getFinalPriceExTax();
                            currentPrice = op.getPrice();
                        } else {
                            currentOrderPrice = op.getFinalPriceIncTax();
                        }

                        // Apply the discount
                        BigDecimal discount = null;
                        BigDecimal orderDiscount = null;
                        if (percentageDiscount) {
                            // Apply a percentage discount
                            discount = (currentPrice.multiply(discountApplied)).divide(new BigDecimal(100));
                            orderDiscount = (currentOrderPrice.multiply(discountApplied)).divide(new BigDecimal(100));
                            op.setPrice(currentPrice.subtract(discount));
                            order.setTotalExTax(order.getTotalExTax().subtract(orderDiscount));
                            order.calculateTotals();

                        } else {
                            // Apply an amount based discount
                            discount = discountApplied.multiply(new BigDecimal(op.getQuantity()));
                        }


I did not modify the dollar amount discount because we are not using it, so be aware that this mod does not cover all types of discounts.  Also testing is very limited at the moment.