Round off the shipping cart unit price, discount.

kentico newuser asked on August 6, 2015 21:39

Hello,

I am trying to round off the value present in the cart items using the code as below:

foreach (var shoppingCart in ShoppingCartInfoObj.CartItems) { shoppingCart.UnitPrice = Math.Round(shoppingCart.UnitPrice,2);
}

but this says it can't be changed as it's a read only property. Is there any way from where this can be changed?

Kind Regards, Amit

Recent Answers


Virgil Carroll answered on August 7, 2015 19:28

Amit,

Where are you trying to do this? Did you create an override on a Kentico class to try this?

0 votesVote for this answer Mark as a Correct answer

kentico newuser answered on August 8, 2015 11:49 (last edited on August 9, 2015 00:04)

My problem is actually I want to round-off the Price of an item calculated after applying discount to two decimal places and use this round-off value to calculate further values like total price etc.

To give a background why I am doing is because of some minor miscalculations happening. For e.g. let's say the price of an item is 91.3 and discount applied is like 0.425 the value becomes 91.3 * .425 = $41.3525. This value is formatted to round-off to two decimal places on the UI side as 41.35. But the actual object for the price in the code is still 41.3525. Now if the user orders 3 items the total price should have been 41.35* 3= 124.05 but as the total price is fetched from the object present in the code this comes out to be 41.3525 * 3= 124.0575 which after round-off is displayed on UI side as 124.06.

So I tried changing the price object by overriding the ShoppingCartInfoProvider class and overriding method EvaluateContentInternal as below:

protected override void EvaluateContentInternal(ShoppingCartInfo cart) { base.EvaluateContentInternal(cart);

        foreach (DataRow data in cart.ContentTable.Rows)
        {
            data.SetField("UnitPrice", (Math.Round(Convert.ToDouble(data["UnitPrice"]),2)));
        }
        cart.ContentTable.AcceptChanges();
    }

When I applied debugger to the UnitPrice the value get rounded to two decimal places but I expected other values to be changed by this like the cart.TotalPrice in the cart object but this is still the old value.

Let me know if something more is needed to understand the problem.

Kind Regards, Amit

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.