9

I'm helping a group that has a small Commerce store on the site (Drupal 7, CiviCRM 4.6.9) and I'd love to be able to push data from Commerce purchases back into CiviCRM. I have seen some general talk about CiviCRM Entity and Rules integration being able to do this, but I'm curious if anyone has a recipe that is working for them that they would be willing to post -- including the Rule's event(s), condition(s), and action(s) to do this.

I'm assuming the event should be upon successful checkout in Commerce, but I haven't determined the best way to handle conditions and actions.

Event, Condition, Action

Ideally I'd love to be able to do the following:

  • Push first name, last name, billing address into the CiviCRM contact that is already created (but just contains email address)
  • On a product type basis (or if necessary, a line item basis) push a contribution onto this contact record, tagged to the appropriate contribution type. (e.g. we have set up the ability to 'add a donation to your order' in the Commerce shopping cart, so we'd like to be able to split apart the physical product purchase into a 'store purchase' contribution, and push any add-on donation as a 'donation' contribution).
3

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:

Commerce Donate

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

  1. Events: Completing the checkout process
  2. 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.
  3. Actions:

    • Fetch entity by id with Entity type = "CiviCRM Contact" and Identifier = "commerce-order:owner:civicrm-contact:id" Entity type/Identifier
    • 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) Actions

Second rule: Add contribution info to CiviCRM on store order

  1. Events: Completing the checkout process
  2. 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.
  3. Actions:

    • Fetch entity by id with Entity type = "CiviCRM Contact" and Identifier = "commerce-order:owner:civicrm-contact:id" Entity type/Identifier
    • 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. Add a variable
    • 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) Switch
    • 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. Case
    • 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. Case Donation
    • 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

Create new entity

  • 2
    This is fantastic! i ran into the the date convert issue in rules. There are a couple of approaches off the top of my head: 1) fix field in civicrm-entity (see drupal.org/node/2314125#comment-9112569) 2) convert in rules - drupal.org/project/rules_data_transforms (you may want a separate rule using variables to simplify or 3) use the API directly in rules which is a bit of a hack ... – Sonicthoughts Oct 21 '15 at 19:15
  • @Sonicthoughts Just tried a test with your suggestion #2, doing a convert to text on the date, and it inserted a contribution record with this date: "October 22nd, 3474 2:45 PM" !! – Laryn - CEDC.org Oct 22 '15 at 19:53
  • When you are comfortable with it please push to github and let's try to get the community to maintain it. – Sonicthoughts Oct 22 '15 at 23:58
  • I haven't found the time to finish this off, but just updated to note another problem area (permissions). – Laryn - CEDC.org Dec 15 '15 at 17:11
  • 1
    @petednz-fuzion I added a link to Github at the top of my answer: github.com/CEDC/civicrm_drupal_commerce_rules – Laryn - CEDC.org Dec 16 '15 at 4:24
2
+25

As they say ... there is a module for that:commerce civicrm You can also use rules if you want to trigger other actions but it sounds like this may cover a lot of what you need.

  • 1
    When we investigated Commerce CiviCRM a year or so ago, it had a number of hardcoded assumptions (eg which type of address is recorded against a transaction) which led us to not use it; it also doesn't seem to be actively maintained. It may be a good reference/starting point for custom work, but may not suit all use cases well. – Chris Burgess Oct 20 '15 at 1:21
  • 1
    And if I remember correctly the module uses an older version of the API. Not a deal breaker, just something to consider. – Allen Hutchison Oct 20 '15 at 2:23
  • which is why we (eileen) headed down the Rules approach instead of the module - but the client we thought was going to let us get it all packaged nicely as as starter kit for others put the project on hold ;( – petednz - fuzion Oct 20 '15 at 3:47
  • Thanks -- yes, I had seen the module but it looked limited and like it was on the way out. Since I'd seen a lot of talk of using Rules with this I was keen to try that out and see if I could get the custom functionality of different "Financial Types" within a single store order. – Laryn - CEDC.org Oct 21 '15 at 18:17

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.