API Questions on Kentico API.
Version 6.x > API > Ecommerce - product multiple times in the cart View modes: 
User avatar
Certified Developer 9
Certified Developer 9
Petr Dvorak - 6/4/2012 4:35:42 PM
   
Ecommerce - product multiple times in the cart
Hello,

we are developing shop offering products with variable attributes.
User can add to the shopping cart a product and based on various conditions system appends custom data to the cart item. For example, user adds to the cart "Test product" and system (based on context) ads to the cart item flags such as "bought from homepage", "bought from eshop", "can get bonus" etc.

We are using custom "add to cart" form using ShoppingCartInfo.SetShoppingCartItem method. Our problem is, that we can not add to the cart one product multiple times -- with different custom data (flags), because the method only updates the quantity of already present product (and overwrites its custom data).

Although this is usually desired behavior, we need to be able to have "Test product [custom data 1]" and "Test product [custom data 2]" separately in the shopping cart at the same time (i.e. one product multiple times in the cart).

Is it possible? Which attributes are used to compare current cart items with the new cart item (is it possible to change same attribute in ShoppingCartItemParameters to create a new "unique" cart item which will not be merged with existing one)?

Thank you for you suggestions.

Best regards,

Petr Dvorak

User avatar
Kentico Support
Kentico Support
kentico_zdenekc - 6/8/2012 10:51:02 AM
   
RE:Ecommerce - product multiple times in the cart
Hi,

Some customization will be needed. You could either try to find the same product in the shopping cart before adding another one into the cart, read its CustomData and merge them with the newly added item CustomData. The item will then just increase the item count and overwrite the data, but now with preserved previous data. You would just need to parse the CustomData to correctly update/merge them.

You have also a good point when you write about ShoppingCartItemParameters.
The GetShoppingCartItemInternal method used inside the below code checks for the same SKUID, and if it's not a Bundled product, then it checks if the product has some options and if they match with the existing item in the cart.

Below is the part of the code of ShoppingCartInfoProvider.SetShoppingCartItemInternal method:

...
// Try to find specified product with such product options in cart
ShoppingCartItemInfo product = GetShoppingCartItemInternal(cart, itemParams);

// Update product when it is not donation or product with text option or bundle item
if ((product != null) && (product.SKUObj != null)
&& (product.SKUObj.SKUProductType != SKUProductTypeEnum.Donation)
&& !ShoppingCartItemInfoProvider.IsProductWithTextOption(product))
{
UpdateShoppingCartItemInternal(product, itemParams);
}
// Add product
else
{
product = AddShoppingCartItemInternal(cart, itemParams);
}
...


A customization of the code can be made either in the comparison in GetShoppingCartItemInternal method (here you would return null if there's a same product with different CustomData)
or in the UpdateShoppingCartItemInternal or AddShoppingCartItemInternal methods.

Some examples of customization of similar methods can be found e.g. in the 6.0 website project in ~\CMSModules\Ecommerce\Samples\CustomShoppingCartInfoProvider.cs
and explanations in the Ecommerce new customization model in version 6.0 blogpost.

It would be better to discuss more details in the direct email conversation, so feel free to contact us at our support email address. We can communicate in Czech too :)

Regards,
Zdenek