Clone a purchase order and convert into cart
We have a requirement for our client that
he can masquerade user and can create a new cart based on some
existing Purchase Order(e.g. for damaged/lost orders).
Option 1: Get The OrderForm and other
objects from PurchaseOrder and assign it to new Cart.
It did not work as result was a nasty output. On saving Purchase Order for new cart, OrderForm from Parent was assigned to new cart and parent Purchase order lost its associated OrderForms.
It did not work as result was a nasty output. On saving Purchase Order for new cart, OrderForm from Parent was assigned to new cart and parent Purchase order lost its associated OrderForms.
Option 2:
Clone the Purchase Order and create a cart
based on that but it will not work because. (I tested this with EpiServer Commerce R2SP2)
Type
'Mediachase.Commerce.Orders.PurchaseOrder' in Assembly 'Mediachase.Commerce,
Version=5.2.628.0, Culture=neutral, PublicKeyToken=6e58b501b34abce3' is not
marked as serializable.
Solution:
We can clone OrderForms and OrderAddresses. Therefore I created a clone copy of OrderForm and order addresses
and added that into the Cart.
Below piece of code helped me
to achieve this. Note I have not tested/run workflows.
Cart mycart = OrderContext.Current.GetCart("replacementcart", SecurityContext.Current.CurrentUserId);
CopyCart(mycart, Order);
//Delete and added a new Payment(Zero Charged)
//Convert into Purchase Order
//Convert into Purchase Order
/// <summary>
/// Copy cart
/// </summary>
/// <param
name="_cart"></param>
/// <param
name="orderGroup"></param>
public static void CopyCart(Cart _cart, PurchaseOrder orderGroup)
{
//
Initial validation
if (_cart == null) throw new ArgumentNullException("_cart");
if (orderGroup == null) throw new ArgumentNullException("orderGroup");
// need
to set meta data context before cloning
MetaDataContext.DefaultCurrent = OrderContext.MetaDataContext;
OrderForm of = orderGroup.OrderForms[0].Clone() as OrderForm;
//
Remove existing Order Forms
for (int i = _cart.OrderForms.Count-1 ; i>=0;i--)
{
_cart.OrderForms[i].Delete();
}
//Add
order Forms to basket from PurchasOrder.
_cart.OrderForms.Add(of);
//
Remove existing Order Addresses
for (int i = _cart.OrderAddresses.Count-1 ; i>=0;i--)
{
_cart.OrderAddresses[i].Delete();
}
foreach (OrderAddress address in orderGroup.OrderAddresses)
{
MetaDataContext.DefaultCurrent = OrderContext.MetaDataContext;
OrderAddress oa = address.Clone() as OrderAddress;
_cart.OrderAddresses.Add(oa);
}
_cart.SetParent(_cart);
_cart.AddressId =
orderGroup.AddressId;
_cart.AffiliateId = orderGroup.AffiliateId;
_cart.ApplicationId =
orderGroup.ApplicationId;
_cart.BillingCurrency =
orderGroup.BillingCurrency;
_cart.CustomerId =
orderGroup.CustomerId;
_cart.CustomerName =
orderGroup.CustomerName;
_cart.ProviderId = orderGroup.ProviderId;
_cart.Status = orderGroup.Status;
_cart.Owner = orderGroup.Owner;
_cart.OwnerOrg =
orderGroup.OwnerOrg;
_cart.ShippingTotal =
orderGroup.ShippingTotal;
_cart.SiteId = orderGroup.SiteId;
_cart.SubTotal =
orderGroup.SubTotal;
_cart.TaxTotal =
orderGroup.TaxTotal;
_cart.Total = orderGroup.Total;
_cart.AcceptChanges();
}
Great post! I really found this other link helpful. Refer to it for more creating invoices and information on purchase orders
ReplyDeleteCreate a purchase order