1

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.

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.

1

Aidan 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 does what it does. You should probably look at this file in particular.

FWIW, I'd also look at writing to log files rather than emailing yourself with debugging data.

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

2
  • 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. – yurg Mar 9 at 18:24
  • 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. – Aidan Mar 9 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.