Commerce, Group: add a new user to a certain group after a product purchase

Created on 29 April 2024, 8 months ago
Updated 14 May 2024, 7 months ago

Problem/Motivation

Sorry if this has been already asked & answered or is somewhere across documentation/issues queue.

There is a D10 website which sells "memberships" (Drupal Group module) via Drupal Commerce.
A Commerce Store is "connected" to a Group via entity reference field.
It looks like ECA does have all required tokens: the full chain is [commerce_order:store_id:entity:field_group_reference:id] Unfortunately we can't get this to work because of:
Access denied to Group: add customer as member (group_add_member) from ECA NewOrder (neworder) for event Drupal\commerce_order\Event\OrderEvent: Cannot operate on a non-existing group.
However the group exists and can be seen on the Store Edit page though. Perhaps it worth mentioning the eca_base modeller is being used: at this stage all this BPMN stuff looks too scary to dive into for someone familiar with Drupal 7 Rules.
Please see below the config: perhaps something obvious is missing. Please advice, cheers!

uuid: fa4067bd-f1ab-474e-84bb-213925c3ca6c
langcode: en
status: true
dependencies:
  module:
    - eca_commerce
    - group_action
id: neworder
modeller: core
label: 'Add customer to a Group'
version: ''
weight: -10
events:
  eca_commerce_order_create:
    plugin: 'eca_commerce:order_create'
    label: 'Order: Create'
    configuration: {  }
    successors:
      -
        id: group_add_member
        condition: null
conditions: {  }
gateways: {  }
actions:
  group_add_member:
    plugin: group_add_member
    label: 'Group: add customer as member'
    configuration:
      values: {  }
      operation: create
      content_plugin: group_membership
      group_id: '[commerce_order:store_id:entity:field_group_reference:id]'
      entity_id: ''
      add_method: skip_existing
      object: '[commerce_order:uid:entity]'
      replace_tokens: false
    successors: {  }
πŸ’¬ Support request
Status

Fixed

Version

1.1

Component

User interface

Created by

πŸ‡ΈπŸ‡°Slovakia yurg

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @yurg
  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    This sounds like a permission issue: the current user (the one who purchases the membership) does not have the permission to add themselves to the group. See https://git.drupalcode.org/project/group_action/-/blob/1.0.x/src/Plugin/...

    Your model will probably have to switch the user to a more privileged one to perform this action.

    Perhaps it worth mentioning the eca_base modeller is being used: at this stage all this BPMN stuff looks too scary to dive into for someone familiar with Drupal 7 Rules.

    Don't worry, there is nothing scary about BPMN. In fact, it is so much more efficient in building and reviewing models. That's certainly what we hear from all users who originally had similar reservations.

  • πŸ‡ΈπŸ‡°Slovakia yurg

    @jurgenhaas Thank you for your note! We've been trying to escalate permissions by user switch - should have been easy since a Store owner and a Group creator is the same UID ([commerce_order:store_id:entity:uid] in our case).
    Wrong Successors perhaps?

    uuid: fa4067bd-f1ab-474e-84bb-213925c3ca6c
    langcode: en
    status: true
    dependencies:
      module:
        - eca_commerce
        - eca_user
        - group_action
    id: newordertagall
    modeller: core
    label: 'Add customer to a Group'
    version: ''
    weight: -10
    events:
      eca_commerce_order_create:
        plugin: 'eca_commerce:order_create'
        label: 'Order: Create'
        configuration: {  }
        successors:
          -
            id: eca_switch_account
            condition: null
          -
            id: group_add_member
            condition: null
    conditions: {  }
    gateways: {  }
    actions:
      eca_switch_account:
        plugin: eca_switch_account
        label: 'User: switch current account'
        configuration:
          user_id: '[commerce_order:store_id:entity:uid]'
        successors:
          -
            id: group_add_member
            condition: null
      group_add_member:
        plugin: group_add_member
        label: 'Group: add customer as member'
        configuration:
          values: {  }
          operation: create
          content_plugin: group_membership
          group_id: '[commerce_order:store_id:entity:field_group_reference:id]'
          entity_id: ''
          add_method: skip_existing
          object: '[commerce_order:uid:entity]'
          replace_tokens: false
        successors: {  }
    

  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    I can't verify all the tokens, but commerce_order:store_id:entity:uid is probably not giving you what you think. You could either try with a fixed id 1 to see if superpowers is solving the issue. If even that doesn't work, you may want to move this issue over to the group action module to find out why the access check on that action is failing.

  • πŸ‡ΈπŸ‡°Slovakia yurg

    @jurgenhaas Thank you, it's tokens issue indeed and it should be a [simple] matter of retrieving proper IDs from orders and filling the correct combination of Group ID / UUID, Entity ID / UUID and Entity fields. And finding out why would a member be added twice ( have tried with hard-coded tokens already ).

  • πŸ‡ΈπŸ‡°Slovakia yurg

    Well then, it's all about Tokens indeed: have had to use Rules token chaining to get right ones.
    Here is the list in case someone needs it, basically, we add the "target_id" everywhere:

    - Customer UID: [commerce_order:uid:target_id]
    Makes sense for logged-in user only.

    - Store ID: [commerce_order:store_id:target_id]
    ID of a Store the purchased product belongs to.

    - Store owner ID: [commerce_order:store_id:entity:uid:target_id]

    - Group ID: [commerce_order:store_id:entity:field_group_reference:target_id]
    field_group_reference here is a name of your Store Entity Reference field, referencing Groups.
    Same should work for the other reference fields.

  • Assigned to yurg
  • Status changed to Fixed 8 months ago
  • πŸ‡ΈπŸ‡°Slovakia yurg
  • πŸ‡©πŸ‡ͺGermany jurgenhaas Gottmadingen

    Thanks for your feedback.

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

Production build 0.71.5 2024