Exported Rules:
The description of what I'm trying to do is below, and you can download (and/or pitch in) on the current versions of the Rules below at this GitHub repo.
Problem Areas:
- In creating the CiviCRM Contribution record, I haven't been able to figure out how to set the Receive Date. I can't input the token for the date of the order because that is of type Date and the CiviCRM Receive Date field only allows data of type Text. Pointers/ideas welcome as currently it leaves the date off completely in the inserted contribution record.
- Also less than ideal is leaving
Fee amount
and Net amount
blank...
- This seems not to work unless the purchaser is an administrator so I've got some further digging to do on permissions
With a lot of trial and error, I have a start on this. This is a work in progress, so feel free to chip in on remaining items if you have an idea or have thought of a better way.
Needed Modules:
Working Elements:
- Add some contact info to the CiviCRM contact
- Add separate contributions to the CiviCRM contact depending on type (store item, donation, shipping fee)
Instructions:
Set up Commerce Donate and have a pane on your checkout to allow someone to add a donation to a store order:
Now go to Rules (Configuration > Workflow > Rules) and create a new rule. I ended up breaking this into two separate rules but after I discovered the Conditional Rules module I'm pretty sure I could have done it in one.
First rule: Add contact info to CiviCRM on store order
- Events:
Completing the checkout process
- Conditions: I don't think any conditions are needed. I have a few in there from testing currently but I don't think they affect anything.
Actions:
Fetch entity by id
with Entity type = "CiviCRM Contact" and Identifier = "commerce-order:owner:civicrm-contact:id"
Fetch entity by id
with Entity type = "User" and Identifier = "site:current-cart-order:uid"
Fetch entity by id
with Entity type = "Commerce Customer profile" and Identifier = "site:current-cart-order:uid"
Set a data value
selecting "civi-contact:first-name" and using the value "[customer:commerce-customer-address:first_name]" (do likewise for any other information you want to pull from the Commerce profile and insert into the appropriate CiviCRM field)
Second rule: Add contribution info to CiviCRM on store order
- Events:
Completing the checkout process
- Conditions: Same as above; I don't think any conditions are needed. I have a few in there from testing currently but I don't think they affect anything.
Actions:
Fetch entity by id
with Entity type = "CiviCRM Contact" and Identifier = "commerce-order:owner:civicrm-contact:id"
- For each contribution type (e.g. donation, store product, shipping fee) do:
Add a variable
to capture the total amount for that type. (e.g. donation_total
) and set it to 0 to start.
- After all variables are added,
Add a loop
to the rule's actions
- Within this loop,
Add a switch
on Data to match against = "line-item:type" (switch functionality is provided by the Conditional Rules module; this is assuming your store products are set up as different types -- e.g. Donations as a donation type)
- For each contribution type again, within this Switch, add a Case (again provided by Conditional Rules) and select the type of product in the Data value. Select "Fall through" as False.
- Within each of these conditional Cases, add two new actions. The first one is to
Calculate a value
(input value 1 as the respective variable you set up above, e.g. donation_total
, and input value 2 as line-item:commerce-total:amount-decimal
and the operator as + for addition). The second action within this case is to Set a data value
on the donation_total
variable, using the value you just calculated.
- After all of the Cases have been added to the Switch, add a new root level Conditional for each product type, to make sure the total for that type is greater than zero before creating a Contribution record. To do this, choose
Add conditional
and then within this conditional, choose Add if
with a "Data comparison" to make sure it isn't empty (or is greater than zero).
Within this final If, we can add a new action to Create a new entity
, selecting CiviCRM Contribution
. Within here, I've added a number of direct input/hard coded values such as:
- Financial Type ID = 1 (for donations, adjust as needed for other types such as a custom 'Store purchase' financial type within CiviCRM)
- Total Amount: use the variable that you created and have updated for this type, e.g. total_donations
- Contact id:
civi-contact:id
- Payment Instrument ID: kind of hackish but I've just entered a hard-coded value for credit card purchase (4)
- You can add a direct input value in the "Contribution source" field and "note" field if you like