Do not delete price_set_entity record while page supports payment
authoreileen <emcnaughton@wikimedia.org>
Mon, 1 Jan 2024 03:39:54 +0000 (16:39 +1300)
committereileen <emcnaughton@wikimedia.org>
Mon, 1 Jan 2024 03:53:19 +0000 (16:53 +1300)
CRM/Contribute/Form/ContributionPage.php
CRM/Contribute/Form/ContributionPage/Amount.php

index 3b8f5a1d6fc0baec46d1cdac47fe1c844550f64d..104e2801dee6a680e57dfb81de53278a851d7516 100644 (file)
@@ -14,6 +14,7 @@
  * @package CRM
  * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
+use Civi\Api4\MembershipBlock;
 
 /**
  * Contribution Page form.
@@ -413,6 +414,18 @@ class CRM_Contribute_Form_ContributionPage extends CRM_Core_Form {
     return $this->_id ? (int) $this->_id : NULL;
   }
 
+  /**
+   * Get the membership Block ID, if any, attached to the contribution page.
+   *
+   * @return int|null
+   */
+  public function getMembershipBlockID(): ?int {
+    return MembershipBlock::get(FALSE)
+      ->addWhere('entity_table', '=', 'civicrm_contribution_page')
+      ->addWhere('entity_id', '=', $this->getContributionPageID())
+      ->addWhere('is_active', '=', TRUE)->execute()->first()['id'] ?? NULL;
+  }
+
   /**
    * Get the price set ID for the contribution page.
    *
index 03092f75e7b8e943b2d2995a354325eeb9eda1e0..69667cabcb0915faa4ce969ea5e1c5cfc7a50112 100644 (file)
@@ -543,10 +543,12 @@ class CRM_Contribute_Form_ContributionPage_Amount extends CRM_Contribute_Form_Co
     $contributionPageID = $contributionPage->id;
 
     // prepare for data cleanup.
-    $deleteAmountBlk = $deletePledgeBlk = $deletePriceSet = FALSE;
-    if ($this->_priceSetID) {
-      $deletePriceSet = TRUE;
-    }
+    $deleteAmountBlk = $deletePledgeBlk = FALSE;
+    // We delete the link to the price set (the price set entity record) when
+    // one exists and there is neither a contribution or membership section enabled.
+    // This amount form can set & unset the contribution section but must check the database
+    // for the membership section (membership block).
+    $deletePriceSet = $this->_priceSetID && !$this->getSubmittedValue('amount_block_is_active') && !$this->getMembershipBlockID();
     if ($this->_pledgeBlockID) {
       $deletePledgeBlk = TRUE;
     }
@@ -558,9 +560,9 @@ class CRM_Contribute_Form_ContributionPage_Amount extends CRM_Contribute_Form_Co
 
       if (!empty($params['amount_block_is_active'])) {
         // handle price set.
+        $deletePriceSet = FALSE;
         if ($priceSetID) {
           // add/update price set.
-          $deletePriceSet = FALSE;
           if (!empty($params['price_field_id']) || !empty($params['price_field_other'])) {
             $deleteAmountBlk = TRUE;
           }
@@ -568,8 +570,6 @@ class CRM_Contribute_Form_ContributionPage_Amount extends CRM_Contribute_Form_Co
           CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageID, $priceSetID);
         }
         else {
-
-          $deletePriceSet = FALSE;
           // process contribution amount block
           $deleteAmountBlk = FALSE;
 
@@ -795,7 +795,6 @@ class CRM_Contribute_Form_ContributionPage_Amount extends CRM_Contribute_Form_Co
           }
           else {
             $deleteAmountBlk = TRUE;
-            $deletePriceSet = TRUE;
           }
         }
       }