CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeff Lynch [MVP]

Everything E-Commerce!

Commerce Server 2007: Development Tip #6 - Adding Items to your Basket

I've revised the code presented in this post after a reader pointed out the (seemingly obvious, but I never saw it) flaw in my code. It's always important to have someone checking your code occasionally. Special thanks to Tim Bond for pointing out this error.

Tip #6 - Adding Items to your Basket

The basic funtionality of the LineItem class has changed little from Commerce Server 2002 and adding an item to your basket works pretty much as it did in the previous version.

The first thing to do is create a new LineItem instance, set the required and optional (weakly-typed) properties and then add the LineItem to the OrderForm's LineItemCollection.

As with every action taken in the Basket, you should always update the Basket and run the appropriate pipeline as shown here.

In Commerce Server 2007, it's also important to remember to dispose of the PipeLineInfo object to ensure that resources are released in a timely manner. Also, since Commerce Server 2007 now supports multiple "named" baskets that are active at the same time, you may need to extend your code to accommodate this new feature.



Comments

Jeff Lynch [MVP] said:

What do I do if the StarterSite doesn't fit my scenario?

# January 4, 2007 8:28 PM

Kevin Burton said:

Can you also post the source to GetBasketPipelineInfo and the pipeline that you are running? Thank you. Kevin
# February 21, 2007 2:04 AM

jlynch said:

Kevin,

I'll edit this post and include the code this afternoon. Ping me via my blog if you need it sooner.

Jeff

# February 21, 2007 8:08 AM

Glen J Fergo said:

Jeff, We are receiving an error on a pretty regular basis. Although I did not code our new site (which uses CS 2007) I am very familiar with CS 2000. My question is this... Is it safe to assume that this check ('If the Basket Doesn't already have an OrderFormCollection') cannot happen in two lines like this? OrderForm dstOf = dst.OrderForms[0]; if (dstOf == null) { } I am guessing this will throw an exception if there are no OrderForms in the OrderGroup. Thanks!
# March 21, 2007 9:56 AM

Tim Bond said:

Unless I am missing something this is a pretty cronic abuse of Dispose(). You have to call Dispose on every instance you create. In this code you create 2 instances one is never Dispose and the other is created just so that it can be disposed. You should also Dispose in the error case to so this should all be wrapped in a try-finally or a using. i.e. // Run the pipeline using (PipelineInfo info = GetBasketPipelineInfo(pipelinename)) { _Basket.RunPipeline(info); // Save the Basket _Basket.Save(); // Disposing of the PipelineInfo object done by the using }
# March 30, 2007 6:06 PM

jlynch said:

Tim,

The Commerce Server 2007 PipelineInfo object is a wrapper around a COM+ dictionary object (a hold over from CS2002) and I believe my code is written correctly but I will check with the Commerce Server team. My understanding of how .NET interacts with COM+ is limited and the details of this object are not fully exposed in the Commerce Server Runtime APIs.

The details on this method can be found at http://msdn2.microsoft.com/en-us/library/microsoft.commerceserver.runtime.orders.pipelineinfo.dispose.aspx

According to the docs:

"Always call Dispose before you release your last reference to the PipelineInfo. Otherwise, the resources it is using will not be freed until the garbage collector calls the PipelineInfo object's Finalize method."

---

Jeff

# March 31, 2007 10:29 AM

TrackBack said:

# May 12, 2007 11:00 AM

GFI said:

Hi Jeff,

I m facing a situation with baskets. Here's the problem:

In current code we are saving basket as Purchase order (bskt.saveAsOrder) after checkout pipeline. But we run into issues if in case there is a communication error from bank. If there's a communication error there is no order saved in Database.

I need to find a way to save this basket before checkout; run checkout pipeline and save it again.

I cant use saveAsOrder twice as it will clear the basket after first save.

Can u suggest any alternative??

Thanks,

GFI

# November 17, 2007 1:43 PM

Padma said:

Hi, I have a problem with the AcceptBasket in the BasKetManager class of OrderManagementContext. When the saveorder value is set to true, the basket is getting saved as purchase order. What should i do to save the basket as just a basket? When the saveOrder is set to false, then nothing is saved in the database. I am using agent services to achive this functionality. Please help. Regards & Thanks Padma
# April 16, 2008 9:35 AM

jlynch said:

Padma,

You don't have to do anything special to update and save a basket, just use the code shown in the post. This persists the basket in the OrderTemplatesAndBaskets table in the <sitename>_transactions database as blob data. When the basket is converted to a PurchaseOrder, it is then persisted to the PurchaseOrders, LineItems, PurchaseOrderPayments, Shipments, OrderForms and OrderAddresses tables.

Jeff

# April 23, 2008 10:06 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

This Blog

Syndication

News