VirtueMart Forum

VirtueMart 2 + 3 + 4 => Product creation => Topic started by: dron_gr on March 25, 2012, 14:33:47 PM

Title: How to make one of custom field REQUIRED?
Post by: dron_gr on March 25, 2012, 14:33:47 PM
Hi,

VM2.0.0. How can i make one of custom fields with cart attribute (text plugin) - required field? I mean that IF client does not fill the field he cannot push the "ADD TO CART" button.

Thanks in advance!
Title: Re: How to make one of custom field REQUIRED?
Post by: boucdur on March 26, 2012, 13:06:13 PM
I have the same question.

I created custom attributes for sizes and colors but it's possible to press the button add to cart without choosing from the drop down menus.

Furthermore the price don't change dynamicly when I choose something from the drop down menus though I wrote +5 in the column "Price" within the tab "custom field" which is in the "product creation page"
Title: Re: How to make one of custom field REQUIRED?
Post by: boucdur on March 26, 2012, 16:35:47 PM
i could do it with a javascript made in jquery.
When cliking on "add to cart" a bubble (saying please choose a size) would appear if nothing has been chosen in the drop down list "size".
I would do just the kind of things we see on contact forms for required information

But I would have to work on it and it seems such an important feature that I imagine there is some way to do get a custom field required without making my own javascript script.

So how can we make custom field REQUIRED ?

Title: Re: How to make one of custom field REQUIRED?
Post by: dron_gr on March 28, 2012, 22:17:37 PM
NOBODY??? :)

UP!!
Title: Re: How to make one of custom field REQUIRED?
Post by: dron_gr on April 02, 2012, 00:25:50 AM
up
Title: Re: How to make one of custom field REQUIRED?
Post by: HenrikS on April 10, 2012, 13:15:20 PM
Any news on this subject ?
Title: Re: How to make one of custom field REQUIRED?
Post by: Virtual on April 12, 2012, 13:01:50 PM
Have you tried to use custom field Image and display input type="radio"?
Title: Re: How to make one of custom field REQUIRED?
Post by: Bruce Morgan on April 12, 2012, 17:01:47 PM
Looks interesting.  How did you do it? 
Title: Re: How to make one of custom field REQUIRED?
Post by: HenrikS on April 15, 2012, 12:56:22 PM
There should be an option to make every customfield required in virtuemart 2 .... this is needed for most shops.
Title: Re: How to make one of custom field REQUIRED?
Post by: vigier on May 13, 2012, 18:19:29 PM
I second that!
Title: Re: How to make one of custom field REQUIRED?
Post by: Actlas on May 30, 2012, 10:20:38 AM
Does anybody know how??
A tutorial with javascript could also be helpful if this is the only way.
Title: Re: How to make one of custom field REQUIRED?
Post by: Krisse on June 12, 2012, 11:19:44 AM
*BUMP*

This should be resolved quickly, I imagine most shops would need some form of required custom fields!
Title: Re: How to make one of custom field REQUIRED?
Post by: Genius WebDesign on June 13, 2012, 14:36:39 PM
Couldn't agree more.. Its really odd that this is not included in the 2.xxx version when it worked fine and people used it in v1.xxx...

We can only hope that a dev or a kind Java wizard will help us get this feature working ASAP
Title: Re: How to make one of custom field REQUIRED?
Post by: Genius WebDesign on June 19, 2012, 21:53:13 PM
I did it!!

I actually found a thread that helped me hack my way into making custom field child variants where the first drop-down choise automatically says "Please Choose" or whatever you want, and when you try to add to the basket a modal popup appears telling the customer to make a variant choise and the product is not added to the cart.

Here is the thread:
http://forum.virtuemart.net/index.php?topic=100330.0


Further more I found a thread with a hack that makes your "internal note text" shown as text in the drop down menu.
Here's the thread:
http://forum.virtuemart.net/index.php?topic=101947.0

You can see this working on my site here:  (login: demo1   demo1)
http://traeogbyg.dk/index.php/produkter/isolering/paroc-isolering/paroc-a-byg-70mm-600x980mm-pr-m2-details

It's in Danish but google translate makes a proper translation..

On a side note I also changed the "Continue shopping" and "close" link in the modal popup to href="javascript:history.go(0)"   - so that you always go back the current page and refresh if you want to continue shopping instead of always ending up in the category browse..




Title: Re: How to make one of custom field REQUIRED?
Post by: spyderwebdesign on June 19, 2012, 22:31:48 PM
Quote from: fabelmik on June 19, 2012, 21:53:13 PM
I actually found a thread that helped me hack my way into making custom field child variants where the first drop-down choise automatically says "Please Choose" or whatever you want, and when you try to add to the basket a modal popup appears telling the customer to make a variant choise and the product is not added to the cart.

Here is the thread:
http://forum.virtuemart.net/index.php?topic=100330.0

I must be blind, because I don't see where it shows how to add the modal popup and stop the customer from adding the parent item to cart. Would you please post instructions how to do this?
Title: Re: How to make one of custom field REQUIRED?
Post by: spyderwebdesign on June 19, 2012, 22:34:14 PM
Quote from: fabelmik on June 19, 2012, 21:53:13 PM
On a side note I also changed the "Continue shopping" and "close" link in the modal popup to href="javascript:history.go(0)"   - so that you always go back the current page and refresh if you want to continue shopping instead of always ending up in the category browse..

How did you do this? What file? What code did you change and what code did you change it to?
Title: Re: How to make one of custom field REQUIRED?
Post by: Genius WebDesign on June 20, 2012, 00:46:16 AM
Regarding stopping the customer from adding the parent to the cart see post #3 http://forum.virtuemart.net/index.php?topic=100330.0

Regarding the "continue shopping" link you must edit the card.php file located in /components/com_virtuemart/controllers/
Just  change the href="' . $continue_link . '" to href="javascript:history.go(0)"    after    $this->json->msg =

Remember to make backup before you start editing!:)

Title: Re: How to make one of custom field REQUIRED?
Post by: spyderwebdesign on June 20, 2012, 02:27:31 AM
Quote from: fabelmik on June 20, 2012, 00:46:16 AM
Regarding the "continue shopping" link you must edit the card.php file located in /components/com_virtuemart/controllers/

So this is odd. I am running VM 2.0.6 and I do not have a /components/com_virtuemart/controllers/card.php file...
Are you running the same version?
Title: Re: How to make one of custom field REQUIRED?
Post by: spyderwebdesign on June 20, 2012, 02:31:20 AM
I figured it out. You meant cart.php
Title: Re: How to make one of custom field REQUIRED?
Post by: spyderwebdesign on June 20, 2012, 02:32:59 AM
Quote from: fabelmik on June 20, 2012, 00:46:16 AM
Regarding stopping the customer from adding the parent to the cart see post #3 http://forum.virtuemart.net/index.php?topic=100330.0

Did you mean reply #3 and not post #3? Post number 3 is

Quote
One word: GREAT!

One small suggestion: when trying to add the parent to the shopping cart, give a error message that the user has to choose!
Title: Re: How to make one of custom field REQUIRED?
Post by: Genius WebDesign on June 20, 2012, 03:14:34 AM
yea, sry bout my typos..
I meant cart.php and "reply #3"
Title: Re: How to make one of custom field REQUIRED?
Post by: Virtual on July 17, 2012, 08:40:27 AM
Is main topic still actual for somebody?
Please send personal message with link to your site
Title: Re: How to make one of custom field REQUIRED?
Post by: Robert_ITMan on April 01, 2013, 06:15:52 AM
HELP – we just need someone to get validation to work on the product page add to cart – similar to how it already works for the user tmpl files (user/edit.php and user/edit_address.php).
!? I'm not sure which file to add the formvalidation to, maybe:
(1) template default_addtocart.php as it has the form in it, but the form class is used to autoupdate the price – I tried this but my attempt at adding the validation code and validation to the add to cart button fails so far.
(2) template default_customfields.php is the safest place as it only affects the custom fields but it might not pass the needed info to the form in the cart.

!? I'm not sure how to pass the required="required" to the $field->display – I see hints at this in the user/edit_address.php form where it uses jquery. So instead I found I can edit the vmcustom tmpl files, for example:

plugins/vmcustom/textinput/textinput/tmpl/default.php
<input class="<?php echo $class ?>" type="text" value="" size="<?php echo $this->params->custom_size ?>" name="customPlugin[<?php echo $viewData[0]->virtuemart_customfield_id ?>][<?php echo $this->_name?>][comment]" <?php if ( $this->is_required "1" ){ echo 'required="required"';} // added by RM - if statement doesn't actually work – simply adding the required="required" would work well enough for me here ?>><br />

NOTE: the following edits will add is_required similar to is_hidden to all VM2 Custom Fields so anyone can easily make these required or mandatory (we still need help getting the validation working, see above)

Database Edit:
ALTER TABLE `jos_virtuemart_customs` ADD `is_required` TINYINT( 1 ) NOT NULL DEFAULT '0' AFTER `is_hidden`

File Edits

components/com_virtuemart/views/productdetails/tmpl/default_addtocart.php
templates\(my template)/html/com_virtuemart/productdetails/default_addtocart.php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');
// added by RM
// Implement Joomla's form validation
JHTML::_('behavior.formvalidation');
JHTML::stylesheet('vmpanels.css', JURI::root() . 'components/com_virtuemart/assets/css/');
?>

administrator/language/en-GB/en-GB.com_virtuemart.ini
COM_VIRTUEMART_CUSTOM_IS_HIDDEN="Hidden"
COM_VIRTUEMART_CUSTOM_IS_REQUIRED="Required" ; added by RM

COM_VIRTUEMART_TOGGLE_HIDDEN="Toggle Hidden"
COM_VIRTUEMART_TOGGLE_REQUIRED="Toggle Required" ; added by RM

administrator/components/com_virtuemart/views/custom/tmpl/default.php
<th><?php echo JText::_('COM_VIRTUEMART_CUSTOM_IS_HIDDEN'); ?></th>
<th><?php echo JText::_('COM_VIRTUEMART_CUSTOM_IS_REQUIRED'); // added by RM ?></th>

<td><a href="javascript:void(0);" onclick="return listItemTask('cb<?php echo $i;?>','toggle.is_hidden')" title="<?php echo ($custom->is_hidden ) ? JText::_('COM_VIRTUEMART_YES') : JText::_('COM_VIRTUEMART_NO');?>"><span class="vmicon <?php echo ( $custom->is_hidden  'vmicon-16-checkin' 'vmicon-16-bug' );?>"></span></a></td>
<?php // following <td>..</td> added by RM ; ?>
                <td><a href="javascript:void(0);" onclick="return listItemTask('cb<?php echo $i;?>','toggle.is_required')" title="<?php echo ($custom->is_required ) ? JText::_('COM_VIRTUEMART_YES') : JText::_('COM_VIRTUEMART_NO');?>"><span class="vmicon <?php echo ( $custom->is_required  'vmicon-16-checkin' 'vmicon-16-bug' );?>"></span></a></td>

administrator/components/com_virtuemart/views/custom/view.html.php
JToolBarHelper::custom('toggle.is_hidden.1', 'publish','', JText::_('COM_VIRTUEMART_TOGGLE_HIDDEN'), true);
JToolBarHelper::custom('toggle.is_hidden.0', 'unpublish','', JText::_('COM_VIRTUEMART_TOGGLE_HIDDEN'), true);
JToolBarHelper::custom('toggle.is_required.1', 'publish','', JText::_('COM_VIRTUEMART_TOGGLE_HIDDEN'), true); // added by RM
JToolBarHelper::custom('toggle.is_required.0', 'unpublish','', JText::_('COM_VIRTUEMART_TOGGLE_HIDDEN'), true); // added by RM

administrator/components/com_virtuemart/tables/customs.php
/** @var int(1)  1= hidden field info */
var $is_hidden = 0;
/** @var int(1)  1= required field info */
var $is_required = 0; // added by RM

administrator/components/com_virtuemart/models/custom.php
$this->setToggleName('is_hidden');
$this->setToggleName('is_required'); added by RM

administrator/components/com_virtuemart/models/customfields.php
$html .= VmHTML::row ('booleanlist', 'COM_VIRTUEMART_CUSTOM_IS_HIDDEN', 'is_hidden', $datas->is_hidden);
$html .= VmHTML::row ('booleanlist', 'COM_VIRTUEMART_CUSTOM_IS_REQUIRED', 'is_required', $datas->is_required); // added by RM

// in the following , `is_required` added after  , `is_hidden` // edited by RM
$query = 'SELECT C.`virtuemart_custom_id` , `custom_element`, `custom_jplugin_id`, `custom_params`, `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` , C.`custom_value` AS value, `custom_field_desc` , `field_type` , `is_list` , `is_cart_attribute` , `is_hidden` , `is_required` , C.`published` , field.`virtuemart_customfield_id` , field.`custom_value`,field.`custom_param`,field.`custom_price`,field.`ordering`
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
Where `virtuemart_product_id` =' . $virtuemart_product_id . ' order by field.`ordering` ASC';

// in the following , `is_required` added after  , `is_hidden` // edited by RM
$query = 'SELECT C.`virtuemart_custom_id` , `custom_element`, `custom_params`, `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` , C.`custom_value` AS value, `custom_field_desc` , `field_type` , `is_list` , `is_hidden` , `is_required` , `layout_pos`, C.`published` , field.`virtuemart_customfield_id` , field.`custom_value`, field.`custom_param`, field.`custom_price`, field.`ordering`
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
Where `virtuemart_product_id` =' . (int)$product->virtuemart_product_id . ' and `field_type` != "G" and `field_type` != "R" and `field_type` != "Z"';

// in the following , `is_required` added after  , `is_hidden` // edited by RM
$query = 'SELECT C.`virtuemart_custom_id` , `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` , C.`custom_value` AS value, `custom_field_desc` , `field_type` , `is_list` , `is_hidden` , `is_required` , C.`published` , field.`virtuemart_customfield_id` , field.`custom_value`, field.`custom_param`, field.`custom_price`, field.`ordering`
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
Where `virtuemart_product_id` =' . (int)$product->virtuemart_product_id . ' and `field_type` = "Z"';

// in the following , `is_required` added after  , `is_hidden` // edited by RM
$query = 'SELECT C.`virtuemart_custom_id` , `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` , C.`custom_value` AS value, `custom_field_desc` , `field_type` , `is_list` , `is_hidden` , `is_required` , C.`published` , field.`virtuemart_customfield_id` , field.`custom_value`, field.`custom_param`, field.`custom_price`, field.`ordering`
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
Where `virtuemart_product_id` =' . (int)$product->virtuemart_product_id . ' and `field_type` = "R"';

// group by virtuemart_custom_id
// in the following , `is_required` added after  , `is_hidden` // edited by RM
$query = 'SELECT C.`virtuemart_custom_id`, `custom_title`, C.`custom_value`,`custom_field_desc` ,`custom_tip`,`field_type`,field.`virtuemart_customfield_id`,`is_hidden` , `is_required`
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
Where `virtuemart_product_id` =' . (int)$product->virtuemart_product_id . ' and `field_type` != "G" and `field_type` != "R" and `field_type` != "Z"';

$db = JFactory::getDBO ();
// in the following , `is_required` added after  , `is_hidden` // edited by RM
$query = 'SELECT C.`virtuemart_custom_id` , `custom_element` , `custom_parent_id` , `admin_only` , `custom_title` , `custom_tip` ,
C.`custom_value` AS value, `custom_field_desc` , `field_type` , `is_list` , `is_cart_attribute` , `is_hidden` , `is_required` , C.`published` ,
field.`virtuemart_customfield_id` , field.`custom_value`,field.`custom_param`,field.`custom_price`
FROM `#__virtuemart_customs` AS C
LEFT JOIN `#__virtuemart_product_customfields` AS field ON C.`virtuemart_custom_id` = field.`virtuemart_custom_id`
WHERE `virtuemart_customfield_id` ="' . (int)$selected . '"';
Title: Re: How to make one of custom field REQUIRED?
Post by: PolishedGeek on April 09, 2013, 01:51:03 AM
For those who would like to do required fields without hacks or code modifications, there is an extension that will let you do this: http://extensions.virtuemart.net/products/breezing-forms-custom-fields-plugin-detail
Title: Re: How to make one of custom field REQUIRED?
Post by: treodude on July 17, 2013, 14:40:28 PM
Seriously, why is there no good solution that?

One solution is only for dropdown selections and the other one is €50 for something way too simple and small.

Virtuemart, make this a default option!
Title: Re: How to make one of custom field REQUIRED?
Post by: HenrikS on August 01, 2013, 19:30:01 PM
Yes why is there no good solution to this yet .. almost all shops use it?

And how come devs. dont answer on this topic.
Title: Re: How to make one of custom field REQUIRED?
Post by: Robert_ITMan on November 21, 2013, 16:36:43 PM
To follow up on this I posted a new topic 'VM2 Custom Fields Integration - IT WORKS!' here:
http://forum.virtuemart.net/index.php?topic=120445.0
Title: Re: How to make one of custom field REQUIRED?
Post by: artist.trade on November 23, 2013, 21:03:37 PM
Hey, here is my simple solution - it marks the border color of mandatory fields and disable/enable send button in product details. To mark field as mandatory, simply add three dots at the end of text for index 0. Like "Please choose ...".

Just copy code below into line 29 (above <div class="addtocart-area">) inside \components\com_virtuemart\views\productdetails\tmpl\default_addtocart.php.

<script type='text/javascript'>
  // On window load start function
  window.onload = initForm;
  window.onunload = function() {};
 
  function initForm() {
    // Last characters of first index in selection box define mandatory status.
    var mandatory = "...";
    var elements = document.getElementsByClassName('product-field-display');
   
    // Find mandatory fields
    if (elements.length > 0) {
      var j = 0;
      var ids = new Array();
      for (var i = 0; i < elements.length; i++) {
        var text = elements[i].innerHTML;
        var x = text.search("customPrice");
        var y = text.search("name=");
        var id = text.substr(x,y - x - 2);
        var index = document.getElementById(id).options[0].text;
        if (index.substr(index.length - mandatory.length,mandatory.length) == mandatory) {
          ids[j] = id;
          j++;
        }
      }
     
      // Set the color of mandatory fields
      var j = 0;
      for (var i = 0; i < ids.length; i++) {
       
        document.getElementById(ids[i]).setAttribute("onchange", "initForm()");
       
        if (document.getElementById(ids[i]).selectedIndex == 0) {
          document.getElementById(ids[i]).style.borderColor="#FF0000";
          j++;
        } else {
          document.getElementById(ids[i]).style.borderColor="#AAAAAA";
        }
      }
     
      // Disable or enable send button
      if (j != 0) {
        document.getElementsByName("addtocart")[0].disabled = true;
      } else {
        document.getElementsByName("addtocart")[0].disabled = false;
      }
    }
  }
</script>
Title: Re: How to make one of custom field REQUIRED?
Post by: frighten on February 12, 2014, 00:25:16 AM
Hello artist.trade
Your solution seem to be interesting. Did it work? I added the code and then created some custom fields and added the dots as you described.
Nothing was changed. Maybe I made something wrong in the creation of the custom fields itself...
Is there a proper way to create the fields to make it work with your code?
My main goal is to get one dropdown menu just above the "Add to cart" button. By default it will say "Please Choose..." and then the customer has to choose one of three following options in the dropdown. If they don't choose anything they can not add to cart, or be prompted to add to cart.

Using VM 2.0.26d (with Joomla 2.5.18)
Title: Re: How to make one of custom field REQUIRED?
Post by: klattr1 on April 05, 2014, 12:21:30 PM
This should be a standard feature in VM 2. It is used by so many other eCommerce platforms and prevents users from choosing incorrect cart variant options accidentally.

Mods, pleas add this functionality to the core code.
Title: Re: How to make one of custom field REQUIRED?
Post by: dron_gr on January 11, 2016, 17:21:23 PM
Hey mods you doing great job out there! Keep up! But after almost 4 years passed nothing been done about Required custom fields. I was forced to buy an extension for 17 euro but it doesnt worked for me because i use VMUIKIT and it has its own overrides. So there is still a need in Required custom fields.  :) Would very appreciate if it will be done in nearly update!  :)