/** * Implementation of hook_civicrm_buildForm() * * If the event id of the form being loaded has a discount code, modify * the form to include the textfield. Only display the textfield on the * initial registration screen. * * Works for events and membership. * * @param string $fname * @param CRM_Contribute_Form_Contribution_Main|CRM_Core_Form $form */ function cividiscount_civicrm_buildForm($fname, &$form) { // skip for delete action // also skip when content is loaded via ajax, like payment processor, custom data etc $snippet = CRM_Utils_Request::retrieve('snippet', 'String', CRM_Core_DAO::$_nullObject, false, null, 'REQUEST'); if ($snippet == 4 || $form->getVar('_action') && $form->getVar('_action') & CRM_Core_Action::DELETE) { return false; } // Display discount textfield for offline membership/events if (in_array($fname, array('CRM_Contribute_Form_Contribution', 'CRM_Event_Form_Participant', 'CRM_Member_Form_Membership', 'CRM_Member_BAO_Membership'))) { if ($form->getVar('_single') == 1 || in_array($form->getVar('_context'), array('membership', 'standalone'))) { _cividiscount_add_discount_textfield($form); $code = trim(CRM_Utils_Request::retrieve('discountcode', 'String', $form, false, null, 'REQUEST')); if ($code) { $defaults = array('discountcode' => $code); $form->setDefaults($defaults); } } } else { if (in_array($fname, array('CRM_Event_Form_Registration_Register', 'CRM_Contribute_Form_Contribution_Main', 'CRM_Event_Form_ParticipantFeeSelection'))) { // Display the discount textfield for online events (including // pricesets) and memberships. $ids = $memtypes = array(); $addDiscountField = FALSE; if (in_array($fname, array('CRM_Event_Form_Registration_Register', 'CRM_Event_Form_ParticipantFeeSelection'))) { $contact_id = _cividiscount_get_form_contact_id($form); $discountCalculator = new CRM_CiviDiscount_DiscountCalculator('event', $form->getVar('_eventId'), $contact_id, NULL, TRUE); $addDiscountField = $discountCalculator->isShowDiscountCodeField(); } elseif ($fname == 'CRM_Contribute_Form_Contribution_Main') { $ids = _cividiscount_get_discounted_membership_ids(); if (!empty($form->_membershipBlock['membership_types'])) { $memtypes = explode(',', $form->_membershipBlock['membership_types']); } elseif (isset($form->_membershipTypeValues)) { $memtypes = array_keys($form->_membershipTypeValues); } if (count(array_intersect($ids, $memtypes)) > 0) { $addDiscountField = TRUE; } } if (empty($ids)) { $ids = _cividiscount_get_discounted_priceset_ids(); if (!empty($ids)) { if (in_array($form->getVar('_eventId'), $ids)) { $addDiscountField = TRUE; } } } // Try to add the textfield. If in a multi-step form, hide the textfield // but preserve the value for later processing. if ($addDiscountField) { _cividiscount_add_discount_textfield($form); $code = trim(CRM_Utils_Request::retrieve('discountcode', 'String', $form, false, null, 'REQUEST')); if (empty($code) && $fname == 'CRM_Event_Form_ParticipantFeeSelection') { $code = _cividiscount_get_item_by_track('civicrm_participant', $form->getVar('_participantId'), $contact_id, TRUE); } if ($code) { $defaults = array('discountcode' => $code); $form->setDefaults($defaults); } } } } }
/** * @param string $discountCode * @param array $price_set_amount * @param $cost */ protected function apply_discount($discountCode, &$price_set_amount, &$cost, $event_id) { //need better way to determine if cividiscount installed $autoDiscount = array(); $sql = "select is_active from civicrm_extension where name like 'CiviDiscount%'"; $dao = CRM_Core_DAO::executeQuery($sql, ''); while ($dao->fetch()) { if ($dao->is_active != '1') { return; } } $discounted_priceset_ids = _cividiscount_get_discounted_priceset_ids(); $discounts = _cividiscount_get_discounts(); $stat = FALSE; foreach ($discounts as $key => $discountValue) { if ($key == $discountCode) { $events = CRM_Utils_Array::value('events', $discountValue); $evt_ids = implode(",", $events); if ($evt_ids == "0" || strpos($evt_ids, $event_id)) { $event_match = TRUE; } //check priceset is_active if ($discountValue['active_on'] != NULL) { $today = date('Y-m-d'); $diff1 = date_diff(date_create($today), date_create($discountValue['active_on'])); if ($diff1->days > 0) { $active1 = TRUE; } } else { $active1 = TRUE; } if ($discountValue['expire_on'] != NULL) { $diff2 = date_diff(date_create($today), date_create($discountValue['expire_on'])); if ($diff2->days > 0) { $active2 = TRUE; } } else { $active2 = TRUE; } } if ($discountValue['is_active'] == TRUE && ($discountValue['count_max'] == 0 || $discountValue['count_max'] > $discountValue['count_use']) && $active1 == TRUE && $active2 == TRUE && $event_match == TRUE) { foreach ($price_set_amount as $key => $price) { if (array_search($price['price_field_value_id'], $discounted_priceset_ids) != NULL) { $discounted = _cividiscount_calc_discount($price['line_total'], $price['label'], $discountValue, $autoDiscount, "USD"); $price_set_amount[$key]['line_total'] = $discounted[0]; $cost += $discounted[0]; $price_set_amount[$key]['label'] = $discounted[1]; } else { $cost += $price['line_total']; } } $stat = TRUE; } } return $stat; }