Making Customer Fields Required for Product Template

From AbleCommerce Wiki
Jump to: navigation, search

You can make requirement for a customer to enter text into a customer field. Lets suppose you have a product template that has two customer fields where they can enter a personalization like a monogram for example. There is a way to require the customer enter something in that field before adding it to the cart.

In order to accomplish this task edit the following file.

App_Code/ProductHelper.cs 

Locate and replace the BuildProductChoices function with the following method and keep one more thing in your mind that replace the method in case that you haven't customized this method before other wise replace operation will cause the lose of your customized work. In this situation you need to merge this code with your custom code.

public static void BuildProductChoices(Product product, PlaceHolder phChoices)
    {
        // ADD IN THE PRODUCT TEMPLATE CHOICES
        ProductTemplate template = product.ProductTemplate;
        if (template != null)
        {
            foreach (InputField input in template.InputFields)
            {
                if (!input.IsMerchantField)
                {
                    // ADD THE CONTROL TO THE PLACEHOLDER
                    phChoices.Controls.Add(new LiteralControl("<tr><td colspan=\"2\">"));
                    phChoices.Controls.Add(new LiteralControl((input.UserPrompt + "<br />")));
                    WebControl o = input.GetControl();
                    
                    if (o != null)
                    {   
                        phChoices.Controls.Add(o);
                        if (input.InputType == InputType.TextBox)
                        {
                            RequiredFieldValidator rfv = new RequiredFieldValidator();
                            rfv.ID = "rfv" + input.InputFieldId.ToString();
                            rfv.ControlToValidate = o.ID;
                            rfv.Text = "*";
                            rfv.ValidationGroup = "AddToBasket";
                            phChoices.Controls.Add(rfv);
                        }
                    }

                    phChoices.Controls.Add(new LiteralControl("</td></tr>"));
                }
            }
        }
    }

Now when order from store side the customer must have to provide some values for these fields, in other words you just made these fields mandatory and they can't be left blank.

Reference

Originally posted in forums by DFresh http://forums.ablecommerce.com/viewtopic.php?f=47&t=6393