Adding custom field to cart

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
mskurnik
Ensign (ENS)
Ensign (ENS)
Posts: 18
Joined: Mon Nov 01, 2010 12:53 pm

Adding custom field to cart

Post by mskurnik » Mon Dec 27, 2010 12:40 pm

I had to create a custom product page for a client due to some complex variants and I got the product to add to the cart but I cannot seem pass anything to the "Previous CD Key" custom field. Any thoughts?

Code: Select all

protected void AddToCart(int _ProductId)
{
	string PreviousCDKey = PreviousKeyTextBox.Text;
	//GET THE PRODUCT ID FROM THE URL
	Product product = ProductDataSource.Load(_ProductId);
	if (product != null)
	{
		string lastShoppingUrl = NavigationHelper.GetLastShoppingUrl();

		BasketItem basketItem = BasketItemDataSource.CreateForProduct(_ProductId, Convert.ToInt16(QuantityTextBox.Text));
		if (basketItem != null)
		{
			if (product.ProductProductTemplates.Count > 0)
			{
				ProductCustomField pcf = new ProductCustomField();
				pcf.FieldName = "Previous CD Key";
				pcf.FieldValue = "Blah";

				product.CustomFields.Add(pcf);
				product.Save();
			}
			//ADD ITEM TO BASKET
			Basket basket = Token.Instance.User.Basket;
			basket.Items.Add(basketItem);
			basket.Save();
			basketItem.Save();

			List<string> basketMessages;
			if (!basket.Validate(out basketMessages))
			{
				Session.Add("BasketMessage", basketMessages);
				Response.Redirect(NavigationHelper.GetBasketUrl());
			}
			//IF THERE IS NO REGISTERED BASKET CONTROL, WE MUST GO TO BASKET PAGE
			if (!PageHelper.HasBasketControl(this.Page)) Response.Redirect(NavigationHelper.GetBasketUrl());
		}
	}
}

User avatar
jmestep
AbleCommerce Angel
Posts: 8164
Joined: Sun Feb 29, 2004 8:04 pm
Location: Dayton, OH
Contact:

Re: Adding custom field to cart

Post by jmestep » Tue Dec 28, 2010 7:31 am

You would need to use a field related to the basket to store it, like the BasketItem.CustomFields. The product custom field is related to the product itself, not the basket item. You can use a customer templatefield to store the input, which would transfer it to the basket item.
Judy Estep
Web Developer
jestep@web2market.com
http://www.web2market.com
708-653-3100 x209
New search report plugin for business intelligence:
http://www.web2market.com/Search-Report ... -P154.aspx

mskurnik
Ensign (ENS)
Ensign (ENS)
Posts: 18
Joined: Mon Nov 01, 2010 12:53 pm

Re: Adding custom field to cart

Post by mskurnik » Tue Dec 28, 2010 11:42 am

I figured it out. Find out the Template ID that you want to use and change the following:

Code: Select all

Product product = ProductDataSource.Load(_ProductId);
            if (product.ProductProductTemplates.Count > 0)
            {
                BasketItemInput BII = new BasketItemInput();
                BII.InputFieldId = MYTEMPLATEIDGOESHERE;
                BII.InputValue = MYINPUTTEXTBOX.Text;
                basketItem.Inputs.Add(BII);
            }


Post Reply