Support » Plugin: Charitable - Donation Plugin » Conditional donation form field

  • Resolved IstanbulMMV

    (@istanbulmmv)


    Hi there, is it possible to have a custom field appear on some donation forms but not others? I have created a custom field (receivers_email) that is a required field for campaigns in one category only.

    I’ve tried the code below which removes the field, but on submission a validation error indicates a ‘receivers_email’ value is required even though the field does not actually appear on the frontend.

    function wg_customize_donation_form( $fields, Charitable_Donation_Form $form ) {
    
            if ( ! has_term( 'groups', 'campaign_category', get_the_ID() ) ) {
    
    		unset( $fields['receivers_email'] );
    
    	}
    
            return $fields;
    
    }
    add_filter( 'charitable_donation_form_user_fields', 'wg_customize_donation_form', 10, 2 );

    Any thoughts much appreciated and thank you for this outstanding plugin.

    • This topic was modified 1 month, 2 weeks ago by IstanbulMMV.
    • This topic was modified 1 month, 2 weeks ago by IstanbulMMV.
    • This topic was modified 1 month, 2 weeks ago by IstanbulMMV.
    • This topic was modified 1 month, 2 weeks ago by IstanbulMMV.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Please ignore the above question. The code, as shown above, does indeed work exactly as intended! Apologies.

    Plugin Author Eric Daams

    (@ericdaams)

    Good to hear it worked @istanbulmmv! I was looking at the code thinking it should work perfectly, so good to know I’m not going crazy 🙂

    Cheers,
    Eric

    Hi Eric, sorry to bother you again. The above code works perfectly on the front end, but unfortunately doesn’t save my custom value to the database.

    If I amend my code with the addition of a “! is_admin” condition, my custom value is successfully saved to the database, but I’m back to receiving a validation error on all other forms where the ‘receivers_email’ field has been excluded / is not required.

    My code that saves to the database, but causes a validation error on forms that do not feature the ‘receivers_email’ field:

    function wg_customize_donation_form( $fields, Charitable_Donation_Form $form ) {
    
            if ( ! is_admin() && ! has_term( 'groups', 'campaign_category', get_the_ID() ) ) {
    
    		unset( $fields['receivers_email'] );
    
    	}
    
            return $fields;
    
    }
    add_filter( 'charitable_donation_form_user_fields', 'wg_customize_donation_form', 10, 2 );

    My code that produces no validation errors, but doesn’t save my custom value to the donation post_meta donor array:

    function wg_customize_donation_form( $fields, Charitable_Donation_Form $form ) {
    
            if ( ! has_term( 'groups', 'campaign_category', get_the_ID() ) ) {
    
    		unset( $fields['receivers_email'] );
    
    	}
    
            return $fields;
    
    }
    add_filter( 'charitable_donation_form_user_fields', 'wg_customize_donation_form', 10, 2 );

    The code I am using to generate my custom field is:

    function wg_add_recipient_email_field_for_groups_only() {
    
                    $field = new Charitable_Donation_Field( 'receivers_email', array(
                            'label'         => __( 'Receiver\'s email', 'wg' ),
                            'data_type'     => 'user',
                            'donation_form' => array(
                                    'type'          => 'email',
                                    'show_before'   => 'email',
                                    'required'      => true,
                            ),
                            'admin_form' => array(
                                    'type'          => 'email',
                                    'show_after'    => 'email',
                                    'required'      => false,
                            ),
                            'show_in_meta'  => true,
                            'show_in_export' => true,
                            'email_tag'     => array(
                                    'description' => __( 'The receiver\'s email address' , 'wg' ),
                            ),
                    ) );
    
                    charitable()->donation_fields()->register_field( $field );
    
    }
    add_action( 'init', 'wg_add_recipient_email_field_for_groups_only' );

    Any thoughts very much appreciated and apologies if I’m just doing something really silly/careless here!

    • This reply was modified 1 month, 2 weeks ago by IstanbulMMV.
    Plugin Author Eric Daams

    (@ericdaams)

    Hi @istanbulmmv,

    That all looks fine. How have you added this code? Did you add it in a custom plugin, or use Code Snippets, or some other way?

    Hi Eric, I’ve added this code via my own little plugin.

    Trying to eliminate any possible conflicts, I’ve now removed every piece of code from my custom plugin, including where I add my custom ‘receivers_email’ field.

    What I’m now trying to do is simply exclude the core ‘first_name’ and ‘last_name’ fields on some, but not all, donation forms.

    My plugin now contains just one function as shown below:

    function wg_customize_donation_form( $fields, Charitable_Donation_Form $form ) {
    
            unset( $fields['address'] );
            unset( $fields['address_2'] );
            unset( $fields['city'] );
            unset( $fields['state'] );
            unset( $fields['postcode'] );
            unset( $fields['country'] );
            unset( $fields['phone'] );
    
            if ( ! has_term( 'groups', 'campaign_category', get_the_ID() ) ) {
    
                    unset( $fields['first_name'] );
                    unset( $fields['last_name'] );
    
            }
    
            return $fields;
    
    }
    add_filter( 'charitable_donation_form_user_fields', 'wg_customize_donation_form', 10, 2 );

    Using the above code, all forms submit with no validation errors, but first_name and last_name are never saved to the database.

    When I add the ! is_admin() condition as shown …

    function wg_customize_donation_form( $fields, Charitable_Donation_Form $form ) {
    
            unset( $fields['address'] );
            unset( $fields['address_2'] );
            unset( $fields['city'] );
            unset( $fields['state'] );
            unset( $fields['postcode'] );
            unset( $fields['country'] );
            unset( $fields['phone'] );
    
            if ( ! is_admin() && ! has_term( 'groups', 'campaign_category', get_the_ID() ) ) {
    
                    unset( $fields['first_name'] );
                    unset( $fields['last_name'] );
    
            }
    
            return $fields;
    
    }
    add_filter( 'charitable_donation_form_user_fields', 'wg_customize_donation_form', 10, 2 );

    … first_name and last_name are successfully saved to donor array when the first_name and last_name fields are present on the form.

    I get a validation error however when trying to submit a form that has had the first_name and last_name fields removed.

    Hope this helps in some way and thank you again for this amazing plugin.

    • This reply was modified 1 month, 2 weeks ago by IstanbulMMV.
    Plugin Author Eric Daams

    (@ericdaams)

    Hi @istanbulmmv,

    I spotted the issue. The code you want is this:

    
    function wg_customize_donation_form( $fields, Charitable_Donation_Form $form ) {
            if ( ! has_term( 'groups', 'campaign_category', $form->get_campaign()->ID ) ) {
    		    unset( $fields['receivers_email'] );
    	    }
    
            return $fields;
    }
    
    add_filter( 'charitable_donation_form_user_fields', 'wg_customize_donation_form', 10, 2 );
    

    There were a couple problems with the original one you tried. is_admin() should not be used as the donation form is processed via AJAX, which is an admin request — so the field remains part of the form at that point.

    The second issue was that you were using get_the_ID(). get_the_ID() returns the campaign ID in the donation form initially, but once the donation is getting processed, no idea what it would be — I haven’t tested that. Instead, you can use $form->get_campaign()->ID which will consistently give you the campaign’s ID.

    Cheers,
    Eric

    Wonderful!! $form->get_campaign()->ID fixed the problem.
    Many thanks Eric.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.