Page 1 of 1

Adding custom field to cart

Posted: Mon Dec 27, 2010 12:40 pm
by mskurnik
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());
		}
	}
}

Re: Adding custom field to cart

Posted: Tue Dec 28, 2010 7:31 am
by jmestep
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.

Re: Adding custom field to cart

Posted: Tue Dec 28, 2010 11:42 am
by mskurnik
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);
            }