HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

Creating a return order form and exchange offer

This topic describes how create a return order form and an exchange order.

How it works

  • EPiServer.Commerce.Order.IReturnOrderForm. Information about a return order form. This interface inherits IOrderForm. The ReturnForms collection in IPurchaseOrder was changed from ICollection<IOrderForm> to ICollection<IReturnForm>.

  • EPiServer.Commerce.Order.IReturnLineItem. Information about a return line item. This interface inherits ILineItem. The LineItem class inherits IReturnLineItem and contains OriginalLineItemId.

  • EPiServer.Commerce.Order.IReturnLineItemCalculator. Calculates the extended price for an IReturnLineItem.

    • GetExtendedPrice: Gets the extended price for an IReturnLineItem.
    • GetDiscountedPrice: Gets the discounted price for an IReturnLineItem.
  • EPiServer.Commerce.Order.IReturnOrderFormCalculator. Calculates totals on an IReturnOrderForm

    • GetHandlingTotal. Gets the handling total for an IReturnOrderForm.
    • GetSubTotal. Calculates the total of all shipments in an IReturnOrderForm.
    • GetTotal. Gets the total for an IReturnOrderForm.
    • GetOrderDiscountTotal. Gets the discount price of an IReturnOrderForm.
    • GetDiscountTotal. Gets the discount total of an IReturnOrderForm.
  • EPiServer.Commerce.Order.IShippingCalculator. Add API.

    • GetShippingReturnItemsTotal. Gets the total of process for all return line items in the shipment.
  • EPiServer.Commerce.Order.ITaxCalculator. Add APIs.

    • GetShippingReturnTaxTotal. Gets the tax total for a return shipment that contains.
    • GetReturnTaxTotal. Gets the return tax total for an IReturnOrderForm.
  • EPiServer.Commerce.Order.IPurchaseOrderFactory. Creates an instance of purchasing order components.

    • CreateReturnOrderForm. Creates the return order form for a purchase order.

See also: Blog post CartHelper is dead, long live IOrderRepository.

var purchaseOrderFactory = ServiceLocator.Current.GetInstance<IPurchaseOrderFactory>();
            var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();   
            var orderLink = new OrderReference(1, "Default", Guid.NewGuid(), typeof(IPurchaseOrder));
            var purchaseOrder = orderRepository.Load<IPurchaseOrder>(orderLink.OrderGroupId);
            var returnOrderForm = purchaseOrderFactory.CreateReturnOrderForm(purchaseOrder);
- CreateReturnShipment. Creates a shipment for the return.
var purchaseOrderFactory = ServiceLocator.Current.GetInstance<IPurchaseOrderFactory>();
            var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
            var orderLink = new OrderReference(1, "Default", Guid.NewGuid(), typeof(IPurchaseOrder));
            var purchaseOrder = orderRepository.Load<IPurchaseOrder>(orderLink.OrderGroupId);
            var shipment = purchaseOrder.GetFirstShipment();
            var returnShipment = purchaseOrderFactory.CreateReturnShipment(shipment);
- CreateReturnLineItem. Creates a return line item from original line item, with return quantity and reason.  
var purchaseOrderFactory = ServiceLocator.Current.GetInstance<IPurchaseOrderFactory>();
            var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
            var orderLink = new OrderReference(1, "Default", Guid.NewGuid(), typeof(IPurchaseOrder));
            var purchaseOrder = orderRepository.Load<IPurchaseOrder>(orderLink.OrderGroupId);
            var shipment = purchaseOrder.GetFirstShipment();
            var returnLineItem = purchaseOrderFactory.CreateReturnLineItem(shipment.LineItems.First(), 1, "broken");
- CreateExchangePayment. Creates a payment for an exchange order.  
var purchaseOrderFactory = ServiceLocator.Current.GetInstance<IPurchaseOrderFactory>();
            var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
            var orderLink = new OrderReference(1, "Default", Guid.NewGuid(), typeof(IPurchaseOrder));
            var purchaseOrder = orderRepository.Load<IPurchaseOrder>(orderLink.OrderGroupId);
            var shipment = purchaseOrder.GetFirstShipment();
            var exchangePayment = purchaseOrderFactory.CreateExchangePayment()

;

  • EPiServer.Commerce.Order.IReturnPurchaseOrderCalculator. Calculates return totals on a purchase order.
    • GetTotal. Gets the return total of all return order forms.
var returnOrderCalculator = ServiceLocator.Current.GetInstance<IReturnOrderCalculator>();
            var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
            var orderLink = new OrderReference(1, "Default", Guid.NewGuid(), typeof(IPurchaseOrder));
            var purchaseOrder = orderRepository.Load<IPurchaseOrder>(orderLink.OrderGroupId);
            var returnTotal = returnOrderCalculator.GetTotal(purchaseOrder);
- GetSubTotal. Gets the return subtotal of all return order forms.  
var returnOrderCalculator = ServiceLocator.Current.GetInstance<IReturnOrderCalculator>();
            var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
            var orderLink = new OrderReference(1, "Default", Guid.NewGuid(), typeof(IPurchaseOrder));
            var purchaseOrder = orderRepository.Load<IPurchaseOrder>(orderLink.OrderGroupId);
            var returnSubTotal = returnOrderCalculator.GetSubTotal(purchaseOrder);
  • EPiServer.Commerce.Order.IReturnOrderNumberGenerator. Generates the return order form number and exchange order number.
var purchaseOrderFactory = ServiceLocator.Current.GetInstance<IPurchaseOrderFactory>();
        var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
        var orderLink = new OrderReference(1, "Default", Guid.NewGuid(), typeof(IPurchaseOrder));
        var order = orderRepository.Load<IPurchaseOrder>(orderLink.OrderGroupId);
        var returnOrderNumberGenerator = ServiceLocator.Current.GetInstance<IReturnOrderNumberGenerator>();
        var exchangeOrderNumber = returnOrderNumberGenerator.GenerateExchangeOrderNumber(order);
        var returnOrderFormNumber = returnOrderNumberGenerator.GenerateReturnOrderFormNumber(order);
  • EPiServer.Commerce.Order.IReturnOrderFormExtensions. Extensions for the return order form.

Extensions

- CanCreateExchangeOrder. Checks whether it can create an exchange order from a return order form.
- IsEditable. Checks whether a return order form is editable.
- IsCancellable. Checks whether a return order form is cancellable.
- IsCompletable. Checks whether a return order form is completable.
- IsAwaitingCompletable. Checks whether a return order form is awaiting completable.
  • EPiServer.Commerce.Order.IReturnOrderService. Handles operations on IReturnOrderForm such as complete, cancel a return, or acknowledge a return's receipt items. Also provides an operation to create an exchange order from a purchase order.
    • AcknowledgeReceiptItems. Acknowledges the receipt of items on a return form.
var returnOrderService = ServiceLocator.Current.GetInstance<IReturnOrderService>();
            var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
            var orderLink = new OrderReference(1, "Default", Guid.NewGuid(), typeof(IPurchaseOrder));
            var purchaseOrder = orderRepository.Load<IPurchaseOrder>(orderLink.OrderGroupId);
            var returnOrderForm = purchaseOrder.ReturnForms.First();
            returnOrderService.AcknowledgeReceiptItems(returnOrderForm);
- CancelReturn. Cancels a return form.  
var returnOrderService = ServiceLocator.Current.GetInstance<IReturnOrderService>();
            var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
            var orderLink = new OrderReference(1, "Default", Guid.NewGuid(), typeof(IPurchaseOrder));
            var purchaseOrder = orderRepository.Load<IPurchaseOrder>(orderLink.OrderGroupId);
            var returnOrderForm = purchaseOrder.ReturnForms.First();
            returnOrderService.CancelReturn(returnOrderForm);

Note that the purchase order was saved but was not recalculated. It needs to be recalculated and saved.

OrderStatusManager.RecalculatePurchaseOrder(purchaseOrder);
             orderRepository.Service.Save(purchaseOrder);
- CompleteReturn. Completes a return form.  
var returnOrderService = ServiceLocator.Current.GetInstance<IReturnOrderService>();
            var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
            var orderLink = new OrderReference(1, "Default", Guid.NewGuid(), typeof(IPurchaseOrder));
            var purchaseOrder = orderRepository.Load<IPurchaseOrder>(orderLink.OrderGroupId);
            var returnOrderForm = purchaseOrder.ReturnForms.First();
            returnOrderService.CompleteReturn(returnOrderForm);

Note that the purchase order and the exchange order (if any) were saved but not recalculated. They need to be recalculated and saved.

OrderStatusManager.RecalculatePurchaseOrder(purchaseOrder);
            orderRepository.Service.Save(purchaseOrder);
            var exchangeOrder = orderRepository.Service.Load<IPurchaseOrder>(purchaseOrder.ExchangeOrderGroupId.Value);
            if (exchangeOrder != null)
              {
                OrderStatusManager.RecalculatePurchaseOrder((PurchaseOrder)exchangeOrder);
                orderRepository.Service.Save(exchangeOrder);
              }
  • CreateExchangeOrder. Creates an exchange order from a purchase order.
var returnOrderService = ServiceLocator.Current.GetInstance<IReturnOrderService>();
        var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
        var orderLink = new OrderReference(1, "Default", Guid.NewGuid(), typeof(IPurchaseOrder));
        var purchaseOrder = orderRepository.Load<IPurchaseOrder>(orderLink.OrderGroupId);
        var exchangeOrder = returnOrderService.CreateExchangeOrder(purchaseOrder, purchaseOrder.GetFirstShipment());

After creating exchange order, if you are using PurchaseOrder (from IPurchaseOrder) and OrderAddress (from IOrderAddress), the address needs to be cloned from the original purchase order to the exchange order.

var purchaseOrder = orderRepository.Load<IPurchaseOrder>(orderLink.OrderGroupId) as PurchaseOrder;
        var exchangeOrder = returnOrderService.CreateExchangeOrder(purchaseOrder, purchaseOrder.GetFirstShipment()) as PurchaseOrder;
        exchangeOrder.OrderAddresses.Clear();
        
        foreach (var orderAddress in purchaseOrder.OrderAddresses)
          {
             var clonedOrderAddress = (OrderAddress)orderAddress.Clone();
             clonedOrderAddress.OrderGroupId = purchaseOrder.OrderGroupId;
             purchaseOrder.OrderAddresses.Add(clonedOrderAddress);
          }

What’s Next