Few best practises while working with orders

  • Validate the status of line item (at required stage in sale flow) to make sure the product is active, within the valid date range, and that the catalog entry is available in the current market.
  • Validate the status of line item (at required stage in sale flow) to make sure business does not lose money.
  • It is important to add coupons to the IOrderForm before running apply discounts(cart.ApplyDiscount method)
  • Avoid saving carts unnecessarily and repeatedly.
  • You should avoid casting back and forth between concrete Order classes (OrderGroup, OrderForm, etc.) and the new abstraction interfaces (IOrderGroup, IOrderForm, etc.).
    For example, from the time you load the cart or purchase order until you save it, only use one API. While this is not always possible, doing it whenever possible helps avoid hidden problems when casting.
  • Do not use Math.Round() for rounding. The following methods return a value with the correct number of decimals, which is determined by the input currency.
    - Currency.Round()
    - Currency.Percentage()
    - Money.Round()
    After rounding, use the rounded value for all further totals. e.g
    var billingCurrency = order.Currency;
    foreach (var item in order.GetAllLineItems())
    {
      var costWithoutDiscount = billingCurrency.Round(item.PlacedPrice * item.Quantity);
      item.Properties["costWithoutDiscount"] = costWithoutDiscount;
    }
  • To get an order‘s correctly-rounded tax amount, do not use the tax amount of individual items. Instead, follow this example.
    var billingCurrency = order.Currency;
    var saleTaxesAmount = taxes
      .Where(x => x.TaxType == TaxType.SalesTax)
      .Sum(x => billingCurrency.Percentage(itemPriceWithoutTax, x.Percentage));
  • SerializableCarts provides a fast and efficient way to load, update, and save cart information. All components and custom json converters are internal, which means that the classes support the Episerver infrastructure, and are not intended to be used directly from your code.

Comments

Popular posts from this blog

EPiServer CMS 11 Useful SQL Queries - 1

EPiServer CMS 11 Useful SQL Queries - 2

Strongly Type Models in EPiServer Commerce