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) »
  • Event Discounts/Coupons for Price Sets
Pages: [1]

Author Topic: Event Discounts/Coupons for Price Sets  (Read 1459 times)

jcm55

  • I post occasionally
  • **
  • Posts: 96
  • Karma: 14
Event Discounts/Coupons for Price Sets
June 24, 2010, 06:36:56 am
So I've created a discount/coupon module that works with price sets by merging two examples.

The code for Event Discounts/Coupons from here:
http://civicrm.org/node/566

And this example code for adding a processing fee to price sets:
http://forum.civicrm.org/index.php?topic=11089.0

This code makes a few assumptions, which work fine for us:
1) The discount applies to every item in the price set
2) One set of coupon codes across all events, and enabled for every event (if the module is enabled)

The discount functionality itself is working -- the price field values on the confirmation page reflect the appropriate discount if you've entered a discount code.  However, I'd like to modify the price field descriptions as well, just to make it perfectly clear that the discount has been applied.  And that part doesn't seem to be working. 

Here's my civicrm_buildForm:

Code: [Select]
function cividiscount_civicrm_buildForm( $formName, &$form ) {
    if ( $formName == 'CRM_Event_Form_Registration_Register' ) {
        $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 );
        }
    }

    if ('CRM_Event_Form_Registration_Register' == $formName ||
        ('CRM_Event_Form_Participant' == $formName && isset($form->_priceSet))) {           
        $discountCode = CRM_Utils_Request::retrieve( 'discountCode', 'String', $form, false, null, $_REQUEST);
        if ( ! $discountCode ) { return; }
        list( $discountID, $discountPercent, $discountNumber ) = _cividiscount_discountHelper( $eventID, $discountCode );
        if ( $discountNumber <= 0 ) { return; }
        foreach($form->_values['fee']['fields'] as $k => $fields) {
            foreach($fields['options'] as $k2 => $options) {
                $form->_priceSet['fields'][$k]['options'][$k2]['value'] = ($form->_priceSet['fields'][$k]['options'][$k2]['value'] - $discountPercent / 100 * $form->_priceSet['fields'][$k]['options'][$k2]['value']);
                $form->_priceSet['fields'][$k]['options'][$k2]['description'] = $form->_priceSet['fields'][$k]['options'][$k2]['description'] . "\t - with {$discountPercent}% discount";
                $form->_values['fee']['fields'][$k]['options'][$k2]['value'] = ($form->_values['fee']['fields'][$k]['options'][$k2]['value'] - $discountPercent / 100 * $form->_values['fee']['fields'][$k]['options'][$k2]['value']);
                $form->_values['fee']['fields'][$k]['options'][$k2]['description'] = $form->_values['fee']['fields'][$k]['options'][$k2]['description'] . "\t - with {$discountPercent}% discount";
            }
        }
    }
}

I suspect the part of the code that's modifying $form->_priceSet['fields'][$k]['options'][$k2]['description'] is not correct.  I've also tried modifying $form->_priceSet['fields'][$k]['options'][$k2]['name'] and $form->_priceSet['fields'][$k]['options'][$k2]['label'] but those don't seem to work either.

Any ideas on what I'm doing wrong?

Once I've got this ironed out, I'll package this up as a module for others to use as well.


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: Event Discounts/Coupons for Price Sets
June 24, 2010, 12:19:46 pm

try modifying:

$form->_priceSet['fields'][$fieldID]['help_post']

i'm guessing a bit here and am a bit jet-lagged, so might be way off base, but that might help (from a quick look at the code)

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

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: Event Discounts/Coupons for Price Sets
July 29, 2010, 05:03:43 pm
Just wondering if this progressed - or maybe i missed it moving to another discussion. Any news?
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

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

This forum was archived on 2017-11-26.