Different cartitems in shopping cart for same SKU

Atri Dwivedi asked on May 24, 2017 12:00

I am creating a custom shopping cart. Whenever I try to add a new cartitem of same SKU using below line of code

ShoppingCartItemParameters parameters = new ShoppingCartItemParameters(oSKUInfo.SKUID, 1);
parameters.Price = oSKUInfo.SKUPrice;
ShoppingCartItemInfo oShoppingCartItemInfo = oShoppingCartInfo.SetShoppingCartItem(oShoppingCartItemParameters);
ShoppingCartItemInfoProvider.SetShoppingCartItemInfo(oShoppingCartItemInfo);

It increase SKU Unit count, instead of adding a new cart item. But In our scenario we want a different cart item to be added in shopping cart. I am unable to find any solution as of now.

Please help.

Correct Answer

Atri Dwivedi answered on May 31, 2017 16:33

Hi,

Received following reply from Technical kentico support team, Which worked for me. : -

The behavior of ShoppingCartInfo.SetShoppingCartItem method is that it increases quantity of the existing item with same parameters (mainly product options) instead of adding new separate item.
You can work your way around this behavior by using more low level code, something as:
...
            // Create new shopping cart item with initialized item GUID
            ShoppingCartItemInfo item = new ShoppingCartItemInfo();

            // Initialize product properties
            item.SKUID = itemParams.SKUID;
            item.CartItemUnits = itemParams.Quantity;
            item.CartItemPrice = itemParams.Price;

            if ((cart == null) || (item == null))
            {
                return; 
            }

            // Reference item to the shopping cart
            item.ShoppingCartID = cart.ShoppingCartID;
            item.ShoppingCart = cart;

            // Add item to the shopping cart
            cart.CartItems.Add(item);

            // Set the cart item into DB
            ShoppingCartItemInfoProvider.SetShoppingCartItemInfo(item)

            // Invalidate cart calculations
            cart.InvalidateCalculations();
...

It is also possible to override the default behavior od the SetShoppingCartItem method in custom provider, but as you're using custom code to add products to cart anyway, it may not be necessary.
If your item has options, you'd have to do a lot more following actions (save all the option items, process the price changes, etc.), but if you're using standard products without options, it should be sufficient.
If you're using product type Donation, then you may need to do some other steps too. Just let me know if that's the case in your scenario.
0 votesVote for this answer Unmark Correct answer

Recent Answers


Prashant Verma answered on May 24, 2017 12:39

Hi Atri,

Increase in SKU Unit count is default behavior you cannot list a product with same SKUID as different cart item it should always represent the same product in cart item with increased count.

SKU means Stock keeping unit(Wiki). The SKU in Kentico is the product object which represent the quantity of number product's associated with same SKU.

Thanks

Happy to help you

3 votesVote for this answer Mark as a Correct answer

Atri Dwivedi answered on May 24, 2017 14:34 (last edited on May 24, 2017 14:35)

Hi Prashant,

Thanks for your response, I do understand it is a default behavior. But in our case we want to treat it as separate entity. We do not provide option for applying quantity of any SKU unit. Can you help me to suggest any alternate or customized way to implement it.

Thanks in advance.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on May 24, 2017 15:20

Have a look at this article. Although it appears to be for v7, you should be able to modify it (or at least get the idea on how to do it) to work with v8, v9 or v10.

0 votesVote for this answer Mark as a Correct answer

Atri Dwivedi answered on May 24, 2017 17:02

Thanks Brenden, Let me try it once.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on May 31, 2017 16:35 (last edited on May 31, 2017 16:35)

So the article I posted with the upgraded code from Kentico Support worked huh Arti? It's the same code, just working for newer versions of the API.

0 votesVote for this answer Mark as a Correct answer

Atri Dwivedi answered on June 1, 2017 09:47

Hi Brenden, You lead me to right place. But I was not able to figure out to convert it to newer version of Kentico APIs. So at the same time I received a help from support team. Which actually worked for me.

But I really appreciate that you helped from front when I required most. This is what I like about this community which is always ready to help kentico newbies like me.

Thank you so much.

0 votesVote for this answer Mark as a Correct answer

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