Kentico 11: Shopping Cart Binding Issue

ANKIT MISTRY asked on March 30, 2018 09:21

Hello,

We have just upgraded our site to Kentico 11 from Kentico 8.2 but we are facing issue in adding items into shopping cart, please see below code reference and error details:

Please see below screenshot of Code, it also has the commented code we were using for Kentico 8.2 which is not giving any error but not binding the shopping cart, So, we have put the new code as per given into Kentico 11 documentation:

private void bindCart()
{
    try
    {
        ShoppingCartInfo ShoppingCartInfoObj = ECommerceContext.CurrentShoppingCart;
        if (ShoppingCartInfoObj.ShoppingCartID == 0)
        {
            ShoppingCartInfoProvider.SetShoppingCartInfo(ShoppingCartInfoObj);
        }
        ShoppingCartItemInfo itemInfo = new ShoppingCartItemInfo();
        itemInfo.CartItemID = ShoppingCartInfoObj.CartItems.Count + 1;
        itemInfo.CartItemText = "Simonly";
        itemInfo.CartItemUnits = 1;
        //OLD CODE USED FOR KENTICO 8.2
        //ShoppingCartInfoObj.CartItems.Add(itemInfo);
        //ShoppingCartInfoProvider.SetShoppingCartInfo(ShoppingCartInfoObj);
        //OLD CODE FOR KENTICO 11
        ShoppingCartItemParameters parameters = new ShoppingCartItemParameters(itemInfo);
        ShoppingCartItemInfo cartItem = ShoppingCartInfoProvider.SetShoppingCartItem(ShoppingCartInfoObj, parameters);
        ShoppingCartItemInfoProvider.SetShoppingCartItemInfo(cartItem);
    }
    catch (Exception ex)
    {
        EventLogProvider.LogException(this.GetType().Name, MethodBase.GetCurrentMethod().Name, ex);
    }
}

Below is the screenshot of the error we are getting, "Value cannot be null, Parameter name: itemObj"

what we find here error is from code "ShoppingCartItemInfoProvider.SetShoppingCartItemInfo(cartItem);" because from

"ShoppingCartItemInfo cartItem = ShoppingCartInfoProvider.SetShoppingCartItem(ShoppingCartInfoObj, parameters);" cartItem is remain null.

Can any one please help.

Recent Answers


Peter Mogilnitski answered on March 30, 2018 11:40 (last edited on March 30, 2018 12:11)

Did you follow the example for versision 11? ShoppingCartItemInfo is not initalized correctly, it must be based on a product. otherwise you are adding to the shopping cart an entity which is not connected to any sku/product

// Gets a product to add to the shopping cart
SKUInfo product = SKUInfoProvider.GetSKUs()
                                    .WhereEquals("SKUName", "NewProduct")
                                    .WhereNull("SKUOptionCategoryID")
                                    .FirstObject;

if (product != null)
{
    // Gets the current shopping cart
    ShoppingCartInfo cart = ECommerceContext.CurrentShoppingCart;

    // Creates the shopping cart in the database if it does not exist yet
    if (cart.ShoppingCartID == 0)
    {
        ShoppingCartInfoProvider.SetShoppingCartInfo(cart);
    }

    // Prepares a shopping cart item representing 1 unit of the product
    ShoppingCartItemParameters parameters = new ShoppingCartItemParameters(product.SKUID, 1);
    ShoppingCartItemInfo cartItem = ShoppingCartInfoProvider.SetShoppingCartItem(cart, parameters);

    // Saves the shopping cart item to the shopping cart
    ShoppingCartItemInfoProvider.SetShoppingCartItemInfo(cartItem);

    // Evaluates and recalculates the shopping cart (discounts, shipping, price totals, etc).
    cart.Evaluate();
}
0 votesVote for this answer Mark as a Correct answer

ANKIT MISTRY answered on March 30, 2018 11:59

Hello Peter,

Thank you for your response!

Yes, the new code for Kentico 11 i got from this reference only. The only difference is that in reference for ShoppingCartItemParameters parameters = new ShoppingCartItemParameters(product.SKUID, 1); they have passed SKUID and Quantity. But my need is to pass ShoppingCartItemInfo object which it is accepting.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on March 30, 2018 13:57

The difference is that they get ShoppingCartItemInfo based on SKUID, you create uninilitized ShoppingCartItemInfo and is not connected to any product(SKU). Yes your ShoppingCartItemInfo is accepted at the compile time because ShoppingCartItemInfoProvider.SetShoppingCartItemInfo() requires type ShoppingCartItemInfo, but at the run time it will crush. You have to connect your shopping cart item to SKU.

0 votesVote for this answer Mark as a Correct answer

ANKIT MISTRY answered on April 2, 2018 07:55

Hello Peter,

Yes, now i am able to add Items into Shopping Cart now using SKU. But now I am facing issue in ShoppingCartItemInfo object. After item added to shopping cart, to show item on shopping cart i have written code like:

ShoppingCartInfo ShoppingCartInfoObj = ECommerceContext.CurrentShoppingCart; foreach (ShoppingCartItemInfo objShoppingCartItemInfo in ShoppingCartInfoObj.CartItems) { string cartItemTxt = objShoppingCartItemInfo.CartItemText; }

Here, i am getting object of Shopping Cart and its items but i am not getting values from the field which i have set at the time of adding items to shopping cart.

Please let me know if any confusion.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on April 3, 2018 01:21 (last edited on April 3, 2018 01:23)

You have to consult the API. I think you are trying to list items in the shopping cart:

CartItemText will not work, because it is null. It is set only when cart item represents text product option. You have to list products in the shopping cart, i.e. list of SKUs, it should be something like this:

ShoppingCartInfo ShoppingCartInfoObj = ECommerceContext.CurrentShoppingCart; 
foreach (ShoppingCartItemInfo objShoppingCartItemInfo in ShoppingCartInfoObj.CartItems) 
{ 
    string cartItemName = objShoppingCartItemInfo.SKU.SKUName ; 
}
0 votesVote for this answer Mark as a Correct answer

ANKIT MISTRY answered on April 3, 2018 07:15

Hello Peter,

Thank you for you replay.

I have many custom field added into ShoppingCartItem which i am setting and getting using GetValue/SetValue, and i am not getting blank for those fields as well. Which was working fine till i was on Version 8.2. I tried as per you mentioned and it is working but i am worry about the custom fields which i have added into ShoppingCartItem, do i need to add those fields into SKU?

Please advise.

0 votesVote for this answer Mark as a Correct answer

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