Conditional Required Fields

  • Posts: 19
  • Thank you received: 0
12 years 7 months ago #42933

Heres the issue: My site sells a fundraising product for organizations. Each organization's needs vary, and as such some want to track their sales for individual participants within the group and some do not. So instead of creating 30 different products for each participant within the organization, I've created a custom field on the order table for the name of the participant.

Now this works great, but only if every organization wants to keep track of which participant a sale is to benefit. But not all of them do. I've created a custom field, Required_Participant_Name, on the product table which is a simple yes/no radio button. Based on the value of the radio button, I would like to display the custom field on the order table for the name of the participant. Is this possible? Is there a more elegant solution to my problem? Any feedback would be greatly appreciated.

Please Log in or Create an account to join the conversation.

  • Posts: 82726
  • Thank you received: 13342
  • MODERATOR
12 years 7 months ago #43072

I understand that you want to have a conditional custom order field on the checkout based on the value of a custom product field of the products in the cart.
There is no option for that but you can do it with a few lines of code customization in the file "fields" of the view "checkout" via the menu Display->Views if you know programming. You will have to loop through the products and based on the value of that field, display or not the custom order fields.

Please Log in or Create an account to join the conversation.

  • Posts: 19
  • Thank you received: 0
12 years 7 months ago #43167

Thank you for your response, I've gotten the loop to execute properly and the custom fields are conditionally displayed as expected. However, if the fields are not shown and the user proceeds to the next checkout step an error is thrown asking the user to fill in the custom field even though it is not displayed. Is there a way to disable this error message as well?? If you want to see the errors I can provide you w/ URL's. Thanks again!

Please Log in or Create an account to join the conversation.

  • Posts: 19
  • Thank you received: 0
12 years 7 months ago #43276

In addition to displaying or not displaying the custom field on checkout, my initial thought is to manually set the custom field 'participant name' of the order table to "none" if the radio button on the product table is set to no. Then in the e-mails, edit where the custom field is printed to only display if not equal to "none". The only problem is I am not entirely sure how to reference the custom field array to set the value of 'participant name' to "none".

Please Log in or Create an account to join the conversation.

  • Posts: 82726
  • Thank you received: 13342
  • MODERATOR
12 years 7 months ago #43295

We can look at the problem if you want. Please provide the code modifications you did and a link to the page on your website so that we can reproduce the error.

In the file "fields" of the view "checkout", you can set the default value like that:

if(empty($this->order->Required_Participant_Name)) $this->order->Required_Participant_Name='none';

Please Log in or Create an account to join the conversation.

  • Posts: 19
  • Thank you received: 0
12 years 7 months ago #43317

the url you can add the product to the cart is: themetrocashcard.com/trenton-highschool-sample1 I will set the "required_participant" to No. Below is the code for my modified checkout / fields.php , I've left database info blank for this purpose.




<?php
/**
* @package HikaShop for Joomla!
* @version 1.5.5
* @author hikashop.com
* @copyright (C) 2010-2011 HIKARI SOFTWARE. All rights reserved.
* @license GNU/GPLv3 www.gnu.org/licenses/gpl-3.0.html
*/
defined('_JEXEC') or die('Restricted access');
?>
<?php
$cartClass = hikashop_get('class.cart');
$cart = $cartClass->loadFullCart(true);
$this->rows =& $cart->products;

foreach($this->rows as $row)
{

$productClass = hikashop_get('class.product');
$product = $productClass->get($row->product_id);

if(empty($product)) continue;
if(!empty($product))
{
$hostname='';
$username='';
$password='';
$dbname='';
$usertable='jos_hikashop_product';
mysql_connect($hostname,$username, $password) OR DIE ('Unable to connect to database! Please try again later.');
mysql_select_db($dbname);
$query = 'SELECT * FROM ' . $usertable;
$result = mysql_query($query);
if($result)
{
while($row1 = mysql_fetch_array($result))
{
$query_result_product_id = $row1;
if($query_result_product_id == $product->product_id)
{
$required_participant_name = $row1;

}

}

}
}
}?>

<?php if(hikashop_level(2) && !empty($this->extraFields) && $required_participant_name=="TRUE"){

hikashop = Array('participant_name');
?>
<div id="hikashop_checkout_additional_info" class="hikashop_checkout_additional_info">
<fieldset class="input">
<legend><?php echo JText::_('ADDITIONAL_INFORMATION');?></legend>
<table cellpadding="0" cellspacing="0" border="0" class="hikashop_contentpane">
<?php
if(!empty($this->extraFields)){
JRequest::setVar('hikashop_check_order',1);
$this->setLayout('custom_fields');
$this->type = 'order';
echo $this->loadTemplate();
}
?>
</table>
</fieldset>
</div>
<div style="clear:both"></div>
<?php } ?>

Please Log in or Create an account to join the conversation.

  • Posts: 82726
  • Thank you received: 13342
  • MODERATOR
12 years 7 months ago #43414

Mmm, quite complex for what you want to do.

It should be like that:

$cartClass = hikashop_get('class.cart');
$cart = $cartClass->loadFullCart(true);
$this->rows =& $cart->products;
$required_participant_name = false;
foreach($this->rows as $row){
 $productClass = hikashop_get('class.product');
 $product = $productClass->get($row->product_id);
 if($product->required_participant_name) $required_participant_name = true;
}

if(!$required_participant_name){
$doc =& JFactory::getDocument();
$js="hikashop['reqFieldsComp']['order'] = Array();";
$doc->addScriptDeclaration( "<!--\n".$js."\n//-->\n" );
return;
}

Please Log in or Create an account to join the conversation.

  • Posts: 19
  • Thank you received: 0
12 years 7 months ago #43836

I appreciate your help, this will now properly display or not display the custom field. However ( the custom field is set as a required field) when the field is displayed the user is now able to proceed to the next step without ever entering a value into the field. In otherwords.. right back to where we began. Here's a URL www.themetrocashcard.com/trenton-marketing-nyc2012 to recreate if needed, and below is the code that I currently have for checkout/fields.php

EDIT: In FireFox on windows, if the custom field is NOT displayed and the user proceeds to the next step an error is displayed as follows: "please fill Participant Name field" even though there is no participant name field displayed.

<?php
/**
 * @package    HikaShop for Joomla!
 * @version    1.5.5
 * @author    hikashop.com
 * @copyright  (C) 2010-2011 HIKARI SOFTWARE. All rights reserved.
 * @license    GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?>

<?php
$cartClass = hikashop_get('class.cart');
$cart = $cartClass->loadFullCart(true);
$this->rows =& $cart->products;
$required_participant_name = false;
foreach($this->rows as $row){
 $productClass = hikashop_get('class.product');
 $product = $productClass->get($row->product_id);
 if($product->required_participant_name == "TRUE" ) $required_participant_name = true;
}
 
if(!$required_participant_name){
$doc =& JFactory::getDocument();
$js="hikashop['reqFieldsComp']['order'] = Array();";
$doc->addScriptDeclaration( "<!--\n".$js."\n//-->\n" );
return;
}
?>


<?php if(hikashop_level(2) && !empty($this->extraFields['order']) && $required_participant_name == true){

?>
<div  id="hikashop_checkout_additional_info" class="hikashop_checkout_additional_info">
  <fieldset class="input">
    <legend><?php echo JText::_('ADDITIONAL_INFORMATION');?></legend>
    <table cellpadding="0" cellspacing="0" border="0" class="hikashop_contentpane">
  <?php
    if(!empty($this->extraFields['order'])){
      JRequest::setVar('hikashop_check_order',1);
      $this->setLayout('custom_fields');
      $this->type = 'order';
      echo $this->loadTemplate();
    }
  ?>
    </table>
  </fieldset>
</div>
<div style="clear:both"></div>
<?php } ?>

Last edit: 12 years 7 months ago by usafadmin.

Please Log in or Create an account to join the conversation.

  • Posts: 82726
  • Thank you received: 13342
  • MODERATOR
12 years 7 months ago #44028

Ah yes. You will also have to change the file administrator/components/com_hikashop/classes/field.php so that the check, server side is skipped.
Just remove that code:

if(!$class->check($fields[$k],$val,@$oldData->$namekey)){
$ok = false;
}

Could you give a link to a product for which the field is not displayed so that we can look at your FF problem ?

Please Log in or Create an account to join the conversation.

  • Posts: 19
  • Thank you received: 0
12 years 7 months ago #44105

I apologize in advance if I'm becoming a burden, and truly appreciate your bearing with my inexperience in the matter. Removing that code seems to have fixed the error message when the custom field is not displayed in FireFox. But if the field is displayed you are still able to proceed to the next step without entering any value into the required custom field. I can see the following in the page source

<!-- hikashop['reqFieldsComp']['order'] = Array('participant_name');
					hikashop['validFieldsComp']['order'] = Array('Please enter the name of the participant that you are supporting by making this purchase:  ');
//-->

so it is establishing that it is a required field, but the check is not functioning properly. Any thoughts? If you want to add a product to the cart to recreate the error, please visit themetrocashcard.com/component/hikashop/product/cid-9

Last edit: 12 years 7 months ago by usafadmin.

Please Log in or Create an account to join the conversation.

  • Posts: 82726
  • Thank you received: 13342
  • MODERATOR
12 years 7 months ago #44366

I think that the problem comes from the JS error that you have on your website:
Uncaught TypeError: Cannot call method 'set' of null
in the file themetrocashcard.com/templates/beez_20/j...t/md_stylechanger.js

I don't know why you have that error though.
Looking around online, you're not the only one with that js error:
stackoverflow.com/questions/9187026/mootools-issue-in-ie8
They recommend that you delete that javascript file in order to solve the problem...

Please Log in or Create an account to join the conversation.

  • Posts: 19
  • Thank you received: 0
12 years 7 months ago #44537

I've fixed the javascript error on the site, but yet again to no avail. themetrocashcard.com/component/hikashop/product/cid-11 ( the other link above is no good for the purpose of correcting this issue as it has gone live now ) is a link to see that there are indeed no more javascript issues on the page.

Please Log in or Create an account to join the conversation.

  • Posts: 82726
  • Thank you received: 13342
  • MODERATOR
12 years 7 months ago #44653

Your problem is now basic: You have a default value " " (a space) in your custom field. Because of that, the system understand that something is entered in the custom field and so it doesn't display any error unless you remove the space.

Please Log in or Create an account to join the conversation.

  • Posts: 19
  • Thank you received: 0
12 years 7 months ago #44727

DOH!!

Thank you so much, this issue is now officially resolved. For those that are interested in what the final steps are for this problem here's a breakdown:

1. Create a custom radio button field on the product table. Label it something like 'Required_Participant_Name'. I've set two values, (title)Yes = (value)TRUE, (title)No = (value)FALSE.
2. Create a custom text field on the order table. Label this one something like 'Participant_Name'.
3. Go to Display->Views modify checkout/fields.php with the following code:
<?php
/**
 * @package    HikaShop for Joomla!
 * @version    1.5.5
 * @author    hikashop.com
 * @copyright  (C) 2010-2011 HIKARI SOFTWARE. All rights reserved.
 * @license    GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');
?>
 
<?php
$cartClass = hikashop_get('class.cart');
$cart = $cartClass->loadFullCart(true);
$this->rows =& $cart->products;
$required_participant_name = false;
foreach($this->rows as $row){
 $productClass = hikashop_get('class.product');
 $product = $productClass->get($row->product_id);
 if($product->required_participant_name == "TRUE" ) $required_participant_name = true;
}
 
if(!$required_participant_name){
$doc =& JFactory::getDocument();
$js="hikashop['reqFieldsComp']['order'] = Array();";
$doc->addScriptDeclaration( "<!--\n".$js."\n//-->\n" );
return;
}
?>
 
 
<?php if(hikashop_level(2) && !empty($this->extraFields['order']) && $required_participant_name == true){
 
?>
<div  id="hikashop_checkout_additional_info" class="hikashop_checkout_additional_info">
  <fieldset class="input">
    <legend><?php echo JText::_('ADDITIONAL_INFORMATION');?></legend>
    <table cellpadding="0" cellspacing="0" border="0" class="hikashop_contentpane">
  <?php
    if(!empty($this->extraFields['order'])){
      JRequest::setVar('hikashop_check_order',1);
      $this->setLayout('custom_fields');
      $this->type = 'order';
      echo $this->loadTemplate();
    }
  ?>
    </table>
  </fieldset>
</div>
<div style="clear:both"></div>
<?php } ?>
4. Find the following file, administrator/components/com_hikashop/classes/field.php. Once located, find the following line of code and comment it out. (Or delete it if you so choose)
if(!$class->check($fields[$k],$val,@$oldData->$namekey)){
$ok = false;
}
This removes the server side form validation.
5. Bada Boom, Bada Bing - You've now got conditional custom fields. Enjoy!

Nicolas, thanks again!

Last edit: 12 years 7 months ago by usafadmin.

Please Log in or Create an account to join the conversation.

  • Posts: 82726
  • Thank you received: 13342
  • MODERATOR
10 years 5 months ago #158500

Hi,

Note that conditional custom fields can now be done with the "display limited to" option of the custom fields. No need to change any code.

Please Log in or Create an account to join the conversation.

Time to create page: 0.109 seconds
Powered by Kunena Forum