Now that we have revoke() and suspend() methods for Commerce License, is it possible to get a Rules action for each of these? It would be helpful if, when an order is Canceled, for example, that the licenses are revoked or suspended at that time.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

torgosPizza’s picture

Status: Active » Needs review
FileSize
2.31 KB

Here is a patch that adds these actions to the Rules for Commerce License. They are basically just copy-pastes of the original activate() related code.

capfive’s picture

Thanks for the patch, I have applied this but I ran the following rule and it still did not work.

{ "rules_remove_role" : {
    "LABEL" : "Remove license when new one is purchased",
    "PLUGIN" : "reaction rule",
    "WEIGHT" : "-10",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "commerce_license", "commerce_payment" ],
    "ON" : { "commerce_payment_order_paid_in_full" : [] },
    "IF" : [
      { "user_has_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "6" : "6", "7" : "7", "8" : "8" } },
          "operation" : "OR"
        }
      }
    ],
    "DO" : [
      { "user_remove_role" : {
          "account" : [ "site:current-user" ],
          "roles" : { "value" : { "6" : "6", "7" : "7", "8" : "8" } }
        }
      },
      { "commerce_license_revoke_order_licenses" : { "commerce_order" : [ "commerce-order" ] } }
    ]
  }
}

I am targeting the commerce_order because this is what the activation rule targets, but should i maybe be targeting something else?

torgosPizza’s picture

Not sure. I have this Rule setup for revoking licenses when an order is updated to Canceled and it works fine.

{ "rules_remove_licenses_of_a_canceled_order" : {
    "LABEL" : "Remove Licenses of a Canceled order",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "commerce_license", "entity" ],
    "ON" : { "commerce_order_update" : [] },
    "IF" : [
      { "data_is" : { "data" : [ "commerce-order:state" ], "value" : "canceled" } }
    ],
    "DO" : [
      { "commerce_license_revoke_order_licenses" : { "commerce_order" : [ "commerce_order" ] } }
    ]
  }
}

I have this feeling your Rule is conflicting with the default Rule for "activating" the licenses on an order, since that also runs at "When an order is paid in full." Have you tested that aspect of it? You may need to set your Rules' weights accordingly, and/or create an additional Condition for one or the other so there is no race condition.

capfive’s picture

I see, what I want to use this for us upgrades and downgrades, I'll try the weights and turn on my rule debugging on.

Just thought maybe someone had a rule ready to go :) I'll edit my original post when I figure it out so there is a good support rule

jweirather’s picture

Just cross posting this related thread about using rules to alter the status (set a data value) of a Commerce License entity:

https://www.drupal.org/node/2332375

Also: the patch in this thread applied properly for me, so +1 for that, and thank you.

I didn't realize that the patch cancelled all Commerce Licenses on an order, versus individual licenses, which is what I need for my install. Using rules to "set a data value" appears to be working well for me, so far anyway.

stiyke’s picture

jweirather I also want to cancel individual license of an order using rules set a data value. But i am stuck where an order has many line items how to single out the particular license I would like cancelled. Does anyone have an idea how i could specify the particular license i want cancelled using rules?

vensires’s picture

Status: Needs review » Reviewed & tested by the community

The patch fixed my problem so +1 from me too and I believe it is RTBC since it really matches the scenario described by @TorgosPizza. I would also like to better understand the necessity for what jweirather and stiyke described.

In what cases do you need cancellation of one-but-not-all licenses of an order? If the order contains a line item and the order is not canceled, then all licenses should be valid. If a user shouldn't have a valid license, then the line item should be removed from the user's order and then revoked. This revoking I believe isn't yet implemented. @jweirather could you provide us with an example rule configuration?

BYUStudies’s picture

Works great for me too. Thanks @torgosPizza!

vensires’s picture

Until the patch gets commited, I have created the following module: Commerce License Fix. With proper credit to @torgosPizza for the original code of course.

rodrigoaguilera’s picture

I was stuck with revoking commerce licenses but my problem is that I was passing an order with a recurring line item that stores the license reference differently.

This patch should solve it
#2857268: commerce_license_get_order_licenses() should not hardcode the license reference field name