VirtueMart Forum

VirtueMart 2 + 3 + 4 => General Questions => Topic started by: D_L_I on November 08, 2021, 09:48:45 AM

Title: programmatically change shopper field validation
Post by: D_L_I on November 08, 2021, 09:48:45 AM
Hello,
I wish to add a field on my checkout form that will be shown and required only when certain products are in the cart.
How can I programmatically set a field to be required?
Title: Re: programmatically change shopper field validation
Post by: Studio 42 on November 08, 2021, 11:01:40 AM
You have to write your own userfield plugin to do that
Title: Re: programmatically change shopper field validation
Post by: D_L_I on November 09, 2021, 08:05:15 AM
Thanks.
can you please point me to an example or documentation for that?
Title: Re: programmatically change shopper field validation
Post by: Studio 42 on November 09, 2021, 11:55:47 AM
No doc exist and to long to explain all if you never wrote a plugin before
The front display trigger is
   public function plgVmOnUserfieldDisplay($_prefix, $field, $user_id,  &$return ){
      if ('plugin' . $this->_name != $field->type) {
         return;
      }
//you need to get the cart and loop the products in cart
      $selected = $return['fields'][$field->name]['value']; // the default or current value
      $return['fields'][$field->name]['formcode'] = 'what you need to render';


   }