Skip Navigation

[Resolved] Coupon code for CRED form

This support ticket is created 7 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 1 reply, has 2 voices.

Last updated by Christian Cox 7 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#589207

Hi, I have an order form and I want to have a validation for coupons. They are made as CPT called "kupon" and have title as description, coupon code, date when the coupon starts to be valid and date of expiration. In order form is a field "kupon" for placing the code. Can you show me, how to validate the coupon code using the button and display the result by based on the validation of code and dates? Thank you.

	<div class="form-group">
		[cred_field field='kupon' post='objednavka' value='' urlparam='' class='form-control col-lg-8' output='bootstrap']
      	        <input class="col-lg-4" type="button" value="Uplatnit">
	</div>
#589281

Hi, custom validation can be added using the cred_form_validate API. Here's the general format:

add_filter('cred_form_validate','custom_kupon_validation',10,2);
function custom_kupon_validation($error_fields, $form_data)
{
    //field data are field values and errors
    list($fields,$errors)=$error_fields;
    //uncomment this if you want to print the field values
    //print_r($fields);
    //validate if specific form
    if ($form_data['id']==12345)
    {
      if( $fields['wpcf-kupon']['value'] = 0 ) {
        $errors['wpcf-kupon']='Invalid kupon!';
      }
    }
    //return result
    return array($fields,$errors);
}

Change 12345 to the numeric ID of your CRED form, and modify the conditional validation code as needed. If you need more specific information about the conditionals, let me know.