updating BasketItem custom fields

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
dee123
Ensign (ENS)
Ensign (ENS)
Posts: 4
Joined: Thu Aug 12, 2010 2:11 pm

updating BasketItem custom fields

Post by dee123 » Thu Aug 26, 2010 5:45 pm

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();

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: updating BasketItem custom fields

Post by mazhar » Fri Aug 27, 2010 11:58 am

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.

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();
    }
Now you can use this new utility method in following way

Code: Select all

BasketItem dbCurrentBasketItem = BasketItemDataSource.Load(_basketItemID);
SetCustomField(dbCurrentBasketItem, fieldname, b_filename);
Hopefully this will help you.

dee123
Ensign (ENS)
Ensign (ENS)
Posts: 4
Joined: Thu Aug 12, 2010 2:11 pm

Re: updating BasketItem custom fields

Post by dee123 » Fri Aug 27, 2010 1:04 pm

i didn't notice the dictionary structure. thank you -- this helps out alot!

Post Reply