Hi, We are utilizing the BasketItem.CustomFields feature to save additional personalization data the customer provides for us when adding a product to the cart. We need to be able to update the key/string value. I don't see an update method so I've been using the Remove method and then re-adding using Add. This doesn't seem to do the trick since I get an error saying that the 'key already exists. Any thoughts? I couldn't find much documentation on the customFields API.. Judging from the boards, this seems to be a recent feature?
Here's our code:
//check if custom field exists in the database
BasketItem dbCurrentBasketItem = BasketItemDataSource.Load(_basketItemID);
if (dbCurrentBasketItem.CustomFields.ContainsKey(fieldname))
{
dbCurrentBasketItem.CustomFields.Remove(fieldname);
dbBasketItem.Save();
}
//now re-add the updated key/string combo
item.CustomFields.Add(fieldname, b_filename);
item.Save();
updating BasketItem custom fields
Re: updating BasketItem custom fields
Well basket custom fields actually makes use of dictionary structure. We don't have any explicit update method for dictionary name value pairs. You can write a small helper function in your code to make it easy. Have a look at the code provided below. You can see a new method called SetCustomField, just add this method to your code file and then use this method for both add/update operations on custom field by passing it a basket item, name of field to be added/updated and finally the value for custom field.
Now you can use this new utility method in following way
Hopefully this will help you.
Code: Select all
protected void SetCustomField(BasketItem basketItem,string key,string value)
{
//check if custom field already exists in custom fields then update the value
//or add a new custom field and save basket item
if (basketItem.CustomFields.ContainsKey(key))
basketItem.CustomFields[key] = value;
else
basketItem.CustomFields.Add(key, value);
basketItem.Save();
}
Code: Select all
BasketItem dbCurrentBasketItem = BasketItemDataSource.Load(_basketItemID);
SetCustomField(dbCurrentBasketItem, fieldname, b_filename);
Re: updating BasketItem custom fields
i didn't notice the dictionary structure. thank you -- this helps out alot!