Commit 883860df authored by bgm's avatar bgm
Browse files

Merge branch 'PROJ-1243' into 'master'

Extend it so other contribution pages can have discount code

See merge request extensions/discountcontributions!1
parents 42095189 bfc204bf
......@@ -5,33 +5,74 @@ use CRM_Discountcontributions_ExtensionUtil as E;
class CRM_Discountcontributions_Contribute_Form_Contribution_Main {
/**
* @param $form \CRM_Core_Form
*
* @throws \CRM_Core_Exception
* @see discountcontributions_civicrm_buildForm().
*/
static function buildForm(&$form) {
// FIXME: replace this with a setting
if ($form->getVar('_id') == 14) {
// Force CiviDiscount to kick-in on the payment form.
// Based on cividiscount_civicrm_buildForm().
_cividiscount_add_discount_textfield($form);
$code = trim(CRM_Utils_Request::retrieveValue('discountcode', 'String'));
if ($code) {
$form->setDefaults([
'discountcode' => $code,
]);
// don't render the code field if no discount code set
if (!$form->_values['fee'] || !self::hasDiscountFor($form->_values['fee'])) {
return;
}
// Force CiviDiscount to kick-in on the payment form.
// Based on cividiscount_civicrm_buildForm().
_cividiscount_add_discount_textfield($form);
$code = trim(CRM_Utils_Request::retrieveValue('discountcode', 'String'));
if ($code) {
$form->setDefaults([
'discountcode' => $code,
]);
}
}
/**
* Check if there is a discount code set for the price sets
* @param $fee the fee value stored in the form object
*
* @return bool
*/
static private function hasDiscountFor($fee) {
// get a list of price field value ids
$priceFieldValueID = [];
foreach ($fee as $price) {
$ids = array_keys($price['options']);
$priceFieldValueID = array_merge($priceFieldValueID, $ids);
}
// api3 cannot do the filtering we want, so doing it ourselves
try{
$code = civicrm_api3('DiscountCode', 'get', [
'options' => ['limit' => 0],
// 'pricesets' => SOMETHING not work here
]);
} catch (CiviCRM_API3_Exception $e) {
return FALSE;
}
foreach ($code['values'] as $value) {
// no price set set means it applies to all price set - any
if (empty($value['pricesets'])) {
return TRUE;
}
// any one price field value have discount code -> yes
if (!empty(array_intersect($priceFieldValueID, $value['pricesets']))) {
return TRUE;
}
}
return FALSE;
}
/**
* @see discountcontributions_civicrm_buildAmount().
*/
static function buildAmount($pageType, &$form, &$amounts) {
// This is a simplification of the conditions from cividiscoun't buildAmount,
// This is a simplification of the conditions from cividiscount buildAmount,
// but where we look only for pageType == contribution.
if (!(($form->getVar('_action') & CRM_Core_Action::PREVIEW)
|| ($form->getVar('_action') & CRM_Core_Action::ADD)
|| ($form->getVar('_action') & CRM_Core_Action::UPDATE)
|| !($form->getVar('_action') & CRM_Core_Action::ADD)
|| !($form->getVar('_action') & CRM_Core_Action::UPDATE)
)) {
return;
}
......@@ -58,18 +99,12 @@ class CRM_Discountcontributions_Contribute_Form_Contribution_Main {
$currency = CRM_Core_Config::singleton()->defaultCurrency;
}
// [ML] FIXME HARDCODED value to priceset 20 (boutique).
if ($psid != 20) {
return;
}
// [ML] We do not apply any auto-discounts for now.
if (empty($code)) {
return;
}
// [ML] If we match the correct priceset, we apply the discount to all line items.
$applyToAllLineItems = TRUE;
$applyToAllLineItems = FALSE;
$form->set('_discountInfo', NULL);
$discountEntity = 'PriceSet'; // [ML] this does not really make any difference
......@@ -103,6 +138,12 @@ class CRM_Discountcontributions_Contribute_Form_Contribution_Main {
foreach ($discounts as $discountID => $discount) {
$discountApplied = FALSE;
if (!empty($discount['events']) || !empty($discount['memberships'])) {
// this discount is for events/memberships and should not be used here
// FIXME show error?
continue;
}
$priceFields = isset($discount['pricesets']) ? $discount['pricesets'] : [];
foreach ($amounts as $fee_id => &$fee) {
if (!is_array($fee['options'])) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment