Issue in Custom "Add to Cart" functionality implementation

Divya Mawar asked on May 27, 2016 07:28


In my Kentico 9 application, I am developing my custom functionality for adding an product into Shopping cart.

I am developing things using jQuery Ajax and HTTP handler for it.

In my .ashx handler code, I refereed code from Kentico in built Shopping CartItem Add control code.

Everything is working fine means Item is getting added into shopping cart properly.

Only issue is this that after adding its not reflecting into Shopping cart, means as I clicked on my custom button it called a java script function in which using ajax my .ashx is called and add to shopping cart code logic get execute and return success in json form. now when I close my popup window and click on shopping cart webpart which used on master page header it doesn't get reflect with newly added item, even not after reload the page.

But when I close the browser and again open and relaunch my application on browser and click to shopping cart link on master page, I get the updated product list.

Here I have attached my transformation code from which calling my .ashx and also .ashx code to execute add to shopping cart code logic.

I will request you to review and let me know where I am missing something or doing wrong.

I am new with Kentico and will really appreciate your quick response.

Thanks in advance.

transformation code :

<%@ Register Src="~/CMSModules/Ecommerce/Controls/ProductOptions/ShoppingCartItemSelectorHG2.ascx" TagName="CartItemSelector" TagPrefix="uc1" %> <%@ Register Src="~/CMSAdminControls/ContentRating/RatingControl.ascx" TagName="RatingControl" TagPrefix="cms" %>

    <% // Show pricing details according to saving %> <% if(GetSKUPriceSaving() > 0){ %>
  • <%# "-" + GetSKUPriceSaving(true) + "%" %>
  • <% } %>
" class="btn btn-sm btn-blue above cart-float category-btn wishlist"> Add to Wishlist ">

<div id="add-cart-popup-<%# Eval("SKUID") %>" class="mfp-with-anim mfp-hide mfp-dialog clearfix">

Product Added to Cart

Product has been successfully added to cart.

  • -20%
<img class="product-img-primary" src="<%# GetSKUImageUrl(200,200) %>" alt="<%# EvalText(" SKUName", true) %>" />
<%# LimitLength(EvalText("SKUName", true), 45, "...") %>
was $1500.55 Out For <%# GetSKUFormattedPrice() %>

            <div class="col-xs-12">
                <div class="product-modal-gap"></div>
                <a class="btn btn-block btn-default" href="#"><i class="fa fa-shopping-bag"></i>Continue Shopping</a>
             <a class="btn btn-block btn-blue" href="~/Special-Pages/1-Step-Checkout/Order-Summary"><i class="fa fa-sign-in"></i>Checkout Now</a>

            </div> <!-- End of Column -->   

        </div><!-- End of Row-->


    <button title="Close (Esc)" type="button" class="mfp-close">×</button>

ashx code : 
<%@ WebHandler Language="C#" Class="AddCartDetailHandler" %>

using System; using System.Web; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Collections; using System.Web.UI; using System.Web.UI.WebControls; using System.Linq;

using CMS.Ecommerce; using CMS.EventLog; using CMS.Helpers; using CMS.Membership; using CMS.Base; using CMS.SiteProvider; using CMS.UIControls; using CMS.DataEngine; using System.Text;

using CMS.WebAnalytics;

public class AddCartDetailHandler : IHttpHandler {

bool RedirectToDetailsEnabled = true;
bool ShowProductOptions = false;
bool ShowDonationProperties = false;
int Quantity = 1;

/// <summary>
/// Returns selected shopping cart item parameters containing product option parameters.
/// </summary>
public ShoppingCartItemParameters GetShoppingCartItemParameters(int SKUID)
    // Get product options
    List<ShoppingCartItemParameters> options = new List<ShoppingCartItemParameters>();
    ShoppingCartItemParameters cartItemParams = new ShoppingCartItemParameters(SKUID, Quantity, options);
    return cartItemParams;


/// <summary>
/// Logs activity
/// </summary>
/// <param name="skuId">Product ID</param>
/// <param name="skuName">Product name</param>
/// <param name="quantity">Quantity</param>
private void LogProductAddedToSCActivity(int skuId, string skuName, int quantity)
    Activity activity = new ActivityProductAddedToShoppingCart(skuName, quantity, skuId, AnalyticsContext.ActivityEnvironmentVariables);

// Main Method for add item into cart
public void ProcessRequest(HttpContext context)

    string SKUId = context.Request.QueryString["SKUID"];

    if (SKUId != null)
        int SKUID = Convert.ToInt32(SKUId);
        SKUInfo SKU = SKUInfoProvider.GetSKUInfo(SKUID);
        ShoppingCartInfo ShoppingCart = ECommerceContext.CurrentShoppingCart;
        // Try to redirect before any currently selected variant checks (selector in listings issue)
        if (RedirectToDetailsEnabled)
            if (!ShowProductOptions && !ShowDonationProperties)
                // Does product have some enabled product option categories?
                bool hasOptions = !DataHelper.DataSourceIsEmpty(OptionCategoryInfoProvider.GetProductOptionCategories(SKUID, true));

                // Is product a customizable donation?
                //bool isCustomizableDonation = ((SKU != null)
                //    && (SKU.SKUProductType == SKUProductTypeEnum.Donation)
                //    && (!((SKU.SKUPrice == SKU.SKUMinPrice) && (SKU.SKUPrice == SKU.SKUMaxPrice)) || SKU.SKUPrivateDonation));

        // Get cart item parameters
        ShoppingCartItemParameters cartItemParams = GetShoppingCartItemParameters(SKUID);

        // Get cart item parameters in case something changed
        cartItemParams = GetShoppingCartItemParameters(SKUID);

        // Log activity
        LogProductAddedToSCActivity(SKUID, SKU.SKUName, Quantity);

        if (ShoppingCart != null)
            bool updateCart = false;

            // Assign current shopping cart to current user
            var ui = MembershipContext.AuthenticatedUser;
            if (!ui.IsPublic())
                ShoppingCart.User = ui;
                updateCart = true;

            // Shopping cart is not saved yet
            if (ShoppingCart.ShoppingCartID == 0)
                updateCart = true;

            // Update shopping cart when required
            if (updateCart)

            // Add item to shopping cart
            ShoppingCartItemInfo addedItem = ShoppingCart.SetShoppingCartItem(cartItemParams);

            if (addedItem != null)
                addedItem.CartItemAutoAddedUnits = 0;

                // Update shopping cart item in database

                // Update product options in database
                foreach (ShoppingCartItemInfo option in addedItem.ProductOptions)

                // Update bundle items in database
                foreach (ShoppingCartItemInfo bundleItem in addedItem.BundleItems)

                bool TrackAddToShoppingCartConversion = true;
                if (TrackAddToShoppingCartConversion)
                    // Track 'Add to shopping cart' conversion

                // Recalculate shopping cart

                // If user has to be redirected to shopping cart
                bool RedirectToShoppingCart = true;
                string ShoppingCartUrl = ECommerceSettings.ShoppingCartURL(SiteContext.CurrentSiteName);
                if (RedirectToShoppingCart)
                    // Set shopping cart referrer
                    SessionHelper.SetValue("ShoppingCartUrlReferrer", RequestContext.CurrentURL);

                    // Ensure shopping cart update
                    SessionHelper.SetValue("checkinventory", true);

                    // Redirect to shopping cart
                    //  URLHelper.Redirect(ShoppingCartUrl);

        context.Response.ContentType = "text/plain";
        context.Response.Write("Add Item into Cart");
        context.Response.ContentType = "text/plain";
        context.Response.Write("No item Add");
public bool IsReusable
        return false;


Correct Answer

Anton Grekhovodov answered on May 27, 2016 08:53


You should inherit you class AddCartDetailHandler from interface System.Web.SessionState.IRequiresSessionState, so your class declaration will be:

using System.Web.SessionState;
public class AddCartDetailHandler : IHttpHandler, IRequiresSessionState {
2 votesVote for this answer Unmark Correct answer

Recent Answers

Divya Mawar answered on May 27, 2016 12:41 (last edited on May 27, 2016 13:05)

Thank you for quick reply.

I am calling ashx using ajax and the item show in shopping cart after reloading page. But I need item show in shopping cart without reload page.

So please suggest me How to show item in shopping cart without reload and refresh page.

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on May 27, 2016 12:48

In response on your ajax call you can return json item with ShoppingCartItems and modify html code in javascript.

0 votesVote for this answer Mark as a Correct answer

Divya Mawar answered on May 27, 2016 14:05

Thank you Anton Grekhovodov For your quick response and I will trying this.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.