1

I installed and configure Drupal commerce successfully in Drupal 8 and now I want to change the role of a user when he/her purchase a product. E.g I have a product type test and user role paid now when the test user purchase a product I want to assign the paid role to this user after successful purchase.

I did this in Drupal 7 via rules module but how in Drupal 8.

3

For Drupal Commerce on Drupal 8, Rules is not complete or integrated. This leaves many such tasks like this for custom development. Fortunately, it's also easier to alter individual parts of the Commerce system thanks to Drupal 8's object oriented architecture. In this case, you'd write a new class called an event subscriber to assign that user role on the "placed" transition - the point at time an order completes checkout or is placed by an administrator. It should then handle assigning the user role to the order owner.

There may be contributed modules available for Rules integration to support individual tasks like this, but it hasn't been a core priority as of yet while we focus on other major feature groups (e.g. shipping, addressbook, recurring billing, etc.).

0

Commerce license provides this feature out of the box. From readme.txt :

The Commerce License allows the creation of products that sell access to some aspect of the site. This could be a role, publication of a node, and so on.

This access is controlled by a License entity, which is created for the user when the product is purchased.

The nature of what a License entity grants is handled by License type plugins.

The modules ships with a Role plugin.


Configuration

To create products that grant a role when purchased :

  1. Configure a checkout flow which does not allow anonymous checkout.
  2. Configure an Order Type to use this checkout flow.
  3. Configure an Order Item Type to use this Order Type, and work with Licenses.
  4. Configure a Product Variation Type to use this Order Item Type, and provide Licenses.
  5. Configure a Product Type that uses this Product Variation Type.
  6. Create one or more products that provide licenses. In the product variation, configure the license type to use the Role license type provided by the plugin

And that's it ! Additionally this almost smoothly integrates with recurring subscriptions.

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.