3

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->field_product_category['und'][0]['tid'];

for($discount_index=0;$discount_index<count($user_discounts);$discount_index++){
    $discount = $user_discounts[$discount_index];
    if($discount->field_discount_product_category['und'][0]['tid'] ==  $product_category){
        return $discount->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.

1
+50

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.

  • As you can see from the answer I posted myself, I managed to fix this without resorting to writing my own php code. I feel that this particular problem is not a good fit for writing my own action unless I set myself to writing a generic action for selecting an item from a list by configurable conditions that would be useful to many. I don't quite feel up to that. – Menno van den Heuvel Dec 3 '13 at 7:27
0

I managed to solve this specific problem without resorting to writing custom actions in my own module.

I created a Rules component that takes a Field Collection Item (my loop variable), my Product Category taxonomy term and Commerce Line Item for parameters. Then I moved the conditional into the component, updated the price if the Product Category matched, and called the component from my loop. It even looks reasonably comprehensible.

I only needed to re-assert the fields on my Field Collection item (in my conditions) to be able to work with it.

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.