Posts Tagged ‘post order’

After placing order, there are many cases where the order modification may happen. Commerce server allows us to modify the purchase order but make sure you don’t allow any modifications of purchase after some stage. For example, don’t allow purchase order modification once the ordered products are dispatched. In this article, I am explaining how to remove items from purchase order.

Here is the code snippet used to simulate modifications to an order.

OrderContext OrderContext = CommerceContext.Current.OrderSystem;
ProfileContext profileContext = CommerceContext.Current.ProfileSystem;
Profile userProfile = profileContext.GetProfile(“GeneralInfo.email_address”, “ravi@ava.com”, “UserObject”);
string userId = new Guid().ToString();
if (userProfile != null)
{
       userId = userProfile["GeneralInfo.user_id"].Value.ToString();
}
else
{
       Response.Write(“Login Failed”);
}

OrderGroupCollection ogCollection= OrderContext.GetPurchaseOrdersForUser(new Guid(userId));
OrderGroup og = ogCollection[0];
PurchaseOrder po = OrderContext.GetPurchaseOrder(new Guid(userId), og.OrderGroupId);
Response.Write(“Item Total Before removing item:” + po.OrderForms[0].Total.ToString());
po.OrderForms[0].LineItems.Remove(0);
PipelineInfo pipeLineInfo = new PipelineInfo(“basket”);
po.RunPipeline(pipeLineInfo);
pipeLineInfo = new PipelineInfo(“total”);
po.RunPipeline(pipeLineInfo);
po.Save();
Response.Write(“Item Total After removing item:” + po.OrderForms[0].Total.ToString());

 

For example, I created an order with three products and order total is coming to 221$ (as shown in the figure below.)

Purchase Order before removing item.

In the code, I am taking the order of the user, removing first item from the order, running basket and total pipeline.  Note: If you don’t execute pipelines the code will not give any error but the totals will be wrong.

After executing the code the order details are shown below.

Purchase Order after removing item.

 

A product of price 9$ is removed from the order and the current total is decreased to 212.40$.

Related Articles


If you like this post, please click on our sponsor advertisement.


Follow me on Google+
Add to circles

In 0 people's circles

Sign up for Newsletter