I am working on user based restrictions and couldn't find any documentation on how to do it so I thought I would provide what I have done so far. It would be nice to have documentation for rule snippets like this, hence the feature request.

Still haven't fully tested this, but I'll try to confirm when I have.

First I created a user reference field for my fixed coupon (haven't tested percent coupons yet) called "field_limit_users" that allows for unlimited user references.

Here is the component rule:

{ "rules_coupon_validation_set_the_coupon_as_invalid_based_on_user" : {
    "LABEL" : "Coupon Validation: Set the coupon as invalid based on user",
    "PLUGIN" : "rule",
    "TAGS" : [ "coupon" ],
    "REQUIRES" : [ "rules", "commerce_coupon" ],
    "USES VARIABLES" : {
      "commerce_coupon" : { "label" : "Coupon", "type" : "commerce_coupon" },
      "limited_users" : { "label" : "Limited Users", "type" : "list\u003Cuser\u003E" },
      "current_user" : { "label" : "Current User", "type" : "user" }
    },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "commerce_coupon" ], "field" : "field_limit_users" } },
      { "AND" : [
          { "NOT data_is" : {
              "data" : [ "current-user" ],
              "op" : "IN",
              "value" : [ "limited-users" ]
            }
          },
          { "NOT data_is_empty" : { "data" : [ "limited-users" ] } }
        ]
      }
    ],
    "DO" : [
      { "drupal_message" : {
          "message" : "Sorry, you are not authorized to use this coupon.",
          "type" : "error"
        }
      },
      { "commerce_coupon_action_is_invalid_coupon" : [] }
    ]
  }
}

And here is the reaction rule:

{ "rules_coupon_validation_check_the_users_allowed" : {
    "LABEL" : "Coupon Validation: Check the users allowed",
    "PLUGIN" : "reaction rule",
    "TAGS" : [ "coupon" ],
    "REQUIRES" : [ "rules", "commerce_coupon" ],
    "ON" : [ "commerce_coupon_validate" ],
    "IF" : [
      { "entity_has_field" : { "entity" : [ "coupon" ], "field" : "field_limit_users" } }
    ],
    "DO" : [
      { "component_rules_coupon_validation_set_the_coupon_as_invalid_based_on_user" : {
          "commerce_coupon" : [ "coupon" ],
          "limited_users" : [ "coupon:field-limit-users" ],
          "current_user" : [ "site:current-user" ]
        }
      }
    ]
  }
}

This is all without the need of any patch on the most recent dev version (2012-May-22 ).

Comments

pcambra’s picture

Status: Active » Closed (duplicate)

Thanks for the rule exported, I'm marking this as dupe of #1468148: Coupon redemption once per user as both deal with the same