Magento 1 Enterprise Rewards Refund Bug

In the Magento 1 Enterprise Rewards module, there is a bug that affects page load time and server performance during the creation of a credit memo.  In the case of a large store with large Reward Points history, it can actually prevent you from creating a credit memo.  This problem affects orders placed from guests – customers that do not have a customer_id.

Fixing the Magento 1 Enterprise Guest Reward Points History Bug

Nature of the Issue

When creating a credit memo while the Magento 1 Enterprise Reward Points module is installed and enabled, Magento will load all of the reward points history for the customer.  It then iterates through this history, looking for this order in the Reward Points history so that it knows how to correctly calculate points during the refund.

This all happens in this file/method:

Reward Points history is saved based on a reward_id, which is correlated to a customer_id in the enterprise_reward table.

Here is where Magento looks up the Reward Points history:

The issue comes in then when the customer is a guest and thus does not have a customer_id.  This line:

calls:

If the $customerId is null, the join is never added to the query, thus resulting in ALL of the Reward Points history for ALL customers being returned.

Depending on the size of your Reward Points history this can be a large issue.  I’ve noticed that Magento will not return a page when it is too large because the server is asked to process far too much data.  It can throw an error in the traffic logs similar to this:

Bug Fix

The solution to this issue is to ensure that even if $customerId is null (guest customer) that this join still is called.  We can $customerId = 0 as that customer would never exist in a Magento store.

This bug fix sets $customerId = 0, thus the join will have something to look for.

Also the if statement has to be modified as a value of 0 would fail.

The end result is that the query resulting from this change will look like:

This should return zero results (as opposed to ALL results) and your server will hum right along just fine from there.

Bug Fix Via an Extend

As always, DO NOT modify core code – instead extend this module and properly fix this bug through the app/code/local directory.  NEVER modify core code in a production environment!

Other Areas Using This Function

For clarity, there are a few other areas of the website that use this same function.  These areas have been taken into account with this bug fix and are not affected by this change.

app/code/core/Enterprise/Reward/Model/Resource/Reward/History/Collection.php

Usage: The function with the bug that is modified with the proposed fix above

app/code/core/Enterprise/Reward/Model/Observer.php

Usage: Called after saving a credit memo.

app/code/core/Enterprise/Reward/Block/Adminhtml/Customer/Edit/Tab/Reward/History/Grid.php

Usage: In the Magento admin, displays the customers Reward Points history.

app/code/core/Enterprise/Reward/Block/Customer/Reward/History.php

Usage: On the frontend, displays the customers Reward Points history.