Handle auto-renew correctly on original-price-sets
authoreileen <emcnaughton@wikimedia.org>
Thu, 4 Jan 2024 07:14:29 +0000 (20:14 +1300)
committereileen <emcnaughton@wikimedia.org>
Thu, 4 Jan 2024 09:14:02 +0000 (22:14 +1300)
CRM/Contribute/Form/Contribution/Main.php
CRM/Price/BAO/PriceField.php

index 5a82759d0ae6f04caccaaf518f3c901e7620a33f..8b5544e590f0b295bec7c9d31ea97d663159ca37 100644 (file)
@@ -551,6 +551,17 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
             ];
           }
 
+          if (!empty($field['options'])) {
+            foreach ($field['options'] as $option) {
+              if (!empty($option['membership_type_id.auto_renew'])) {
+                $extra += [
+                  'onclick' => "return showHideAutoRenew(CRM.$(this).data('membershipTypeId'));",
+                ];
+                $this->assign('membershipFieldID', $fieldID);
+              }
+            }
+          }
+
           CRM_Price_BAO_PriceField::addQuickFormElement($form,
             'price_' . $fieldID,
             $field['id'],
@@ -623,8 +634,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
     $this->addOptionalQuickFormElement('auto_renew');
     $this->addExpectedSmartyVariable('renewal_mode');
     if ($this->_membershipBlock) {
-
-      $membershipTypeIds = $membershipTypes = $radio = $radioOptAttrs = [];
+      $membershipTypeIds = $membershipTypes = $radio = [];
       // This is always true if this line is reachable - remove along with the upcoming if.
       $membershipPriceset = TRUE;
 
@@ -658,7 +668,6 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
           if ($memType['is_active']) {
             $autoRenewMembershipTypeOptions["autoRenewMembershipType_{$value}"] = $this->getConfiguredAutoRenewOptionForMembershipType($value);
             if ($allowAutoRenewOpt) {
-              $javascriptMethod = ['onclick' => "return showHideAutoRenew( this.value );"];
               $allowAutoRenewMembership = TRUE;
             }
             else {
@@ -667,14 +676,12 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
 
             //add membership type.
             $radio[$memType['id']] = NULL;
-            $radioOptAttrs[$memType['id']] = $javascriptMethod;
             if ($cid) {
               //show current membership, skip pending and cancelled membership records,
               $membership = $memberships[$memType['id']] ?? NULL;
               if ($membership) {
                 if ($membership["membership_type_id.duration_unit:name"] === 'lifetime') {
                   unset($radio[$memType['id']]);
-                  unset($radioOptAttrs[$memType['id']]);
                   $this->assign('hasExistingLifetimeMembership', TRUE);
                   unset($memberships[$memType['id']]);
                   continue;
index 39803c6d4f6a63bd97e942268a6e54346446bcbd..a161fae72659c69566b4909ea0cc8576b6ac2556 100644 (file)
@@ -372,16 +372,9 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
             'data-amount' => $opt[$valueFieldName],
             'data-currency' => $currencyName,
             'data-price-field-values' => json_encode($customOption),
+            'data-membership-type-id' => $opt['membership_type_id'] ?? NULL,
             'visibility' => $visibility_id,
           ] + $incomingExtra;
-          // @todo - move this back to the only calling function on Contribution_Form_Main.php
-          if ($field->name == 'membership_amount') {
-            $extra += [
-              'onclick' => "return showHideAutoRenew({$opt['membership_type_id']});",
-              'membership-type' => $opt['membership_type_id'],
-            ];
-            $qf->assign('membershipFieldID', $field->id);
-          }
 
           $choice[$opt['id']] = CRM_Utils_String::purifyHTML($priceOptionText['label']);
           $choiceAttrs[$opt['id']] = $extra;
@@ -412,7 +405,10 @@ class CRM_Price_BAO_PriceField extends CRM_Price_DAO_PriceField {
           }
 
           $choice['0'] = '<span class="crm-price-amount-label">' . $none . '</span>';
-          $choiceAttrs['0'] = ['price' => json_encode([$elementName, '0'])];
+          $choiceAttrs['0'] = [
+            'price' => json_encode([$elementName, '0']),
+            'data-membership-type-id' => NULL,
+          ] + $incomingExtra;
         }
 
         $element = &$qf->addRadio($elementName, $label, $choice, [], NULL, FALSE, $choiceAttrs);