Temporary cart

Store UI, layout, design, look and feel; Discussion on the customer facing pages of your online store. Cascading Style Sheets, Themes, Scriptlets, NVelocity and the components in the ConLib directory.
Post Reply
User avatar
heinscott
Captain (CAPT)
Captain (CAPT)
Posts: 375
Joined: Thu May 01, 2008 12:37 pm

Temporary cart

Post by heinscott » Mon Nov 17, 2008 8:16 am

Hello... This might be an easy question for you experienced .Net programmers, but, I am scratching my head over this one. Let me explain my situation.
We are trying to create some specialized product pages to walk a customer through building a custom swimming pool. So, for example, in Step 1, you would be choosing a pool size. Step 2, choose a Liner. Step 3, a pump/filter system... etc., etc....
In order to make this work like a kit, we have create a category for each pool, and then sunk the appropriate categories and products that match that pool type in that category. As such, I am able to build out the pages fairly easily to jump from option to option throughout the process.
Here is where I am having a problem...
If I add each item to the customer cart after each step, I can get this whole process to work no problem. My boss, however, would rather have nothing added to the cart, until after the last step is completed, and the customer chooses to add the entire thing to the cart. I am, of course, able to make an new instance of a Basket and populate that, but, I am uncertain how to keep that basket from re-initializing upon each server call.
Any ideas for me? I guess I'm unsure (in general) how to keep any variable current through a server call where the page is reloaded (or partially loaded).
Sorry if this is an obvious question. I would be most appreciative of any help anyone could give me with this!
Thanks,

Scott

User avatar
nickc
Captain (CAPT)
Captain (CAPT)
Posts: 276
Joined: Thu Nov 29, 2007 3:48 pm

Re: Temporary cart

Post by nickc » Mon Nov 17, 2008 8:39 am

Make a singleton class:

Code: Select all

   public class TemporaryBasket
    {
        public static TemporaryBasket Current
        {
            get
            {
                return (Session["TemporaryBasket"] ?? (Session["TemporaryBasket"] = new Basket())) as TemporaryBasket;
            }
        }
        ...
 
So you get hold of the object via "TemporaryBasket mybasket = TemporaryBasket.Current;" and you can add in any extra properties and methods you might need in the class. Session is not the most stable place to store things, but should be adequate for your purpose.

Cheers,

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Temporary cart

Post by mazhar » Mon Nov 17, 2008 8:43 am

You can place a basketid in the query string let say it TempBasketId. Then first try to load the basket using the TempBasketId from query string. In the first step the TempBasketId will be 0 so there will be no basket to load hence you will create a new one, for each next step you will have a valid TempBasketId and all you need to just load the basket and append the next basket item to it.

User avatar
heinscott
Captain (CAPT)
Captain (CAPT)
Posts: 375
Joined: Thu May 01, 2008 12:37 pm

Re: Temporary cart

Post by heinscott » Mon Nov 17, 2008 8:45 am

Thank you both for your speedy replies! I will get started trying it out right now.
Your posts were both very helpful!!!
Thanks again,

Scott

User avatar
heinscott
Captain (CAPT)
Captain (CAPT)
Posts: 375
Joined: Thu May 01, 2008 12:37 pm

Re: Temporary cart

Post by heinscott » Tue Nov 18, 2008 8:42 am

Hey, I just thought I would post this solution in case it is helpful to anyone...
I've found that when trying to pass querystrings on elements of a page that are updated with AJAX, it's helpful to set the querystring, and not redirect manually. As such, .net won't let you set a querystring, as the collection is read-only by default. To get around this, use this code to temporarily set the collection to writeable.

Code: Select all

NameValueCollection QS = Request.QueryString;
        QS = (NameValueCollection)Request.GetType().GetField("_queryString", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Request);
        PropertyInfo readOnlyInfo = QS.GetType().GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
        readOnlyInfo.SetValue(QS, false, null);
After this, you should be able to add whatever you need to the querystring, and have it then available after the AJAX part of the page reloads.

Code: Select all

QS["BasketId"] = _Basket.BasketId.ToString();
        readOnlyInfo.SetValue(QS, true, null);
This solution made it possible to keep the id of my temporary basket as well as the step in the process that the customer is on without reloading the entire page each time.

Thanks again for your help Mazhar and NickC.

Scott

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Temporary cart

Post by mazhar » Tue Nov 18, 2008 8:54 am

Thanks Scott, great tip!

Post Reply