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) »
  • Discussion (deprecated) »
  • Feature Requests and Suggestions (Moderator: Dave Greenberg) »
  • Discount events for members
Pages: [1]

Author Topic: Discount events for members  (Read 2085 times)

mnestor

  • I’m new here
  • *
  • Posts: 9
  • Karma: 2
Discount events for members
April 02, 2010, 09:00:46 pm
So after looking through the civitest.module.sample and asking a lot of dumb questions in IRC I decided, with some prompting from dlobo, to post what I learned. This may not be totally correct and I would appreciate anyone pointing out my mistakes here.

I wanted to give a discount to anyone with a New or Current membership without looping through every membership type like the example was showing.

Code: [Select]
function cividiscount_civicrm_buildAmount( $pageType, &$form, &$amount ) {
    //Create an array of event type ids that are allowed this discount
    $eventTypes = array(1,7); //1 = Class, 7 = Workshop

    if ($pageType != 'event' || !in_array($form->_values['event']['event_type_id'], $eventTypes)) {
        return;
    }

    //try to get the contactid of the person being viewed. This is for when an admin is adding
    // someone else to an event. So that the discount will apply based on the person their adding
    $userID = $form->get('cid');

    //if that fails then we need to find out if the logged in user has a discount because they're
    // viewing the end user registration page
    if (!$userID) {
        $session =& CRM_Core_Session::singleton();
        $userID = $session->get('userID');
    }

    //if we have no user id something failed, just exit and don't give a discount.
    // let them call in to complain about the missing discount
    if (!$userID) {
        return;
    }

    //check for existing membership
    // WHERE status_id IN (1,2)  1=New,2=Current,3=Grace (I don't care to give a discount to
    //  users that have a status of grace)
    // I also add the end_date check here in as a double check in case the status isn't right
    $query = "SELECT membership_type_id
        FROM civicrm_membership
            WHERE status_id IN ( 1,2 )
            AND civicrm_membership.contact_id = {$userID}
            AND (end_date IS NULL OR end_date > NOW())";
    $dao = CRM_Core_DAO::executeQuery( $query );

    $membershipTypeID = null;
    //I really don't know why I chose to put this in a loop. there should only be 1 record
    while ( $dao->fetch( ) ) {
        $membershipTypeID = $dao->membership_type_id;
    }

    if ( ! $membershipTypeID ) {
        return;
    }

    $discountPercent = 10;

    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";
    }
}

Hope this helps someone else in some way.

jimmyjam

  • I post occasionally
  • **
  • Posts: 87
  • Karma: 4
Re: Discount events for members
May 12, 2010, 10:46:03 am
Providing discounts to members who register for an event is exactly what I need to do.

How would I modify this script if I wanted to hide one price option (general admission standard price) and replace it with another (general admission member price) for members who are logged in?

I don't think I can use your script in its current form because I have multiple types of tickets (e.g. VIP, General Admission), and the member discount only applies to one type of ticket (e.g. General Admission but not VIP).

Thank you!

-James

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Discussion (deprecated) »
  • Feature Requests and Suggestions (Moderator: Dave Greenberg) »
  • Discount events for members

This forum was archived on 2017-11-26.