Skip to content

Instantly share code, notes, and snippets.

@netsi1964
Created February 1, 2014 09:24
Show Gist options
  • Save netsi1964/8750028 to your computer and use it in GitHub Desktop.
Save netsi1964/8750028 to your computer and use it in GitHub Desktop.
Dynamicweb ASPX C# code to return Cart TotalPrice in JSON
<%@ Page Language="cs" AutoEventWireup="false" %>
<%
/* SHO 01/02/2014 10.21: Returns a JSON object containing JSON object with info of Cart TotalPrice formated as 2.999,95
If no cart exists it contains -1
It also has a soloution based counter which can be used to get a unique number.
It sets cache information in header to avoid client side caching of response.
Place it in /Admin/Public/ or use it in Razor
*/
// Clear any previous reponse, importend in Razor scenaries
HttpContext.Current.Response.Clear();
// Avoid caching of this response
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public);
HttpContext.Current.Response.Cache.SetMaxAge(new TimeSpan(0, 0, 0));
// Set the response to be JSON
HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.AddHeader("Content-type", "text/json");
HttpContext.Current.Response.AddHeader("Content-type", "application/json");
var cart = "-1";
Application["BasketChanged"] = Dynamicweb.Base.ChkInteger(Application["BasketChanged"])+1;
if (System.Web.HttpContext.Current.Session["eComCart"]!=null)
{
Dynamicweb.eCommerce.Orders.Order order = (Dynamicweb.eCommerce.Orders.Order) System.Web.HttpContext.Current.Session["eComCart"];
// Format like this: 1.299,95
cart = String.Format("{0:0,0.00}", order.TotalPrice);
}
%>
{"TotalPrice":"<%=cart%>","UniqueInt":<%=Application["BasketChanged"]%>}
<%
HttpContext.Current.Response.End();
%>
<!-- SHO har rettet den 06/02/2014 08.50: This is at test -->
@netsi1964
Copy link
Author

You can also use the code in Razor without too much change to the code.

ASPX code block

<%
  CODE BLOCK
%>

Razor code block

@{
  CODE BLOCK
}

ASXP Inline code

<%=YourValue%>

Razor inline code

@YourValue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment