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) »
  • Support »
  • Using CiviCRM »
  • Using CiviEvent (Moderator: Yashodha Chaku) »
  • Implementing Event Discounts/Coupons
Pages: [1] 2

Author Topic: Implementing Event Discounts/Coupons  (Read 3663 times)

skoehn

  • I’m new here
  • *
  • Posts: 14
  • Karma: 1
Implementing Event Discounts/Coupons
February 24, 2010, 02:50:51 pm
Hi, all. I recently began setting up CiviCRM for a non-profit group. We hold an annual conference where speakers & sponsors receive free registrations.
I would like to be able to implement this http://civicrm.org/node/566, but I am having some trouble understanding it.

My first question (which is a big one) is if anyone has successfully implemented and willing to share their module.
Realizing that might be asking too much, is a more step-by-step instruction available for implementing CiviCRM hooks, or will Drupal's documentation on creating modules help me understand how to make this work?

I do have some PHP knowledge and I understand basically what that is saying. I'm just not clear on the details and am not in a position to hire a professional. (Which may mean I'm just out of luck, but I figured it was worth asking!)
Thanks!

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Implementing Event Discounts/Coupons
February 24, 2010, 03:36:35 pm

check:

http://svn.civicrm.org/tools/trunk/drupal/modules/multicurrency/

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

sonicthoughts

  • Ask me questions
  • ****
  • Posts: 498
  • Karma: 10
Re: Implementing Event Discounts/Coupons
March 10, 2010, 05:09:15 pm
Hi - were you able to get Discounts/Coupons working?

skoehn

  • I’m new here
  • *
  • Posts: 14
  • Karma: 1
Re: Implementing Event Discounts/Coupons
April 13, 2010, 01:10:38 pm
I had to take a hiatus for a different project, and I am just now revisiting this. :-)

jimmyjam

  • I post occasionally
  • **
  • Posts: 87
  • Karma: 4
Re: Implementing Event Discounts/Coupons
May 12, 2010, 10:48:32 am
Any updates on your progress? I could really use this feature.

Thanks,
James

skoehn

  • I’m new here
  • *
  • Posts: 14
  • Karma: 1
Re: Implementing Event Discounts/Coupons
May 18, 2010, 10:38:30 am
Not really. When I enable the module, it breaks my whole Drupal installation (after I click Save I just get a white screen & have to manually disable it in the database,) but I have no idea why.

Well, the why is because I still don't know what I'm doing. :-)

skoehn

  • I’m new here
  • *
  • Posts: 14
  • Karma: 1
Re: Implementing Event Discounts/Coupons
May 18, 2010, 10:52:05 am
So...here's what I did, which is clearly wrong, but maybe somebody can guide me from here.

Created civicrm_civitest folder.
Created civicrm_civitest.info
Code: [Select]
name = Discount Coupon Support for CiviCRM
description = CiviTest Coupon Support for a specific Event
version = 3.1
dependencies[] = civicrm
package = CiviCRM
core = 6.x
php = 5.2

Created civicrm_civitest.module
Code: [Select]
<?php
  
// $Id: civicrm_discount.module,v 1.0 2010/05/01 04:17:11 posco Exp $
  
function civitest_civicrm_buildForm( $formName, &$form ) {
    if ( 
$formName == 'CRM_Event_Form_Registration_Register' &&
         
$form->getVar( '_eventId' ) == 3 ) { //use event id here
        
$form->addElement( 'text', 'discountCode', ts( 'Discount Code' ) );

        
// also assign to template
        
$template =& CRM_Core_Smarty::singleton( );
        
$beginHookFormElements = $template->get_template_vars( 'beginHookFormElements' );
        if ( ! 
$beginHookFormElements ) { $beginHookFormElements = array( ); }
        
$beginHookFormElements[] = 'discountCode';
        
$form->assign( 'beginHookFormElements', $beginHookFormElements );

        
$discountCode = CRM_Utils_Request::retrieve( 'discountCode', 'String', $form, false, null, $_REQUEST );
        if ( 
$discountCode ) {
            
$defaults = array( 'discountCode' => $discountCode );
            
$form->setDefaults( $defaults );
        }
    }
}

function 
civitest_civicrm_buildAmount( $pageType, &$form, &$amount ) {
    
$eventID = $form->getVar( '_eventId' );
    if ( 
$pageType != 'event' ||
         
$eventID != 3 ) { // use event ID here
        
return;
    }
    
$discountCode = CRM_Utils_Request::retrieve( 'discountCode', 'String', $form, false, null, $_REQUEST );
    if ( ! 
$discountCode ) { return; }

    list( 
$discountID, $discountPercent, $discountNumber ) = _civitest_discountHelper( $eventID, $discountCode );
    if ( 
$discountNumber <= 0 ) { return; }

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

function 
civitest_civicrm_postProcess( $class, &$form ) {
    
$eventID = $form->getVar( '_eventId' );
    if ( ! 
is_a($form, 'CRM_Event_Form_Registration_Confirm') || $eventID != 3 ) { return; }
        
    
$discountCode = CRM_Utils_Request::retrieve( 'discountCode', 'String', $form, false, null, $_REQUEST );
    if ( ! 
$discountCode ) { return; }

    list( 
$discountID, $discountPercent, $discountNumber ) = _civitest_discountHelper( $eventID, $discountCode );
    if ( ! 
$discountID || $discountNumber <= 0 || $discountNumber == 123456789 ) { return; }

    
$query = "UPDATE civicrm_option_value v SET v.weight = v.weight - 1 WHERE  v.id = %1 AND v.weight > 0";
    
$params = array( 1 => array( $discountID, 'Integer' ) );
    
CRM_Core_DAO::executeQuery( $query, $params );
}

INSERT INTO `civicrm_option_group` (`name`, `description`, `is_reserved`, `is_active`) 
VALUES ('event_discount_3', 'Event Discount', 0, 1); # 3 is the event ID here

SELECT @option_group_id_ed := max(id) from civicrm_option_group where name = 'event_discount_3';

INSERT INTO `civicrm_option_value` (`option_group_id`, `label`, `value`, `name`, `weight`, `is_active`) 
VALUES (@option_group_id_ed, 'Discount Code', 50, '1234XYZ5678', 2, 1);

function 
_civitest_discountHelper( $eventID, $discountCode ) {
    
$sql = "
SELECT v.id as id, v.value as value, v.weight as weight
FROM   civicrm_option_value v,
       civicrm_option_group g
WHERE  v.option_group_id = g.id
AND    v.name = %1
AND    g.name = %2"
;

    
$params = array( 1 => array( $discountCode              , 'String' ),
                     
2 => array( "event_discount_{$eventID}", 'String' ) );
    
$dao = CRM_Core_DAO::executeQuery( $sql, $params );
    if ( 
$dao->fetch( ) ) {
        
// ensure discountPercent is a valid numeric number <= 100
        
if ( $dao->value && is_numeric( $dao->value ) && $dao->value >= 0 &&
             
$dao->value <= 100 && is_numeric( $dao->weight ) ) {
            return array( 
$dao->id, $dao->value, $dao->weight );
        }
    }
    return array( 
null, null, null );
}

Uploaded files into sites/all/modules/civicrm/drupal/modules
Like I said...clearly I'm missing something...

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Implementing Event Discounts/Coupons
May 18, 2010, 11:14:35 am
u r getting the white screen because you have SQL statments (INSERT / SELECT) in the middle of your php file. Not sure where/why you copied those statements, but u just need to "source" them in your civicrm database only once

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

skoehn

  • I’m new here
  • *
  • Posts: 14
  • Karma: 1
Re: Implementing Event Discounts/Coupons
May 18, 2010, 12:46:48 pm
Thank you thank you!!!

I used those statements to update the database and now the module is okay when enabled.

However, I don't see how to actually apply the coupon code. Do I need to create a custom template where the registrant has a form field to enter the code or what?

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Implementing Event Discounts/Coupons
May 18, 2010, 01:26:47 pm

1. you might want to re-read the blog post here: http://civicrm.org/node/566 and make sure u understand it

2. you can send in the discountCode via the URL, i.e. civicrm/event/register?re...&discountCode=USEDISCOUNTCODEHERE

3. if the module is enabled and the discountCode is not passed in, you should see a form element at the very top of the page

4. if you are still having issues, u might want to hire someone from http://civicrm.org/professional/ to help you and potentially build a module which is configurable and makes it easier for other users

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

skoehn

  • I’m new here
  • *
  • Posts: 14
  • Karma: 1
Re: Implementing Event Discounts/Coupons
May 19, 2010, 08:23:57 am
Well, I think I understand it in theory, but obviously I'm not getting it in practice!

I've tried using the URL format mentioned to pass my discount code that way, but it doesn't change any behavior.

I wish I was in position to hire someone, but as I am not, do I have any other options?

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Implementing Event Discounts/Coupons
May 19, 2010, 09:31:24 am

you might want to see if you can find someone in your community willing to donate some time to help you out with this

sorry, no other options at this time. hopefully someone will help take the discount module to the next level sometime soon and make it easier for folks to configure and use

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

skoehn

  • I’m new here
  • *
  • Posts: 14
  • Karma: 1
Re: Implementing Event Discounts/Coupons
May 24, 2010, 09:45:10 am
Can anyone point out to me where I might be missing something or gone wrong?

I have a test event set up where I am trying to get this working: http://kipanet.org/index.php?q=civicrm/event/register&id=5&reset=1&discountCode=speaker

Here is what I have in my .module file:
Code: [Select]
<?php
  
// $Id: civicrm_discount.module,v 1.0 2010/05/01 04:17:11 posco Exp $
  
function discount_civicrm_buildForm( $formName, &$form ) {
    if ( 
$formName == 'CRM_Event_Form_Registration_Register' &&
         
$form->getVar( '_eventId' ) == 5 ) { //use event id here
        
$form->addElement( 'text', 'discountCode', ts( 'Discount Code' ) );

        
// also assign to template
        
$template =& CRM_Core_Smarty::singleton( );
        
$beginHookFormElements = $template->get_template_vars( 'beginHookFormElements' );
        if ( ! 
$beginHookFormElements ) { $beginHookFormElements = array( ); }
        
$beginHookFormElements[] = 'discountCode';
        
$form->assign( 'beginHookFormElements', $beginHookFormElements );

        
$discountCode = CRM_Utils_Request::retrieve( 'discountCode', 'String', $form, false, null, $_REQUEST );
        if ( 
$discountCode ) {
            
$defaults = array( 'discountCode' => $discountCode );
            
$form->setDefaults( $defaults );
        }
    }
}

function 
discount_civicrm_buildAmount( $pageType, &$form, &$amount ) {
    
$eventID = $form->getVar( '_eventId' );
    if ( 
$pageType != 'event' ||
         
$eventID != 5 ) { // use event ID here
        
return;
    }
    
$discountCode = CRM_Utils_Request::retrieve( 'discountCode', 'String', $form, false, null, $_REQUEST );
    if ( ! 
$discountCode ) { return; }

    list( 
$discountID, $discountPercent, $discountNumber ) = _discount_discountHelper( $eventID, $discountCode );
    if ( 
$discountNumber <= 0 ) { return; }

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

function 
discount_civicrm_postProcess( $class, &$form ) {
    
$eventID = $form->getVar( '_eventId' );
    if ( ! 
is_a($form, 'CRM_Event_Form_Registration_Confirm') || $eventID != 3 ) { return; }
        
    
$discountCode = CRM_Utils_Request::retrieve( 'discountCode', 'String', $form, false, null, $_REQUEST );
    if ( ! 
$discountCode ) { return; }

    list( 
$discountID, $discountPercent, $discountNumber ) = _discount_discountHelper( $eventID, $discountCode );
    if ( ! 
$discountID || $discountNumber <= 0 || $discountNumber == 123456789 ) { return; }

    
$query = "UPDATE civicrm_option_value v SET v.weight = v.weight - 1 WHERE  v.id = %1 AND v.weight > 0";
    
$params = array( 1 => array( $discountID, 'Integer' ) );
    
CRM_Core_DAO::executeQuery( $query, $params );
}

function 
_discount_discountHelper( $eventID, $discountCode ) {
    
$sql = "
SELECT v.id as id, v.value as value, v.weight as weight
FROM   civicrm_option_value v,
       civicrm_option_group g
WHERE  v.option_group_id = g.id
AND    v.name = %1
AND    g.name = %2"
;

    
$params = array( 1 => array( $discountCode              , 'String' ),
                     
2 => array( "event_discount_{$eventID}", 'String' ) );
    
$dao = CRM_Core_DAO::executeQuery( $sql, $params );
    if ( 
$dao->fetch( ) ) {
        
// ensure discountPercent is a valid numeric number <= 100
        
if ( $dao->value && is_numeric( $dao->value ) && $dao->value >= 0 &&
             
$dao->value <= 100 && is_numeric( $dao->weight ) ) {
            return array( 
$dao->id, $dao->value, $dao->weight );
        }
    }
    return array( 
null, null, null );
}

I added the following field in civicrm_option_group:
id = 70
name = event_discount_5
label = NULL
description = Event Discount
is_reserved = 0
is_active = 1

And this is in civicrm_option_value:
option_group_id = 70
label = Discount Code
value = 100
name = speaker
weight = 200
is_active = 1

The module is enabled.

skoehn

  • I’m new here
  • *
  • Posts: 14
  • Karma: 1
Re: Implementing Event Discounts/Coupons
May 25, 2010, 07:28:40 am
I figured it out!!!!

I had titled my actual module "civicrm_discount" but in my .module file I only used "discount" when calling the functions instead of "civicrm_discount_xxxx"

When I used the right name in my functions, it worked perfectly!!!

jimmyjam

  • I post occasionally
  • **
  • Posts: 87
  • Karma: 4
Re: Implementing Event Discounts/Coupons
June 10, 2010, 12:32:25 pm
Congratulations! That's great news.

Can you offer any tips or tricks about how less technical folks (like me) can implement this module? It will be really helpful for some of the nonprofit orgs I am helping with event management.

Thank you,
James

Pages: [1] 2
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviEvent (Moderator: Yashodha Chaku) »
  • Implementing Event Discounts/Coupons

This forum was archived on 2017-11-26.