I'm running a class registration site using Commerce. Each class that is held will have an organic group that holds documents/notes. The goal is to have users purchase a class registration, then become a member of the appropriate group.

I've managed to create a Rule that has a condition of "Order contains a particular product", which looks at the SKU using direct input mode. Then I've added an "Add entity to group" action which, again, I've had to use the direct input mode and manually select the group. It works, but what I'm looking for is automation.

Here's my process, so far:

1) Create "class registration" product
2) Create a piece of "class" content, which references the class registration product
3) On saving the "class" content, I've set a rule to automatically create a new group entity that has the same name of the product. This saves the admin a step in having to create a group.

My Rule, thus far consists of:

EVENTS
Completing the checkout process

CONDITIONS
Order contains a particular product (Parameter: Order: [commerce_order], Product SKU: CLASS-ELECTRICIAN-Spring-2012)

ACTIONS
Add entity to group (Parameter: Entity: [commerce-order:owner], Group: 1)

Using Rules, how can I automate the customer to be added to the organic group? Rather than me having to create a separate Rule for each time we put on a class, there must be a way to automatically set this up. I've tried for hours using various "Fetch entity by property", "Entity has field", etc... conditions/actions, but to no avail (with my amateurish Rules knowledge).

Any ideas?

Comments

rszrama’s picture

Status: Active » Fixed

Does OG have a group reference field? If so, you could add the reference field (or use a generic Entity Reference field) to your product type and select the related group when you create the product. Your Rule could then just look for the value of that field for the group the customer should be added to.

trevorkjorlien’s picture

I've created an Entity Reference field in my product type, and I've selected one of my organic groups.

I've created a new Checkout Rule with a condition "Entity has field (Parameter: Entity: [commerce-order], Field: field_group_reference)". However, I feel this is wrong, as I can't select 'field_group_reference' anywhere when I try to make an action below.

The real problem here – I think – is that on a Checkout Rule, I can't access my custom fields in my product. Does this involve me having to use a Component to access that custom field I made in the product type?

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

cherrysuede’s picture

did you get this working? i'm looking to do the same thing

i tried an entity reference on the product - which you can access in a component in rules - but then i can't get the data type to match

trevorkjorlien’s picture

OK, I haven't solved it, but I think I'm getting closer. Like my first post in #2 above, I've created an Entity Reference field on my product type (field_og_reference).

Then in my checkout rule, I've created two conditions:

1) - Entity has field - commerce-order:commerce-line-items:0
- Field - commerce_product

2) - Entity has field - commerce-order:commerce-line-items:0:commerce-product
- Field - field_og_reference

Basically, these two conditions allow me to access the my Entity Reference field. I believe (hrmm, hrmm...)

Then, I've created an action to add the user to the group:

- Subscribe user to group
- User - account-fetched:0
- Group - commerce-order:commerce-line-items:0:commerce-product:field-og-reference:group

Note that the way you select your user may be different than mine. When I test this out by purchasing the product, it does not add the user to the group. Argh.

FilipNest’s picture

Just in case anyone is stuck on this, the only way I've got this working is using rules alongside views bulk operations.

To do this:

Install VBO and in a "When an order is first paid in full" rule add an action to loop over a list of entities from a VBO view. You'll be passing the current order ID to this view.

The view itself is a bit relationship heavy but goes as follows:

View of type: Commerce Order
Contextual Filters = Commerce Order: Order ID

Relationships (these have to be added in order as each one makes the next appear):

1) Commerce Order: Line items
2) (Line Item) Commerce Line item: Product
3) (Product) Commerce Product: Node referencing products from field_product_reference
4) (Node referencing products from field_product_reference) Group membership: Node group membership

The bracketed bits are references to the other relationships.

After that's done, add two fields in the display:

1) (Node referencing products from field_product_reference) Content: Title
2) (Node referencing products from field_product_reference) Bulk operations: Content (just to get it to show up in Rules and work)

That should pull up a list of all the groups associated with any products in the order (you won't need any extra reference fields aside from the usual product reference on the group node itself. Unless you're not pressing add to cart from that node directly.).

Back to your Rule configuration, as an action under the looping over entities, just add subscribe user to this group and select the entity ID.

There are bound to be better ways of doing this (I'm sure there's a really quick PHP snippet that does the same thing, quicker) so please let me know if there are. Otherwise I hope the above works and someone finds it helpful.

trevorkjorlien’s picture

Hey @Blue_Jumpers_Filip, great work! Actually, I figured this out a few weeks ago and haven't had a chance to update this issue. Now I wish I had done it sooner to help you out. Here's how I solved it:

1) Using the Entity Reference module, I've created an entity reference on my product type. It's target type is "Node", and the Target Bundle is the content type of my Organic Group. In my case, that's called "Classroom".

2) I create a piece of content of type "Classroom".

3) I create a new product and a drop-down will show a list of all my Classrooms. I select one of them.

4) Now comes the time to make the rule to automatically add the user to the correct group.

Conditions
Event: Completing the checkout process

Conditions
Entity has field: commerce-order:commerce-line-items:0
.. Name of the field to check for: commerce_product

Entity has field: commerce-order:commerce-line-items:0:commerce-product
.. Name of the field to check for: field_og_reference (this is the name of my entity reference that I put on the product in Step #1).

Actions
Fetch entity by property.
.. Value: User
.. Property: Email
.. Value: commerce-order:mail
.. Limit result count: 1
.. Variable Label: Fetched Account
.. Variable Name: account_fetched

Subscribe User to Group
.. User: account-fetched:0
.. Group: commerce-order:commerce-line-items:0:commerce-product:field-og-reference:group

Now when the user buys the product that has a reference to the group, they will be subscribed. I haven't outlined it here, but I've made a few other rules that will automatically create an Organic Group when a new product is made and reference the newly made group automatically. It saves a step for the client and reduces the chance of them selecting the wrong group.

I've also made a few other rules for other instances: sometimes the person buying the product is not the person who should be signed up. I've made a few other fields which ask for an email address/name and create a new user on purchase if that email address is different than the customer account email. That's a whole other post in itself, though.

--

While writing this, I looked at my last post on here and I don't see a difference in what I've done. For some reason, what I've just outlined above seems to work. Maybe it's me writing a more specific post and being more careful with the rules? Hmm, hmm... bulk bag.

FilipNest’s picture

Thanks! I'll have a go at this method as well. Ideally you'd be able to do it without an entity reference field (as the entities are actually related already) or without my hacky views way but it seems like you can't.

Yours is smoother than mine of course. The only thing I'm not sure about is the "Entity has field: commerce-order:commerce-line-items:0" part. What if the order contains five different items, only one of which is the purchase of the group? Isn't :0 only the first item?

trevorkjorlien’s picture

Ever since I've started using Commerce/Rules, I've wondered about the whole :0 thing. It usually goes up to :3 for me all the time. Can anybody confirm if what @Blue_Jumpers_Filip says is true?

If that's the case, then I'll have to change some Rules...

rszrama’s picture

You probably shouldn't be using those tokens, as they refer just to specific line items referenced by an order. Instead I would loop over commerce-order:commerce-line-items and pass each line item to a Rules component for individual conditional processing.

Anonymous’s picture

@trevorkjorlien, this is great! Thanks so much. Some people might run into what I did, so I figure I'll document it. The last step in your comment 7: "Subscribe user to group" I didn't get those options in the data selectors. I get "site...", "entity fetched...", and "commerce order". I used "commerce order:owner", and that worked fine.

Rosamunda’s picture

THANK YOU trevorkjorlien!!!
Your explanation was exactly what I needed!
Very neat!
Thanks!
Rosamunda

Rosamunda’s picture

Nope, it didn´t worked, and don´t know why...
The user gets created but it won´t get added to the group...
I see this error at the log: "The variable or parameter group is empty."

Rosamunda’s picture

Status: Closed (fixed) » Active

I´ve tried #6 too, and I get this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'commerce_product_field_data_commerce_product__og_membership.og_group_ref_target_id' in 'on clause'

rszrama’s picture

Status: Active » Closed (fixed)

I'm going to leave this one closed as well. This just isn't a great place to find people who are using OG with Commerce as the queue is reserved specifically for Commerce tasks and bug reports.

Rosamunda’s picture

Oh I see. Where should I ask then? I´ve already asked at DrupalCommerce without any reply or clue... yet. (http://www.drupalcommerce.org/discussions/7405/how-subscribe-user-group-...)
Anyway, after reading all those other comments above I thought I should ask this here...

silkogelman’s picture

I decided to dedicate my morning to writing a tutorial about this, have a look:
http://s1l.org/selling-access-organic-groups-drupal-commerce

The Feature can be found at http://drupal.org/sandbox/s1l/1944556.
Please post issues regarding this in the issue queue of that sandbox.

Rosamunda’s picture

THANK YOU SO MUCH SIL!
This was a very much needed feature (a lot of answerless questions out there!)

rszrama’s picture

For anyone who comes across this later, http://www.drupalcommerce.org/questions is the place to ask questions. Support requests in the queue will be closed out and redirected there. We want to build a single audience where people can ask questions and provide answers - spreading the same request out across multiple channels just means people will continue to be frustrated when they go to your question looking for an answer only to see no activity b/c you got an answer in a closed issue in the issue queue that was never linked back in.

And thanks for the awesome tutorials, s1l!