Support » Plugin: Give - Donation Plugin and Fundraising Platform » How to create dynamic donation amount? (based on calc)

  • Resolved tmf33

    (@tmf33)


    We have a few different donation forms on our site. The donors are all registered on the site and logged in.

    At one of these form the donation amount should not be inserted / choosen by the donor, but calculated (based on db entry? give_donors_purchase_value).

    -> How can we get a custom (calculated) amount into the [amount] field?

    -> How can we make that field uneditable by donor?

    -> Is there any option to hide the form (or make it unfunctional) after the donation is done?

    Any pointers are greatly appreciated!

    Add

    I thought of eventually having a shortcode that displays the needed calculation output. Then insert the shortcode into [amount] field? Then make amount field uneditable (how?)

    • This topic was modified 8 months, 2 weeks ago by  tmf33. Reason: formatting
    • This topic was modified 8 months, 2 weeks ago by  tmf33. Reason: added thoughts
Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Contributor Ravinder Kumar

    (@ravinderk)

    @tmf33 follow these step:
    1.create set type donation form which accepts the custom amount
    2.use this code as a base to set custom donation amount:

    function __give_custom_fixed_amount( $current_form_id ) {
    	$form_id = 22190;
    
    	// Bailout.
    	if (
    		! is_user_logged_in()
    		|| $form_id !== $current_form_id
    	) {
    		return;
    	}
    
    	// Add code here for donation amount calculation.
    	$custom_amount = 20;
    	?>
    	<script>
    		jQuery(document).ready(function ($) {
    			var formWrapperContainder = $('#give-form-<?php echo esc_js( $form_id ); ?>-wrap'),
    				formContainer = $( 'form', formWrapperContainder ),
    				amountContainer = $('#give-amount', formContainer ),
    				totalDonationAmountContainer = $('#give-final-total-wrap .give-final-total-amount', formContainer );
    
    			amountContainer.val(<?php echo esc_js( $custom_amount ); ?>);
    			amountContainer.prop( 'readonly', true );
    			totalDonationAmountContainer.attr( 'data-total', '<?php echo esc_js( $custom_amount ); ?>' );
    			totalDonationAmountContainer.html( Give.fn.formatCurrency(
    				'<?php echo esc_js( $custom_amount ); ?>',
    				{
    					symbol: Give.form.fn.getInfo( 'currency_symbol',formContainer ),
    					position: Give.form.fn.getInfo( 'currency_position', formContainer )
    				},
    				formContainer
    			) );
    		})
    	</script>
    	<?php
    }
    
    add_action( 'give_before_donation_levels', '__give_custom_fixed_amount' );

    3. Update logic in above code to set $custom_amount.

    Let me know if you need function help.

    Wow! Thank you very much Ravinder! You guys really rock!

    Your posted code goes into /my-theme/functions.php?
    (have made a child theme for being able to survive updates so can insert it directly)

    1.create set type donation form which accepts the custom amount

    Check.

    -> How can we get a custom (calculated) amount into the [amount] field?

    For the calc of our:

    $custom_amount

    Are you able to give me a pointer where to look/start, because the amount needs to be calculated from give_donors_purchase_value (total spent by this donor) * a input variable like eg 0,1.

    So the final calc would be something like
    (give_donors_purchase_value + another_variable)*0,1

    I even thought of doing the calc via wpdatatables plugin (as it´s doing the calc.. and i can input eg 0,1 for mulitplikation)

    Sadly i´m not a coder.. and I´m having a hard time finding one with enough givewp knowledge. So I´m really thankful for your code above and any further input.

    Add
    Would it be a good idea to store some sort of transaction id or the timestamp of amount creation to prevent users from donating that same amount twice? (at some point in future the calced amount will change and will output different amount)

    • This reply was modified 8 months, 1 week ago by  tmf33. Reason: formatting
    • This reply was modified 8 months, 1 week ago by  tmf33.
    • This reply was modified 8 months, 1 week ago by  tmf33.
    Plugin Contributor Ravinder Kumar

    (@ravinderk)

    @tmf33 you can get donor total donation amount by following code.

    $user = wp_get_current_user();
    	$donor = new Give_Donor( $user->ID, true );
    	$donor_total_donation_amount = $donor->id ? give_format_amount( $donor->purchase_value, array( 'currency' => give_get_option('currency') ) ) : 0;
    
    	$custom_amount = $donor_total_donation_amount; // Add your logic here

    @tmf33 If you like plugin then you can rate use: https://wordpress.org/support/plugin/give/reviews/

    Awesome, thank you very very much!

    I guess/hope the rest of it should be straight forward for a coder with wp experience.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to create dynamic donation amount? (based on calc)’ is closed to new replies.