Published
Thursday, November 13, 2008 - 13:47
Written by
Our global meeting resulted in some new features being added to 2.2. Our team worked on a new jQuery based country/state selector and incorporating hook(s) to the "amount" functionality of online contribution and event registration pages. I'll give a brief description of the feature set and functionality of these projects

New jQuery based State Country widget

Our previous widget was a dojo based combo box widget. Both the country and the state were ajax based combo boxes. The state combo box was dependent on the value chosen in the country combo box. This implementation was quite complex and not easily replicated (hence u did not see the widget in many places). For 2.2 we took a slightly different approach: The country field is a simple html select box (the values are restricted by the countries configured in the admin area). The state selector is now dynamic and refreshed with new values whenever the country is changed. We took inspiration and borrowed a fair part of the code from: Simple chained combobox plugin for jQuery. This model is much easier to replicate and we've introduced it in a more places including profiles, contribution pages and event registration pages. It was kinda nice to see the number of lines of js code being deleted with this new scheme. We felt quite bad that we did not think of doing it this way to begin with, but you learn as you grow :)

Extending the "amounts" field in transaction pages

One request that we hear quite often is: How do we make prices for an event dynamic, so our members get a 10% discount (or $10 off). Trying to add this logic via the UI did not seem easy since different folks had different requirements. We also wanted to make similar changes to the contribution pages, so an org could tailor the amounts displayed to a logged in user based on their past giving history (i.e. increase the donation amounts displayed if the donor has given significant amounts in the recent past). We've been incorporating hooks into many places in CiviCRM, and this seemed another good place to use hooks. Deepak and I spent an afternoon reworking the code to facilitate this along with a sample hook implementation. The good news was the code was in pretty good shape that we wrapped the project up quite quickly. We standardized on the amount structure across contributions and events. An example hook implementation which gives a discount to all Student members is as follows:
function civitest_civicrm_buildAmount( $pageType,
                                      &$form,
                                      &$amount ) {

    // lets add an arbitrary amount here, just to show folks                                                                                                   
    // the power of a hook :)                                                                                                                                  
    $amount[1000] = array( 'value'    => 400,
                           'label'     => 'Hook',
                           'amount_id' => 1000 );


    // now lets get a bit more ambitious                                                                                                                       
    // *GOAL*: lets plan to give 20% discount to students                                                                                                      
    $membershipType  = 'Student';
    $discountPercent = 20;

    // get the membership-type-id for the membership-type                                                                                                      
    $membershipTypeId = CRM_Core_DAO::getFieldValue( 'CRM_Member_DAO_MembershipType',
                                                     $membershipType,
                                                     'id',
                                                     'name' );

    // get the logged in user id                                                                                                                               
    $session =& CRM_Core_Session::singleton();
    $userID  = $session->get( 'userID' );

    if ( $userID ) {
        // check if logged in user has 'Student' membership                                                                                                    
        require_once 'CRM/Member/BAO/Membership.php';
        $membership = CRM_Member_BAO_Membership::getContactMembership( $userID, $membershipTypeId, null );

        // If logged in contact is a member as on today, modify the amount                                                                                     
        // to reflect the discount.                                                                                                                            
        if ( CRM_Utils_Array::value( 'is_current_member', $membership ) ) {
            foreach ( $amount as $amountId => $amountInfo ) {
                    $amount[$amountId]['value'] = $amount[$amountId]['value'] -
                        ceil($amount[$amountId]['value'] * $discountPercent / 100);
                    $amount[$amountId]['label'] = $amount[$amountId]['label'] .
                        "\t - with {$discountPercent}% discount (for $membershipType)";
            }
        }
    }
}
Filed under

Comments

we'd like to extend a discount to multiple membership types