Alter contribution amount options using alterFormDefinition

Removing the default amount

When setting up a Contribution form, you must choose an amount that will be selected by default when a supporter loads the published form. Use this callback if you would prefer that no amount is selected by default on load.

(function() {

    window.nvtag_callbacks = window.nvtag_callbacks || {};
    window.nvtag_callbacks.alterFormDefinition = window.nvtag_callbacks.alterFormDefinition || [];

    function findElementByName(def, name){
        var elements = def.form_elements;
        for(var i = 0; i < elements.length; i++){
            var element = elements[i];
            if(element.name === name){
                return element;
            }
            if(element.children && element.children.length){
                var children = element.children;
                for(var j = 0; j < children.length; j++){
                    var child = children[j];
                    if(child.name === name){
                        return child;
                    }
                }
            }
        }
    }

    function clearDefaultValues(selectAmount){
        if(!selectAmount) return;

        delete selectAmount.default_value;

        if(selectAmount.frequencyOptions){
            for(var i = 0; i < selectAmount.frequencyOptions.length; i++){
                var opt = selectAmount.frequencyOptions[i];
                if(opt && opt.options && opt.options){
                    delete opt.options.default_value;
                }
            }
        }

        if(selectAmount.children){
            for(var i = 0; i < selectAmount.children.length; i++){
                var child = selectAmount.children[i];
                // children of SelectAmount are the currency-specific SelectAmount versions
                // which each have their own frequencyOptions and default values
                clearDefaultValues(child);
            }
        }
    }

    window.nvtag_callbacks.alterFormDefinition.push(function (args){
        var def = args.form_definition;
        if(def.type !== 'ContributionForm') return;

        var selectAmount = findElementByName(def, 'SelectAmount');
        clearDefaultValues(selectAmount);

    });

})();

Note, this callback works on both single and multi-currency forms.

Setting frequency-specific amount options

If you’ve enabled recurring ask amounts on your Contribution form, those ask amounts get applied to each recurring frequency that you have enabled. Use this callback if you would prefer to set frequency-specific ask amounts. The example below sets 6 amounts for the Monthly frequency, and 6 larger amounts for the Yearly frequency.

(function() {

    window.nvtag_callbacks = window.nvtag_callbacks || {};
    window.nvtag_callbacks.alterFormDefinition = window.nvtag_callbacks.alterFormDefinition || [];

    function findElementByName(def, name){
        var elements = def.form_elements;
        for(var i = 0; i < elements.length; i++){
            var element = elements[i];
            if(element.name === name){
                return element;
            }
            if(element.children && element.children.length){
                var children = element.children;
                for(var j = 0; j < children.length; j++){
                    var child = children[j];
                    if(child.name === name){
                        return child;
                    }
                }
            }
        }
    }

    var frequencyLookup = {
        Weekly: 1,
        EveryTwoWeeks: 2,
        EveryFourWeeks: 3,
        Monthly: 4,
        Quarterly: 5,
        Yearly: 6,
        TwiceAYear: 7
    };

    var defaultFrequencyOptions = {};
    defaultFrequencyOptions[frequencyLookup.Monthly] = {
        options: [
            { val: '10.00', display: 'First Monthly' },
            { val: '15.00', display: null },
            { val: '25.00', display: null },
            { val: '35.00', display: null },
            { val: '50.00', display: null },
            { val: '100.00', display: 'Last Monthly' },
            { val:'other', display: 'Other:', other: true }
        ],
        default_value: '15.00'
    };
    defaultFrequencyOptions[frequencyLookup.Yearly] = {
        options: [
            { val: '20.00', display: 'First Yearly' },
            { val: '50.00', display: null },
            { val: '75.00', display: null },
            { val: '100.00', display: null },
            { val: '250.00', display: null },
            { val: '500.00', display: 'Last Yearly' }
            // no "Other" amount
        ],
        default_value: '50.00'
    };

    window.nvtag_callbacks.alterFormDefinition.push(function (args){
        var def = args.form_definition;
        if(def.type !== 'ContributionForm') return;

        var selectAmount = findElementByName(def, 'SelectAmount');
        if(!selectAmount || !selectAmount.frequencyOptions) return;

        for(var i = 0; i < selectAmount.frequencyOptions.length; i++){
            var opt = selectAmount.frequencyOptions[i];
            if(defaultFrequencyOptions[opt.value]){
                opt.options = defaultFrequencyOptions[opt.value];
            }
        }
    });

})();

Note, this callback only works on single-currency forms.