Drupal rules discount matrix - look up by taxonomy term I'm having trouble applying a complex set of discounts to my products through rules.

The discounts are basically a user-category/product-category matrix with a different discount percentage for each cell. User categories and product categories are taxonomy lists. I have added a Field Collection field (with and infinite number of values) to the user category. In this field collection I combine a product category and a discount percentage (as a decimal), and then repeat for all product categories.

So when calculating the product sale price I need to:


Look up the product category (works fine)
Look up the user category and load the discounts list (works fine)
Look up the right discount in the discounts list by product category (having trouble with this)


I've been able to look up the right discount in a custom PHP action like this:

$product_category = $product-&gt;field_product_category['und'][0]['tid'];

for($discount_index=0;$discount_index&lt;count($user_discounts);$discount_index++){
    $discount = $user_discounts[$discount_index];
    if($discount-&gt;field_discount_product_category['und'][0]['tid'] ==  $product_category){
        return $discount-&gt;field_discount_percentage['und'][0]['value'];
    }
}

return 0;


But apparently returning values out of a custom PHP block is not supported. 

Next I thought to install the Conditional Rules module. 
I could loop through the discounts and check the current loop variable against the product category and apply the discount if it matched. Within the If I can't access my loop variable though. 
Can anyone suggest a method that might work? I prefer not to have to write my own module if it can be avoided.
If you want to use rules (and with commerce I think it makes sense), and you are comfortable with PHP, I would recommend writing your own module that defines the custom conditions and actions that you need to look up and apply discounts. 

In my experience, the rules interface starts to break down quickly when use cases become even moderately complex. You can spend a lot of frustrating time fiddling with the interface, trying to get it to do what you want.

By writing your own conditions and actions, you keep the complex logic in code, where you can easily debug, access, and compare any information you might need.

In addition, your custom actions and conditions will be nicely exposed in the rules interface, and keep the high-level logic of your commerce solution tidy in one place.

Docs are here: https://drupal.org/node/298486. Drupal commerce itself has many good examples of custom actions and conditions to use as reference.