1 August 2011

Magento loads default theme on payment decline

If you use a Magento extension such as OneStepCheckout, you may have noticed that if payment is declined for an order, the design of your store changes to frontend/base/default/ rather than remaining as frontend/yourpackagename/yourtheme/ as you would expect. This is actually covered on OSC's wiki, but their solution is to comment out the line of code in their extension that instigates the payment failed email sending. Not a great solution as you will never then know when a customer failed to have their order processed at checkout and you will probably have missed a conversion.

As OSC suggest, the problem is with Magento, and actually affects every email that gets sent out. You only see it typically on the checkout page because this is the only case where an email is not sent either through an AJAX call, or before a redirect to another page.

To resolve the issue but still have the emails send, copy:
app/code/core/Mage/Core/Model/Email/Template.php
to
app/code/local/Mage/Core/Model/Email/Template.php
Open the local version of the file you just copied and you will see two methods, _applyDesignConfig() and _cancelDesignConfig(). Within these two methods the following lines of code run right after the store is set:
$design->setTheme('');
$design->setPackageName('');
They are pretty self explanatory, but they set your store theme, and your store package name to defaults resulting in the theme change you see when the checkout reloads. Comment these lines out in both methods and it should resolve the issue:
//$design->setTheme('');
//$design->setPackageName('');

NOTE:

If you are using a packagname other than default, then instead of commenting out the setPackageName() method, define it like this:
//$design->setTheme('');
$design->setPackageName('yourpackagename');
If you don't do this, you may find that some emails are sent with the Magento store logo rather than yours.