Answers.Body: <p>Aidan is right about the intent of the hook that you're using. It's not really the right tool for the job. If &quot;CiviRules is not an option&quot; then you should look at the <code>civicrm_pre</code> and <code>civicrm_post</code> hooks. You can then use the CiviCRM API to retrieve any relevant Custom Field values for the Membership and take action based on what is returned.</p>
  <p>There are plenty of examples of how to inspect a CiviCRM entity before and after it is saved to the database, but you're welcome to have a look at how <a href="https://github.com/christianwach/civicrm-wp-member-sync" rel="nofollow noreferrer">CiviCRM WordPress Member Sync</a> does what it does. You should probably look at <a href="https://github.com/christianwach/civicrm-wp-member-sync/blob/master/includes/civi-wp-ms-members.php" rel="nofollow noreferrer">this file in particular</a>.</p>
  <p>FWIW, I'd also look at writing to log files rather than emailing yourself with debugging data.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-11T09:50:59.510Z
Answers.LastActivityDate: 2021-03-11T09:50:59.510Z
Answers.UserDisplayName: Christian Wach
Answers.Body: <p>That hook is intended for overriding the effects of the membership status rules ('Current', 'Grace', 'Expired' etc).</p>
  <p>If you want to take action based on membership changes, try <a href="https://civicrm.org/extensions/civirules" rel="nofollow noreferrer">CiviRules</a></p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-09T18:24:07.633Z
Answers.Text: Unfortunately, CiviRules is not an option. And it seems the hook fires even if we change, say,   start/expiration date and a membership custom field in  attached profile. So, it would be very convenient still utilize the hook, once we crack its secrets.
Answers.UserDisplayName: yurg
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-09T19:27:10.200Z
Answers.Text: Why is CiviRules not an option?  You may need to add some conditions to make sure the rules only fire for the particular types of change that are of interest.  
  
  Maybe go back to the beginning and describe what actions you want to take for which membership changes.
Answers.UserDisplayName: Aidan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-09T17:07:47.207Z
Answers.LastActivityDate: 2021-03-09T17:07:47.207Z
Answers.UserDisplayName: Aidan
Body: <p>It seems that hook_civicrm_alterCalculatedMembershipStatus is the right place for adding various actions based on Membership change. It looks like there is some issue with <a href="https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterCalculatedMembershipStatus/" rel="nofollow noreferrer">$membership array from the calling function</a>.</p>
  <p>I v'e started the hook exploration with the code below:</p>
  <pre><code>mail('my@email', 'title', print_r($membership, true)); 
  </code></pre>
  <p>and get two emails with different arrays (with <strong>6043</strong> part is being changed instantly):</p>
  <pre><code>[custom_2_6043] =&gt;
  [custom_4_6043] =&gt;
  [custom_5_6043] =&gt;
  [custom_7_6043] =&gt;
  [custom_10_6043] =&gt;
  [custom_11_6043] =&gt;
  [custom_12_6043] =&gt;
  [custom_94_6043] =&gt; 0
  [custom_19_329] =&gt;
  </code></pre>
  <p>and</p>
  <pre><code>[custom] =&gt; Array
          (
              [2] =&gt; Array
                  (
                      [6043] =&gt; Array
                          (
                              [id] =&gt; 6043
                              [value] =&gt;
                              ...
                          )
  
                  )
  
              [3] =&gt; Array
                  (
                      [6043] =&gt; Array
                          (
                              [id] =&gt; 6043
                              [value] =&gt; 0
                              ...
                          )
  
                  )
              ...
              [94] =&gt; Array
                  (
                      [6043] =&gt; Array
                          (
                              [id] =&gt; 6043
                              [value] =&gt; 0
                              ...
                          )
  
                  )
  </code></pre>
  <p>Since we aim to perform an additional action based on a custom field value, it seems a bit tricky to catch custom field by its key without searching by field key in array or as a part of a [custom_<strong>KEY</strong>_6043] string. Besides, if there are  two emails, it might mean the hook is being executed twice.</p>
  <p>Am I taking right direction here? What would be the simpliest way of getting</p>
  <pre><code>if field value is X [then do Y]
  </code></pre>
  <p>Please advise.</p>
  
Tags: <wordpress><hooks>
Title: hook_civicrm_alterCalculatedMembershipStatus
ThreadText: # hook_civicrm_alterCalculatedMembershipStatus
  
  (Asked by: username_0 on 2021-03-09)
  
  It seems that hook\_civicrm\_alterCalculatedMembershipStatus is the right place for adding various actions based on Membership change. It looks like there is some issue with [$membership array from the calling function](https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterCalculatedMembershipStatus/).
  
  I v'e started the hook exploration with the code below:
  
  ```
  mail('my@email', 'title', print_r($membership, true)); 
  
  ```
  
  and get two emails with different arrays (with **6043** part is being changed instantly):
  
  ```
  [custom_2_6043] =>
  [custom_4_6043] =>
  [custom_5_6043] =>
  [custom_7_6043] =>
  [custom_10_6043] =>
  [custom_11_6043] =>
  [custom_12_6043] =>
  [custom_94_6043] => 0
  [custom_19_329] =>
  
  ```
  
  and
  
  ```
  [custom] => Array
          (
              [2] => Array
                  (
                      [6043] => Array
                          (
                              [id] => 6043
                              [value] =>
                              ...
                          )
  
                  )
  
              [3] => Array
                  (
                      [6043] => Array
                          (
                              [id] => 6043
                              [value] => 0
                              ...
                          )
  
                  )
              ...
              [94] => Array
                  (
                      [6043] => Array
                          (
                              [id] => 6043
                              [value] => 0
                              ...
                          )
  
                  )
  
  ```
  
  Since we aim to perform an additional action based on a custom field value, it seems a bit tricky to catch custom field by its key without searching by field key in array or as a part of a [custom\_**KEY**\_6043] string. Besides, if there are two emails, it might mean the hook is being executed twice.
  
  Am I taking right direction here? What would be the simpliest way of getting
  
  ```
  if field value is X [then do Y]
  
  ```
  
  Please advise.
  
  **Tags:** wordpress, hooks
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2021-03-11. Score: 2 | Accepted answer
  
  username_2 is right about the intent of the hook that you're using. It's not really the right tool for the job. If "CiviRules is not an option" then you should look at the `civicrm_pre` and `civicrm_post` hooks. You can then use the CiviCRM API to retrieve any relevant Custom Field values for the Membership and take action based on what is returned.
  
  There are plenty of examples of how to inspect a CiviCRM entity before and after it is saved to the database, but you're welcome to have a look at how [CiviCRM WordPress Member Sync](https://github.com/username_1/civicrm-wp-member-sync) does what it does. You should probably look at [this file in particular](https://github.com/username_1/civicrm-wp-member-sync/blob/master/includes/civi-wp-ms-members.php).
  
  FWIW, I'd also look at writing to log files rather than emailing yourself with debugging data.
  
  
  ---
  
  ### Answer by username_2 on 2021-03-09. Score: 1
  
  That hook is intended for overriding the effects of the membership status rules ('Current', 'Grace', 'Expired' etc).
  
  If you want to take action based on membership changes, try [CiviRules](https://civicrm.org/extensions/civirules)
  
  > **Comments:**
  > **username_0**: Unfortunately, CiviRules is not an option. And it seems the hook fires even if we change, say,   start/expiration date and a membership custom field in  attached profile. So, it would be very convenient still utilize the hook, once we crack its secrets.  
  > **username_2**: Why is CiviRules not an option?  You may need to add some conditions to make sure the rules only fire for the particular types of change that are of interest.    Maybe go back to the beginning and describe what actions you want to take for which membership changes.  
  
  ---
  
  