Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Create an order return

Vote:
 

Hi,

EPiServer CMS: 11.3.3

EPiServer Commerce: 11.8.1

I'm trying to create a return on an order and have found this, https://world.episerver.com/documentation/developer-guides/commerce/orders/using-the-return-order-form/, but it only shows every step in isolation and I can't seem to get them to work together. I can create the return form but line items are not included. The code I have tried with is the following,

public void CreateReturn(IPurchaseOrder order, IEnumerable lineItems)
{
	var orderRepository = ServiceLocator.Current.GetInstance();
	var returnOrderService = ServiceLocator.Current.GetInstance();
	var purchaseOrderFactory = ServiceLocator.Current.GetInstance();
	var returnOrderForm = purchaseOrderFactory.CreateReturnOrderForm(order);
	var returnShipment = purchaseOrderFactory.CreateReturnShipment(order.GetFirstShipment());
	foreach (var lineItem in lineItems)
	{
		var returnLineItem = purchaseOrderFactory.CreateReturnLineItem(lineItem, lineItem.Quantity, "Faulty") as LineItem;
		returnLineItem.ExtendedPrice = returnLineItem.ReturnQuantity * (lineItem.GetExtendedPrice(order.Currency).Amount / lineItem.Quantity);
		lineItem.ReturnQuantity = lineItem.Quantity;
		returnShipment.LineItems.Add(returnLineItem);
	}
	((OrderForm)returnOrderForm).Total = order.GetFirstForm().GetTotal(order.Market, order.Currency).Amount;
	((OrderForm)returnOrderForm).DiscountAmount = 0;
	((OrderForm)returnOrderForm).ShippingTotal = 0;

	returnOrderForm.Shipments.Add(returnShipment);
	order.ReturnForms.Add(returnOrderForm);
	returnOrderForm.Status = ReturnFormStatus.AwaitingCompletion;
	orderRepository.Save(order);
}

But like I said it only creates the return but without any line items. Any pointers to what I'm missing?

/Viktor

#188247
Edited, Feb 15, 2018 14:29
Vote:
 

Ok, if I add

((OrderForm)returnOrderForm).AcceptChanges();

in the last step it works. Is that the way to do it? Feels a little Mediachase old school :)

/Viktor

#188248
Feb 15, 2018 15:00
Vote:
 

I might be missing something, but the general advice is to not have to cast back to the concrete implementations. For example I'm not sure why this is needed:

	((OrderForm)returnOrderForm).Total = order.GetFirstForm().GetTotal(order.Market, order.Currency).Amount;
	((OrderForm)returnOrderForm).DiscountAmount = 0;
	((OrderForm)returnOrderForm).ShippingTotal = 0;

Also this might not be correct (your might mean returnLineItem.Quantity?)

		lineItem.ReturnQuantity = lineItem.Quantity;
#188252
Feb 15, 2018 17:52
Vote:
 

Hi,

The code is taken from the article I linked to and from reflecting the code used in the commerce manager. Without the lines of code (more correct and not "test" code as I posted above) all totals will be 0 on the returns tab in Commerce Manager. Do you have an example code that just creates a return for one line item in the way it should look?

/Viktor

#188259
Feb 16, 2018 9:26
Vote:
 

The part of creating the return order form is OK (although I'd recommend to use constructor injection instead of servicelocator ). The part of casting back to OrderForm is not OK IMO. I can't look it up now (on vacation actually), but there should be APIs to validate and calculate the total and such.

IPurchaseOrderProcessor.ProcessOrder, maybe? 

#188264
Feb 16, 2018 11:14
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.