Page 1 of 1

Making Customer Fields Required for Product Template?

Posted: Fri May 02, 2008 12:38 pm
by DFresh
Is there a way to make it a requirement for a customer to enter text into a field? For instance you have a product template that has two customer fields where they can enter a personalization like a monogram for example. Is there anyway to require the customer enter something in that field before adding it to the cart?

Thanks in advance!

Re: Making Customer Fields Required for Product Template?

Posted: Fri May 02, 2008 12:45 pm
by compunerdy
:lol: Literally 5 sec ago I ran into the same issue.

Re: Making Customer Fields Required for Product Template?

Posted: Tue May 06, 2008 8:49 am
by m_plugables
Edit the App_Code/ProductHelper.cs file and locate the following function

Code: Select all

public static void BuildProductChoices(Product product, PlaceHolder phChoices)
and make it look like

Code: Select all

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>"));
                }
            }
        }
    }

Re: Making Customer Fields Required for Product Template?

Posted: Tue May 06, 2008 10:43 am
by DFresh
Thank you so much... That worked perfectly!

Re: Making Customer Fields Required for Product Template?

Posted: Tue May 06, 2008 12:09 pm
by calvis
Hi Mazhar,

Have you been making any progress with your Paypal button? I am sure lots of people would like to thank you monetarily for all the great advice that you have been providing the Able users. You're a great asset to community.

-Charles

Re: Making Customer Fields Required for Product Template?

Posted: Mon May 12, 2008 8:30 am
by jmestep
Here is his Paypal email address --- change at to @ plugablesatgmail.com.

Re: Making Customer Fields Required for Product Template?

Posted: Wed Jul 09, 2008 10:19 am
by dnoell
Thank you for the help with making the information required. I also added a message to left the user know why their item was not submitted to their basket. It is as follows:

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";
rfv.ErrorMessage = "Please enter " + input.Name ;
phChoices.Controls.Add(rfv);
}
}

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


I was wondering how hard it would be to modify the code where the Product Templates are set up to indicate if the items is required or not and then have this information passed to this subroutine?

Thanks for any help.

Re: Making Customer Fields Required for Product Template?

Posted: Mon Aug 25, 2008 11:32 am
by voir
dnoell wrote: I was wondering how hard it would be to modify the code where the Product Templates are set up to indicate if the items is required or not and then have this information passed to this subroutine?

Thanks for any help.
After some trial and error: Create a Merchant field named RequiredFieldsList of type CheckBoxList with the choices being the Product Template Customer field names.

Prepend a second loop to get the Required Fields, I use ArrayList so I had to add

Code: Select all

using System.Collections;
to the top of the file

Code: Select all

    public static void BuildProductChoices(Product product, PlaceHolder phChoices)
    {
        // ADD IN THE PRODUCT TEMPLATE CHOICES
        ProductTemplate template = product.ProductTemplate;
        
        if (template != null)
        {
            ArrayList requiredFieldsList = new ArrayList();
            //To use ArrayList add using System.Collections; in the using area
            if (product != null)
            {
                foreach (ProductTemplateField tf in product.TemplateFields)
                {
                    if (!string.IsNullOrEmpty(tf.InputValue) && tf.InputField.IsMerchantField && tf.InputField.Name == "RequiredFieldsList")
                    {
                        foreach (string fn in tf.InputValue.Split(",".ToCharArray()))
                        {
                            requiredFieldsList.Add(fn);
                        }
                    }
                }

            }

            foreach (InputField input in template.InputFields)
            {
                if (!input.IsMerchantField)
                {
                    // ADD THE CONTROL TO THE PLACEHOLDER
                    // I changed these lines to make these input field prompts match the "real" ones
                    phChoices.Controls.Add(new LiteralControl("<tr><th class=\"rowHeader\" valign=\"top\">"));
                    phChoices.Controls.Add(new LiteralControl((input.UserPrompt + "</th><td>")));
                    WebControl o = input.GetControl();
                    if (o != null)
                    {
                        phChoices.Controls.Add(o);

                        if (requiredFieldsList.Contains(input.Name))
                        {
                            //Add Required Control
                            if (input.InputType == InputType.TextBox)
                            {
                                RequiredFieldValidator rfv = new RequiredFieldValidator();
                                rfv.ID = "rfv" + input.InputFieldId.ToString();
                                rfv.ControlToValidate = o.ID;
                                rfv.Text = "*";
                                rfv.ValidationGroup = "AddToBasket";
                                rfv.ErrorMessage = "Please enter " + input.Name;
                                phChoices.Controls.Add(rfv);
                            }

                        }
                    }
                    phChoices.Controls.Add(new LiteralControl("</td></tr>"));
                    
                }
            }
        }
    }
There should be a better way to access it than loopingPalooza :shock: Maybe someone else knows?

Re: Making Customer Fields Required for Product Template?

Posted: Mon Aug 25, 2008 1:31 pm
by voir
(example usage:
Create a Product Template name ExReq
in Merchant Fields add
Name=RequiredFieldsList
Type=Check Box List

Next to add choices
Name=BuildNumber
Value="" (blank)
selected=Checked

Name=BuildDate
Value="" (blank)
selected=

In Customer Fields add
Name=BuildNumber
Type=Textbox

Name=BuildDate
Type=Textbox

Add/Edit a product, go to the Template section and select ExReq
Select which fields are required for this product

Re: Making Customer Fields Required for Product Template?

Posted: Tue Sep 09, 2008 11:26 am
by voir
It seems that Merchant Fields show up in the darndest place and perhaps RequiredFieldsList should be eliminated... So in ~/ConLib/Utility/OrderItemDetail.aspx.cs and (for me) in ~/Admin/UserControls/OrderItemDetail.aspx.cs Above

Code: Select all

OrderItem _OrderItem;
I Added

Code: Select all

OrderItemInputCollection _oiic = new OrderItemInputCollection();
And Modified the //Show Inputs area

Code: Select all

                //SHOW INPUTS
                if (_OrderItem.Inputs.Count > 0)
                {
                    
                    foreach (OrderItemInput oii in _OrderItem.Inputs)
                    {
                        if (oii.Name != "RequiredFieldsList")
                        {
                            _oiic.Add(oii);
                        }
                    }
                    InputList.DataSource = _oiic;
                    InputList.DataBind();
                }

Re: Making Customer Fields Required for Product Template?

Posted: Tue May 19, 2009 10:38 pm
by ZLA
Also note that if you are using merchant fields as part of your custom product templates, you will need to change ProductCustomFieldsDialog if ShowCustomFields is set to "True".

Re: Making Customer Fields Required for Product Template?

Posted: Wed May 27, 2009 10:06 pm
by ZLA
I've created a customization to validate customer fields with a regular expression validators. See this post:
viewtopic.php?f=42&t=11004

It also includes a variation on the required fields code above. My variation makes all text fields required by default and lets you easily make all fields optional.

Re: Making Customer Fields Required for Product Template?

Posted: Mon Jun 01, 2009 5:08 pm
by ZLA
If you're using the vendor notification email template, you'll also probably want to suppress these special fields using something like the following:

Code: Select all

#if ($orderItem.Inputs.Count > 0)
  #foreach( $input in $orderItem.Inputs )
    #if(!$input.Name.StartsWith("Regex") && $input.Name.Replace(" ", "") != "RequiredFieldsList")
      <strong>$input.Name</strong><strong>:</strong> 
      $input.InputValue
    #end
  #end
#end
I think this would apply to all of these templates:
  • Customer Order Notification
  • Vendor Notification
  • Gift Certificate Validated
  • Order Shipped Partial
  • Order Shipped
  • ESD file is activated
  • License key is fulfilled
-- ZLA

Re: Making Customer Fields Required for Product Template?

Posted: Sun Jun 14, 2009 2:55 pm
by ZLA
voir wrote:It seems that Merchant Fields show up in the darndest place and perhaps RequiredFieldsList should be eliminated...
voir, you may want to look at your wishlists - it will show up there as well.