CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Tip: Forcing Some Memberships to Be "On Behalf" and others not
Pages: [1]

Author Topic: Tip: Forcing Some Memberships to Be "On Behalf" and others not  (Read 13680 times)

CiviTeacher.com

  • I live on this forum
  • *****
  • Posts: 1282
  • Karma: 118
    • CiviTeacher
  • CiviCRM version: 3.4 - 4.5
  • CMS version: Drupal 6&7, Wordpress
  • MySQL version: 5.1 - 5.5
  • PHP version: 5.2 - 5.4
Tip: Forcing Some Memberships to Be "On Behalf" and others not
July 31, 2013, 07:30:26 am
I encounter a fairly common situation where multiple membership types are used.  Some of these membership types are for individuals only, and some for organizations only.   These org memberships use the "on behalf of" feature of a contribution pages.  The easiest thing would be to separate all the individual memberships onto one contribution page and all the org memberships onto another.   However that's not always possible or desirable.  So I came up with some jQuery to do it for me.

It shows or hides the "on behalf" section and checks the box for you, based on which membership type you select.  It still shows the 'onbehalf of' text as a sort of disclaimer.   My code does not allow a person to maintain their ability to choose 'on behalf of' for any membership type, but it could, if you build jQuery to check what type and uncheck the box accordingly.  That wasn't my use case.

Because including this code in a Main.extra.tpl file will result it in being loaded onto the page more than once, I placed it in a Drupal 7 block instead and set the visibility to civicrm/contribute/transact*

Code: [Select]
<script>
(function (cj) {
    //start with it hidden
    cj("div.is_for_organization-section").hide();
    //then show or hide based on selection
    cj(".membership_amount-section label:contains('Individual')").prev('input').click(function() {
       cj('#is_for_organization').removeAttr("checked").hide();
       showOnBehalf(false);
       cj("div.is_for_organization-section").hide();
    });
    cj(".membership_amount-section label:contains('Organization')").prev('input').click(function() {
       cj('#is_for_organization').attr('checked','checked').hide();
       showOnBehalf(true);
       cj("div.is_for_organization-section").show();
    });
}(jQuery));
</script>

Feel free to use this code, offer feedback, or share your techniques for dealing with the same use case.
« Last Edit: July 31, 2013, 07:32:39 am by Stoob »
Try CiviTeacher: the online video tutorial CiviCRM learning library.

nicolas

  • I post occasionally
  • **
  • Posts: 92
  • Karma: 6
    • cividesk
  • CiviCRM version: 4.4 LTS
  • CMS version: Standalone (yep)
  • MySQL version: 5.1
  • PHP version: 5.3
Re: Tip: Forcing Some Memberships to Be "On Behalf" and others not
September 17, 2013, 12:40:43 pm
Nice one Stoob. A better way to deal with the placement of this Javascript code would be to write a trivial extension as such:
  • create a descriptor xml file
  • create a main <myextension.php> file just implementing the buildForm hook:
Code: [Select]
function <myextension>_civicrm_buildForm($formName, &$form) {
  if ($formName == 'CRM_Contribute_Form_Contribution_Main') {
    if ($form->getVar('_id') == <form # for membership page>) {
      CRM_Core_Region::instance('page-body')->add(array('script' => '
          [...insert your script here...]
      '));
    }
  }
}

If you wanted to include a file rather than have the js inline in the php script you would need to deal with include directories through the civicrm_config hook (and then use civix for it).
cividesk -- CiviCRM delivered ... your way!

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Tip: Forcing Some Memberships to Be "On Behalf" and others not

This forum was archived on 2017-11-26.