• Resolved vader7071

    (@vader7071)


    I am using CF7 and the paypal option. So far it has been great, but there is one step I would like to add.

    In CF7, I have some basic fields. Simple things like:

    Spots: _____ (Client entered number)
    Tent: Y/N (checkbox)
    Electrical: Y/N (checkbox)

    What I would like to do is have a hidden field (I have that plugin as well) that looks at those two check boxes and does some math. Basically, this:

    [sum] = ([tent]*20) + ([elec]*20) + ([spots]*20)

    Then I would like to have PayPal use [sum] as the value to be charged. I think I have that last part, it is the math part I am not sure about.

Viewing 12 replies - 1 through 12 (of 12 total)
  • anonymized-15380454

    (@anonymized-15380454)

    I don’t think it can.

    But it’s possible using a custom PHP function.

    Thread Starter vader7071

    (@vader7071)

    Thank you. Searching I found this: https://contactform7.com/dom-events/, but I can’t find one that triggers just before send.

    Do you know of a tutorial where I can read up on how to add the function?

    anonymized-15380454

    (@anonymized-15380454)

    I can help you write it. But can you share the form-tags (e.g. [text tent]) for that three fields? (i.e. Spots, Tent, and Electrical)

    And the form ID as in the following example, which is 5:

    [contact-form-7 id="5" title="Contact form 1"]

    • This reply was modified 6 years, 2 months ago by anonymized-15380454.
    Thread Starter vader7071

    (@vader7071)

    Oh terrific. Thank you!!

    Here is the exact CF7 code I am using:

    [contact-form-7 id="490" title="Clan Contact Form"]

    <label> How many spots 15x15 (Clan supplied tent) $20/ea
        [number* tents min:1 max:5 placeholder "0"] </label>
    
    <label> How many spots 15x15 (SEAHG supplied tent) $40/ea
        [number* seahg-tents min:0 max:1 placeholder "0"] </label>
    
    Will you need power? $20
        [checkbox* power use_label_element exclusive "yes" "no"]

    So the “math” will be:
    [sum] = {([tents]*20) + ([seahg-tents]*40) + ([power]*20)}

    Basically I will need [power] not to be a T/F or a Y/N, but treated as a 1/0.

    And then once this is run, I’d like [sum] to be accessible by the Paypal addon feature.

    Basically the idea is a person is registering, they select what they want/need, and then when they hit submit, they can pay via paypal.

    • This reply was modified 6 years, 2 months ago by vader7071.
    • This reply was modified 6 years, 2 months ago by vader7071.
    • This reply was modified 6 years, 2 months ago by vader7071.
    anonymized-15380454

    (@anonymized-15380454)

    First off, replace this tag:

    [checkbox* power use_label_element exclusive "yes" "no"]

    .. with this one:

    [checkbox* power use_label_element exclusive "yes|1" "no|0"]

    Next, if you haven’t, add this before the submit button:

    <input type="hidden" name="sum" />

    Now here is the code: (don’t forget to change the $form_id‘s value)

    add_filter( 'wpcf7_posted_data', function ( $data ) {
    	// Set this to the form ID.
    	$form_id = 490;
    
    	/*
    	 * This is necessary, which ensures that we are filtering/modifying only
    	 * the data submitted via the form with the ID of $form_id and not all forms!
    	 */
    	$form = wpcf7_get_current_contact_form();
    	if ( ! $form || $form_id != $form->id() ) {
    		return $data;
    	}
    
    	// [sum] = {([tents]*20) + ([seahg-tents]*40) + ([power]*20)}
    	$data['sum'] = $data['tents']*20 + $data['seahg-tents']*40 + $data['power']*20;
    
    	return $data;
    } );

    You can add the code to the theme’s functions.php file; or if you are not familiar with that, you can use My Custom Functions.

    Last but not least, the code is optimized for the form-tags (or fields) that you provided. Hence, if you change the field name, type, etc., you should remove/disable or no longer use the code, unless of course you can adjust the code accordingly.

    • This reply was modified 6 years, 2 months ago by anonymized-15380454. Reason: Changed `$form_id` from `5` to `490`
    • This reply was modified 6 years, 2 months ago by anonymized-15380454.
    Thread Starter vader7071

    (@vader7071)

    Awesome! thank you!

    I made the changes (fixing power to “yes|1” “no|0” and adding the hidden field).

    I can follow the code and it makes sense. I added the code, as shown, to the bottom of my functions.php file in my theme.

    I ran a test message. The webpage is:
    https://wiregrasshighlandgames.com/wp/clans-contact

    When I filled out the form, clicked submit, it went to:
    https://wiregrasshighlandgames.com/wp/clans-contact/#wpcf7-f490-p495-o1

    with nothing on the screen. I also have not gotten the email. Paypal is not turned on yet, I wanted to make sure the data was sent through first.

    To make sure, here is my form:

    <label> Clan Name (required)
        [text* your-clan] </label>
    
    <label> Clan Contact (required)
        [text* your-name] </label>
    
    <label> Contact Email (required)
        [email* your-email] </label>
    
    <label> Contact Phone Number (required)
        [tel* your-phone placeholder "xxx-xxx-xxxx"] </label>
    
    <label> Mailing Address (required)
        [text* mailing] </label>
    
    <label> City, State, Zip (required)
        [text* csz placeholder "city, state  zip"] </label>
    
    <label> Website
        [url your-website] </label>
    
    <label> How many spots 15x15 (Clan supplied tent) $20/ea
        [number* tents min:1 max:5 placeholder "0"] </label>
    
    <label> How many spots 15x15 (SEAHG supplied tent) $40/ea
        [number* seahg-tents min:0 max:1 placeholder "0"] </label>
    
    Will you need power? $20
        [checkbox* power use_label_element exclusive "yes|1" "no|0"]
    
    <label> Comments
        [textarea your-message] </label>
    
    <label> <a href="/terms-conditions">Terms & Conditions</a>
        [acceptance terms] <a href="/terms-conditions">I accept the <u>terms and conditions</u></a></label>
    
    <input type="hidden" name="sum" />
    
    [submit "Send"]

    Here is my Mail:

    From: [your-name] <[your-email]>
    Subject: Clan Application
    
    Message Body:
    Business Name:  [your-clan]
    Business Contact:  [your-name]
    Email:  [your-email]
    Phone:  [your-phone]
    Mailing address:  [mailing]
    City, State, Zip:  [csz]
    Website:  [your-website]
    How many spots?  [tents]
    How many tents?  [seahg-tents]
    Will need power?  [power]
    Comments:  [your-message]
    
    Registration fee:  [sum]
    
    -- 
    This e-mail was sent from a contact form on Southeast Alabama Highland Games

    And this is what I added to functions.php:

    // Added for Contact Form 7 to link Registration form to Paypal
    // Clan Application
    add_filter( 'wpcf7_posted_data', function ( $data ) {
    	// Set this to the form ID.
    	$form_id = 490;
    
    	/*
    	 * This is necessary, which ensures that we are filtering/modifying only
    	 * the data submitted via the form with the ID of $form_id and not all forms!
    	 */
    	$form = wpcf7_get_current_contact_form();
    	if ( ! $form || $form_id != $form->id() ) {
    		return $data;
    	}
    
    	// [sum] = {([tents]*20) + ([seahg-tents]*40) + ([power]*20)}
    	$data['sum'] = $data['tents']*20 + $data['seahg-tents']*40 + $data['power']*20;
    
    	return $data;
    } );
    Thread Starter vader7071

    (@vader7071)

    [double by accident]

    • This reply was modified 6 years, 2 months ago by vader7071.
    Thread Starter vader7071

    (@vader7071)

    As is with a good programmer, I tested further.

    I loaded the “Custom Functions” plugin you suggested. That fixed the mail part. Apparently I did something wrong in functions.php. But this works, so I am happy.

    The data did come through as it was supposed to. So when I set my values to (1) $20 spot, and (1) $40 spot and yes to power, I got 80 out.

    Now, I did go to the Paypal tab and tried to update that. Under that tab it has a spot for “item price”. In that I put in [sum], just like in my mail tab. When I saved it, it takes out the [sum] and replaces it with 0.00.

    • This reply was modified 6 years, 2 months ago by vader7071.
    • This reply was modified 6 years, 2 months ago by vader7071.
    • This reply was modified 6 years, 2 months ago by vader7071.
    Thread Starter vader7071

    (@vader7071)

    Update on the paypal addin. I think I found out why. I have to go pro to be able to link tags to paypal. So that is not an issue with CF7, that is between me and the plugin.

    Thread Starter vader7071

    (@vader7071)

    Thank you for all of your help!

    anonymized-15380454

    (@anonymized-15380454)

    I hope my code didn’t give you too much trouble. Plus, I’m sorry, I should have tested the [sum] (or other tags) with that PayPal add-on plugin.

    Anyway, all the best! šŸ™‚

    Thread Starter vader7071

    (@vader7071)

    No, your code was perfect. It did exactly what I wanted. I just put it into the function file wrong. And using the custom function plugin made it easy.

    As for the paypal plugin, the author has hard coded the free version to only accept numbers unless you upgrade to the pro. So there was no way for you to get it to work, and I wouldn’t want to try and override his work.

    I’m talking with the owner about what they want to do now, but just this addition will make them very happy!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Can Contact Form 7 do simple math?’ is closed to new replies.