How to subscribe a user to a group when the OG context is in the commerce order? I'm trying to allow people to subscribe to an organic group when buying a specific product.
The thing is that I need to set the OG context in the order, not the product or anywhere else.

Use case: The anonymous user completes an order. The site admin reviews it and then he may or may not grant the anon user the access to a specific role from a list.

How I´m trying to accomplish that:


I've installed Drupal Commerce, Rules and Organic Groups.
I create an OG audience field inside commerce orders (*og_group_ref_commerce*).
I set a rule with some conditions (if the order is set to "completed", and if the user is an anon...) and some actions (a loop to create the new user and assign him a role).


And I'm adding a special action (which doesn´t work):

Subscribe user to group


  User Data selector: account-fetched:0 Group Data Selector:
  commerce-order:og-group-ref-commerce:0


I´ve tried another way, without any success:


  User Data selector: account-created (in and outside the loop that created the user) Group Data Selector: site:og-context--node


Note: As the order doesn´t initially have the group audience context, I have to edit it in order to assign it a group. After saving it (it is still in "pending" status), I edit it again to change it to "completed".

Any ideas?
Try out following these steps

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

Solution 2
See tutorial about this, have a look: