Making Customer Fields Required for Product Template?

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
DFresh
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 33
Joined: Fri Jan 04, 2008 11:12 am

Making Customer Fields Required for Product Template?

Post by DFresh » Fri May 02, 2008 12:38 pm

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!

User avatar
compunerdy
Admiral (ADM)
Admiral (ADM)
Posts: 1283
Joined: Sun Nov 18, 2007 3:55 pm

Re: Making Customer Fields Required for Product Template?

Post by compunerdy » Fri May 02, 2008 12:45 pm

:lol: Literally 5 sec ago I ran into the same issue.

User avatar
m_plugables
Commander (CMDR)
Commander (CMDR)
Posts: 149
Joined: Tue Mar 11, 2008 12:44 am
Contact:

Re: Making Customer Fields Required for Product Template?

Post by m_plugables » Tue May 06, 2008 8:49 am

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>"));
                }
            }
        }
    }
Image
Visit the links below to Download Plugins for your AC7 Store
http://www.plugables.com
http://blog.plugables.com

DFresh
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 33
Joined: Fri Jan 04, 2008 11:12 am

Re: Making Customer Fields Required for Product Template?

Post by DFresh » Tue May 06, 2008 10:43 am

Thank you so much... That worked perfectly!

User avatar
calvis
Rear Admiral (RADM)
Rear Admiral (RADM)
Posts: 710
Joined: Tue Jan 27, 2004 3:57 pm
Location: Redmond, WA

Re: Making Customer Fields Required for Product Template?

Post by calvis » Tue May 06, 2008 12:09 pm

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
Able Customer Since 1999 Currently Running on GOLD R12 SR1 and PCI Certified.

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

Re: Making Customer Fields Required for Product Template?

Post by jmestep » Mon May 12, 2008 8:30 am

Here is his Paypal email address --- change at to @ plugablesatgmail.com.
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

dnoell
Ensign (ENS)
Ensign (ENS)
Posts: 19
Joined: Mon Jul 07, 2008 9:36 am

Re: Making Customer Fields Required for Product Template?

Post by dnoell » Wed Jul 09, 2008 10:19 am

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.

User avatar
voir
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 24
Joined: Mon Jun 09, 2008 4:25 pm
Location: Belingham, WA US

Re: Making Customer Fields Required for Product Template?

Post by voir » Mon Aug 25, 2008 11:32 am

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?
Last edited by voir on Mon Aug 25, 2008 7:06 pm, edited 2 times in total.

User avatar
voir
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 24
Joined: Mon Jun 09, 2008 4:25 pm
Location: Belingham, WA US

Re: Making Customer Fields Required for Product Template?

Post by voir » Mon Aug 25, 2008 1:31 pm

(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

User avatar
voir
Lieutenant, Jr. Grade (LT JG)
Lieutenant, Jr. Grade (LT JG)
Posts: 24
Joined: Mon Jun 09, 2008 4:25 pm
Location: Belingham, WA US

Re: Making Customer Fields Required for Product Template?

Post by voir » Tue Sep 09, 2008 11:26 am

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

ZLA
Commodore (COMO)
Commodore (COMO)
Posts: 496
Joined: Fri Mar 13, 2009 2:55 pm

Re: Making Customer Fields Required for Product Template?

Post by ZLA » Tue May 19, 2009 10:38 pm

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".

ZLA
Commodore (COMO)
Commodore (COMO)
Posts: 496
Joined: Fri Mar 13, 2009 2:55 pm

Re: Making Customer Fields Required for Product Template?

Post by ZLA » Wed May 27, 2009 10:06 pm

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.

ZLA
Commodore (COMO)
Commodore (COMO)
Posts: 496
Joined: Fri Mar 13, 2009 2:55 pm

Re: Making Customer Fields Required for Product Template?

Post by ZLA » Mon Jun 01, 2009 5:08 pm

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

ZLA
Commodore (COMO)
Commodore (COMO)
Posts: 496
Joined: Fri Mar 13, 2009 2:55 pm

Re: Making Customer Fields Required for Product Template?

Post by ZLA » Sun Jun 14, 2009 2:55 pm

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.

Post Reply