Adding custom field to cart
Posted: 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());
}
}
}