News:

You may pay someone to create your store, or you visit our seminar and become a professional yourself with the silver certification

Main Menu

How to make one of custom field REQUIRED?

Started by dron_gr, March 25, 2012, 14:33:47 PM

Previous topic - Next topic

spyderwebdesign

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?

Genius WebDesign

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!:)


spyderwebdesign

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?

spyderwebdesign


spyderwebdesign

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!

Genius WebDesign

yea, sry bout my typos..
I meant cart.php and "reply #3"

Virtual

Is main topic still actual for somebody?
Please send personal message with link to your site

Robert_ITMan

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 . '"';
WEBSITES @ OURFINGERTIPS
manage > develop > market > repeat

Save a lot of time and money when focused on building a website that works with marketing efforts to get more leads and sales from visitors.

www.ourfingertips.com

PolishedGeek

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
~ Deb Cinkus, CEO
Polished Geek, LLC   |    www.PolishedGeek.com

Creators of JoomLister - eBay Lister for Joomla!

treodude

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!

HenrikS

Yes why is there no good solution to this yet .. almost all shops use it?

And how come devs. dont answer on this topic.

Robert_ITMan

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
WEBSITES @ OURFINGERTIPS
manage > develop > market > repeat

Save a lot of time and money when focused on building a website that works with marketing efforts to get more leads and sales from visitors.

www.ourfingertips.com

artist.trade

#27
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>

frighten

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)

klattr1

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.