Extract isMembershipPriceSet (useForMember)
authorEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 7 Mar 2023 20:40:44 +0000 (09:40 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 7 Mar 2023 20:49:19 +0000 (09:49 +1300)
CRM/Contribute/Form/Contribution/Main.php
CRM/Contribute/Form/ContributionBase.php

index d09ea9c2533d20c0b3ab9992a2c2cc7f22258495..755e0b4d784524ac67f11d5b8f4bd53b2bb045be 100644 (file)
@@ -30,8 +30,6 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
 
   public $_membershipTypeValues;
 
-  public $_useForMember;
-
   /**
    * Array of payment related fields to potentially display on this form (generally credit card or debit card fields). This is rendered via billingBlock.tpl
    * @var array
@@ -237,7 +235,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
 
     $entityId = $memtypeID = NULL;
     if ($this->_priceSetId) {
-      if (($this->_useForMember && !empty($this->_currentMemberships)) || $this->_defaultMemTypeId) {
+      if (($this->isMembershipPriceSet() && !empty($this->_currentMemberships)) || $this->_defaultMemTypeId) {
         $selectedCurrentMemTypes = [];
         foreach ($this->_priceSet['fields'] as $key => $val) {
           foreach ($val['options'] as $keys => $values) {
@@ -357,24 +355,16 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
     }
 
     //build pledge block.
-    $this->_useForMember = 0;
     //don't build membership block when pledge_id is passed
     if (empty($this->_values['pledge_id']) && empty($this->_ccid)) {
       $this->_separateMembershipPayment = FALSE;
       if (CRM_Core_Component::isEnabled('CiviMember')) {
-
-        if ($this->_priceSetId &&
-          (CRM_Core_Component::getComponentID('CiviMember') == CRM_Utils_Array::value('extends', $this->_priceSet))
-        ) {
-          $this->_useForMember = 1;
-          $this->set('useForMember', $this->_useForMember);
-        }
-
         $this->_separateMembershipPayment = $this->buildMembershipBlock();
       }
       $this->set('separateMembershipPayment', $this->_separateMembershipPayment);
     }
-    $this->assign('useForMember', $this->_useForMember);
+
+    $this->assign('useForMember', (int) $this->isMembershipPriceSet());
     // If we configured price set for contribution page
     // we are not allow membership signup as well as any
     // other contribution amount field, CRM-5095
@@ -392,14 +382,6 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
       }
     }
 
-    if ($this->_priceSetId && empty($this->_ccid)) {
-      $is_quick_config = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', $this->_priceSetId, 'is_quick_config');
-      if ($is_quick_config) {
-        $this->_useForMember = 0;
-        $this->set('useForMember', $this->_useForMember);
-      }
-    }
-
     //we allow premium for pledge during pledge creation only.
     if (empty($this->_values['pledge_id']) && empty($this->_ccid)) {
       CRM_Contribute_BAO_Premium::buildPremiumBlock($this, $this->_id, TRUE);
@@ -530,7 +512,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
       $this->_currentMemberships = [];
 
       $membershipTypeIds = $membershipTypes = $radio = $radioOptAttrs = [];
-      $membershipPriceset = (!empty($this->_priceSetId) && $this->_useForMember);
+      $membershipPriceset = (!empty($this->_priceSetId) && $this->isMembershipPriceSet());
 
       $allowAutoRenewMembership = $autoRenewOption = FALSE;
       $autoRenewMembershipTypeOptions = [];
@@ -899,7 +881,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
         $errors["price_{$otherAmount}"] = ts('Amount is required field.');
       }
 
-      if ($self->_useForMember == 1 && !empty($check) && $membershipIsActive) {
+      if ($self->isMembershipPriceSet() && !empty($check) && $membershipIsActive) {
         $priceFieldIDS = [];
         $priceFieldMemTypes = [];
 
index cc1abed649bb46935d76a763670d20daa335fed2..5d9c97dfdd09a361a930f716ddfef0b20ac64b4b 100644 (file)
@@ -59,6 +59,13 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
 
   public $_paymentObject = NULL;
 
+  /**
+   * Order object, used to calculate amounts, line items etc.
+   *
+   * @var \CRM_Financial_BAO_Order
+   */
+  protected $order;
+
   /**
    * The membership block for this page
    *
@@ -332,9 +339,8 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     $this->_bltID = $this->get('bltID');
     $this->_paymentProcessor = $this->get('paymentProcessor');
 
-    // This get will ensure it is set for later. Once we are sure all places
-    // access via the get & not directly, it can go.
-    $this->getPriceSetID();
+    $this->order = new CRM_Financial_BAO_Order();
+    $this->order->setPriceSetID($this->getPriceSetID());
     $this->_priceSet = $this->get('priceSet');
 
     if (!$this->_values) {
@@ -1280,6 +1286,26 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
     return $this->_membershipBlock;
   }
 
+  /**
+   * Is a (non-quick-config) membership price set in use.
+   *
+   * @return bool
+   */
+  protected function isMembershipPriceSet(): bool {
+    if ($this->_useForMember === NULL) {
+      if (CRM_Core_Component::isEnabled('CiviMember') &&
+        (!$this->isQuickConfig() || !empty($this->_ccid)) &&
+        CRM_Core_Component::getComponentID('CiviMember') === (int) $this->order->getPriceSetMetadata()['extends']) {
+        $this->_useForMember = 1;
+      }
+      else {
+        $this->_useForMember = 0;
+      }
+      $this->set('useForMember', $this->_useForMember);
+    }
+    return (bool) $this->_useForMember;
+  }
+
   /**
    * Is the contribution page configured for 2 payments, one being membership & one not.
    *