Skip to content

Commit

Permalink
fix(form): set default level when custom is not set
Browse files Browse the repository at this point in the history
ref #3299
  • Loading branch information
ravinderk committed May 31, 2018
1 parent d4e95e2 commit 7401c76
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
14 changes: 14 additions & 0 deletions assets/src/js/frontend/give-donations.js
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,20 @@ jQuery( function( $ ) {

price_id = 'undefined' === typeof price_id ? Give.form.fn.getPriceID( parent_form, true ) : price_id;

// https://github.com/WordImpress/Give/issues/3299
// If we change from custom amount to donation level then
// this event fire twice. First on amount field blur and second time on level button/radio/select click which cause of minimum donation notice.
// This condition will prevent minimum donation amount notice show by set default level.
if( '' === value_now || 0 === value_now ) {
let $default_level = $( '.give-donation-levels-wrap [data-default="1"]', $parent_form );

if( $default_level.length ) {
price_id = $default_level.data('price-id');
this_value = value_now = $default_level.val();
formatted_total = Give.form.fn.formatAmount( value_now, parent_form, {} );
}
}

// Cache donor selected price id for a amount.
Give.fn.setCache( 'amount_' + value_now, price_id, parent_form );
$( this ).val( formatted_total );
Expand Down
9 changes: 6 additions & 3 deletions includes/forms/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,11 @@ function give_output_levels( $form_id ) {
) );

$output .= sprintf(
'<li><button type="button" data-price-id="%1$s" class="%2$s" value="%3$s">%4$s</button></li>',
'<li><button type="button" data-price-id="%1$s" class="%2$s" value="%3$s" data-default="%4$s">%5$s</button></li>',
$price['_give_id']['level_id'],
$level_classes,
$formatted_amount,
array_key_exists( '_give_default', $price ) ? 1 : 0,
$level_text
);
}
Expand Down Expand Up @@ -553,11 +554,12 @@ function give_output_levels( $form_id ) {
) );

$output .= sprintf(
'<li><input type="radio" data-price-id="%1$s" class="%2$s" value="%3$s" name="give-radio-donation-level" id="give-radio-level-%1$s" %4$s ><label for="give-radio-level-%1$s">%5$s</label></li>',
'<li><input type="radio" data-price-id="%1$s" class="%2$s" value="%3$s" name="give-radio-donation-level" id="give-radio-level-%1$s" %4$s data-default="%5$s"><label for="give-radio-level-%1$s">%6$s</label></li>',
$price['_give_id']['level_id'],
$level_classes,
$formatted_amount,
( ( isset( $price['_give_default'] ) && $price['_give_default'] === 'default' ) ? 'checked="checked"' : '' ),
array_key_exists( '_give_default', $price ) ? 1 : 0,
$level_text
);
}
Expand Down Expand Up @@ -593,11 +595,12 @@ function give_output_levels( $form_id ) {
) );

$output .= sprintf(
'<option data-price-id="%1$s" class="%2$s" value="%3$s" %4$s >%5$s</option>',
'<option data-price-id="%1$s" class="%2$s" value="%3$s" %4$s data-default="%5$s">%6$s</option>',
$price['_give_id']['level_id'],
$level_classes,
$formatted_amount,
( ( isset( $price['_give_default'] ) && $price['_give_default'] === 'default' ) ? 'selected="selected"' : '' ),
array_key_exists( '_give_default', $price ) ? 1 : 0,
$level_text
);
}
Expand Down

0 comments on commit 7401c76

Please sign in to comment.