*NEW* AJAX CART USING UPDATEPANELS (NO JS OR JQUERY)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
As some of you may know, I developed an Ajax enabled cart using jQuery. I love the functionality of it - I love being able to add items to the cart without having to leave the page and have the cart update without the whole page being refreshed - but the whole thing is so complex that it is hard to implement (and harder to explain to people). That being the case I decided to figure out how to do the same thing in a MUCH SIMPLER way - enter update panels.

In my previous Ajax cart I used a lot of JavaScript and jQuery to circumvent the code that is already in nopCommerce and recreate its functionality in a different way- basically I was reinventing the wheel. This version there is NO JavaScript or jQuery, just some slight changes to the code and some minor additions. This version fits a whole lot better into the existing framework and uses the code that is already there for everything so this version supports everything that nopCommerce has to offer - product attributes, gift cards, etc.

Here it is:

List of pages that need to be updated/changed:
Root.Master
OneColumn.Master/TwoColumn.Master/ThreeColumn.Master
ProductBox1.ascx/ProductBox2.ascx
ProductVariantsInGrid.ascx/OneVariant.ascx

The first step is the change the ScriptManager. The ScriptManger is what allows scripts to be run (such as JavaScripts, jQuery, etc.) on the website as well as allowing other Ajax components (such as an UpdatePanel) to work. By default, the ScriptManager code can be found in six files (excluding administration files), they are:

CheckoutOnePage.ascx
OneVariant.ascx
VariantsInGrid.ascx
ForumPostEdit.ascx
Account.aspx
PrivateMessages.ascx

Deletions

Because we're going to be adding a script manager to the root.master file we need to delete (or comment out) the script manager in the pages listed above. You can only have one script manager per page and since we'll be adding one to the master it will already be there. The code to delete looks like this:

<ajaxToolkit:ToolkitScriptManager runat="server" ID="sm1" EnableScriptGlobalization="true" EnableScriptLocalization="true" ScriptMode="Release" CompositeScript-ScriptMode="release"/>


Root.Master

Next we add the script manager to root.master, right after "<div class="master-wrapper-content"> place this code:

<ajaxToolkit:ToolkitScriptManager runat="server" ID="smRoot" EnableScriptGlobalization="true" EnableScriptLocalization="true" ScriptMode="Release" CompositeScript-ScriptMode="release"/>


Next we are going to add an update panel that will update the header so we can see the number of items in the cart and wishlist change in the header. Right after the script manager code do this:

Locate this code:
<nopCommerce:Header ID="ctrlHeader" runat="server"/>

And replace it with this code:

<asp:UpdatePanel runat="server" ID="HeadPanel" UpdateMode="Conditional">
   <ContentTemplate>
      <nopCommerce:Header ID="ctrlHeader" runat="server"/>
   </ContentTemplate>
</asp:UpdatePanel>


Now we go to the code behind file for root.master (Root.Master.cs) and register the ContentPlaceHolder "cph1" as an asynchronous postback control. Add this code to the "protected override void OnLoad" method:
smRoot.RegisterAsyncPostBackControl(cph1);

This will allow all controls (buttons) in that ContentPlaceHolder to be able to update the update panels using the .Update() method.

Next we move to the designer file for root.master (Root.Master.designer.cs) and change the update panel from "protected" to "public", because we want to be able to access it from other pages in order to update it.

OneColumn/TwoColumn/ThreeColumn.Master

We are going to add another update panel to the other master files. If you're only going to be using one of these for your site then you only need to update that particular page. If you plan on having a mix, then you may want to update all of the master files.

Locate the code that looks like this:
<nopCommerce:MiniShoppingCartBox ID="ctrlMiniShoppingCartBox runat="server"/>

And replace it with this:

<asp:UpdatePanel runat="server" ID="CartPanel" UpdateMode="Conditional">
   <ContentTemplate>
      <nopCommerce:MiniShoppingCartBox ID="ctrlMiniShoppingCartBox runat="server"/>
   </ContentTemplate>
</asp:UpdatePanel>


Next we go to the desiger file for whichever master file you're updating and change "CartPanel" from "protected" to "public" so we can access it from other pages to update it.

ProductVariantsInGrid.ascx.cs and OneVariant.ascx.cs

Add this to the "using" section of these pages:
using NopSolutions.NopCommerce.Web.MasterPages;

This will allow us to see the master pages so we can update the panels on them.

Next we add the update code that will update the panel. You must find the code that says:
Response.Redirect("~/shoppingcart.aspx")

And replace it with this:

((OneColumn)Page.Master).CartPanel.Update();
((TwoColumn)Page.Master).CartPanel.Update();
((ThreeColumn)Page.Master).CartPanel.Update();
((root)Page.Master.Master).HeadPanel.Update();

You only need to add update code for whichever master page(s) you are using.

You can skip this next step if you don't want stuff added to the wishlist this way or if you don't use the wishlist of course.

Next find the code that says:
Response.Redirect("~/wishlist.aspx")

And replace it with this:
((root)Page.Master.Master).HeadPanel.Update();


ProductBox1.ascx.cs and ProductBox2.ascx.cs

Add this to the "using" section of these pages:
using NopSolutions.NopCommerce.Web.MasterPages;

This will allow us to see the master pages so we can update the panels on them.

Next we add the update code that will update the panel. You must find the code that says:
Response.Redirect("~/shoppingcart.aspx")

And replace it with this:

((OneColumn)Page.Master).CartPanel.Update();
((TwoColumn)Page.Master).CartPanel.Update();
((ThreeColumn)Page.Master).CartPanel.Update();
((root)Page.Master.Master).HeadPanel.Update();

You only need to add update code for whichever master page(s) you are using.

We now have an Ajax cart! When the user clicks on the "Add to Cart" button the item will be added to their cart and the mini cart box will be updated along with the number at the top of the screen - all without leaving the page or refreshing the whole page.

Hope this is less confusing than my other Ajax cart solution.

Andrei,

How about adding this to nopCommerce 1.90? Configurable, disabled by default??

C'mon Andrei, what do you say???
13 years ago
You can view this code in action at:

http://www.mynewcomicshop.net/
13 years ago
Ajax Cart looks great Barry,
thanks for sharing it with everybody here....
13 years ago
very nice. thanks for sharing.
13 years ago
Really like this, think its something I will implement in my store.

Maybe something that could just be implemented as standard in the next nopCommerce release? Dont see why anyone wouldnt want this as it improves the customer experience.

Dave.
13 years ago
Fantastic contribution. Easy to read documentation

Great job!

:)
13 years ago
Hi

I have implemented this code but when i press the add to cart button the text in my shopping cart doesnt update to reflect the added item unless i reload the page by clicking on the shopping cart link, it seems that the text in the mini shopping cart in my two column master page will not update until a postback occurs, after clicking a link the mini cart is updated.

has anyone else encountered this problem, does anyone have any idea where i could be going wrong?

Kind Regards

Liam Watson
13 years ago
Are you still having problems watto?

I was wonding if anyone thinks that this needs some sort of customer notification that something was added to the cart. I know the cart total (and items if they are shown) are updated as well as the shopping cart number at the top, but should there be a pop-up or some sort of effect to let people know something went into their cart?

Prestashop has a little box that flies to the cart before the cart changes... I don't like that too much. But I think maybe there needs to be something.
13 years ago
By the way, I refreshed the database - can't remember why, but I deleted the old one and created a new one and ran the install script from the install directory without replacing the any of the files and it worked just fine. That was pretty amazing. :)
13 years ago
bfranklin825 wrote:

I was wonding if anyone thinks that this needs some sort of customer notification that something was added to the cart. I know the cart total (and items if they are shown) are updated as well as the shopping cart number at the top, but should there be a pop-up or some sort of effect to let people know something went into their cart?


A simple way I've handled this in the past (non-nop stuff) was to DOM insert some text that says "This item has been added to your cart" below (or above) the add to cart button they clicked...  it's very quick and easy to do and you can be fairly certain the customer will see it.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.