Clone a Purchase Order or Convert into Cart

Vote:
 

How can i clone a purchase order Or How can i convert an existing purchase order into Cart?

#70141
Apr 12, 2013 13:38
Vote:
 

PurchaseOrder purchaseOrder = null;//Get somehow Cart retVal = null; // Clone the object // need to set meta data context before cloning MetaDataContext.DefaultCurrent = OrderContext.MetaDataContext; PurchaseOrder clonedPurchaseOrder = (PurchaseOrder)purchaseOrder.Clone(); clonedPurchaseOrder.SetParent(clonedPurchaseOrder); // Second: create new cart retVal = (Cart)OrderContext.Current.ShoppingCartClassInfo.CreateInstance(); // Third: migrate order group retVal.Initialize(clonedPurchaseOrder);

    You will want to rerun the workflows on the cart to properly calcuate totals again incase somethint has chnaged.  I would also delete the payments on the cart before manipulating.

#70181
Apr 12, 2013 22:44
Vote:
 

Thanks Mark.

Ware are using Commerce Server R2SP2

Type 'Mediachase.Commerce.Orders.PurchaseOrder' in Assembly 'Mediachase.Commerce, Version=5.2.628.0, Culture=neutral, PublicKeyToken=6e58b501b34abce3' is not marked as serializable.

further ShoppingCartClassInfo and Initiaze are internal Functions.

#70210
Apr 15, 2013 10:33
Vote:
 

Thanks.
This worked for me 
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();
}

#70213
Apr 15, 2013 11:40
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.