/** * Implementation of hook_civicrm_membershipTypeValues() * * Allow discounts to be applied to renewing memberships. */ function cividiscount_civicrm_membershipTypeValues(&$form, &$membershipTypeValues) { // Ignore the thank you page. if ($form->getVar('_name') == 'ThankYou') { return; } // Only discount new or renewal memberships. if (!($form->getVar('_action') & (CRM_Core_Action::ADD | CRM_Core_Action::RENEW))) { return; } // Retrieve the contact_id depending on submission context. // Look for contact_id in the form. if ($form->getVar('_contactID')) { $contact_id = $form->getVar('_contactID'); } else { if (!empty($form->_submitValues['contact_select_id'][1])) { $contact_id = $form->_submitValues['contact_select_id'][1]; } else { $contact_id = CRM_Core_Session::singleton()->get('userID'); } } $form->set('_discountInfo', NULL); $code = CRM_Utils_Request::retrieve('discountcode', 'String', $form, false, null, 'REQUEST'); $discountCalculator = new CRM_CiviDiscount_DiscountCalculator('membership', NULL, $contact_id, $code, FALSE); if (!empty($code)) { $discounts = $discountCalculator->getDiscounts(); } if (!empty($code) && empty($discounts)) { $form->set('discountCodeErrorMsg', ts('The discount code you entered is invalid.')); } if (empty($discounts)) { return; } $discount = array_shift($discounts); foreach ($membershipTypeValues as &$values) { if (!empty($discount['memberships']) && CRM_Utils_Array::value($values['id'], $discount['memberships'])) { list($value, $label) = _cividiscount_calc_discount($values['minimum_fee'], $values['name'], $discount, $discountCalculator->isAutoDiscount()); $values['minimum_fee'] = $value; $values['name'] = $label; } } $form->set('_discountInfo', array('discount' => $discount, 'autodiscount' => $discountCalculator->isAutoDiscount(), 'contact_id' => $contact_id)); }
/** * @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; }