How do you perform a rules action by the quantity of a specific commerce product? I'm working on a module which is going to be used alongside Drupal Commerce so people can buy a set number of digital products. I've written a custom rules action, which will be run once for each purchase of a specific commerce product. How can I add this to a loop, so that the action gets run n times (n being the quantity purchased for this product, as you could purchase more than one).

So far I've added this action to a loop for commerce-order:commerce-line-items but even if I purchase more than one, the action is only run once. How can I make the action run 'quantity of this product purchased' number of times?

Many thanks
The quickest way to do this is with another custom action. Since you already wrote your own module, this shouldn't be a problem.

In your my_module.rules.inc file, do something like:

function my_module_rules_action_info() {
 return array(
  'my_module_loop_based_on_qty' =&gt; array(
    'label' =&gt; t('Execute a loop based on a quantity'),
    'parameter' =&gt; array(
      'quantity' =&gt; array(
        'type' =&gt; 'integer',
        'label' =&gt; t('Quantity added to cart'),
      ),
    ),
    'group' =&gt; t('My Module'),
  );
}

function my_module_loop_based_on_qty($qty) {
  // Loop based on the $qty variable
}


Provide whatever other parameters you need for the function as well. You can then pass it the quantity when you create the rule. That other rules action you already wrote can just be wrapped in a helper function that is then called by this new action.