3

We have a script that is sending emails via the command line. Since we are using the email templates we are loading them using the Mage::getModel('core/email_template') model and then calling ->send().

This results in the emails being sent with the wrong store information and locale inside.

1 Answer 1

5

The problem is that the Mage::getModel('core/email_template') loads the design information using Mage_Core_Model_Template::_applyDesignConfig() which intern loads a store via Mage::getDesign()->getStore(). When you follow this down the rabbit hole you will see the following.

public function getStore()
{
    if ($this->_store === null) {
        return Mage::app()->getStore();
    }
    return $this->_store;
}

This means that if the store is not set if will load the default store, note that nothing is being passed into the getStore call.

So one way around this is to call Mage::app()->setCurrentStore(22) but as pointed out by Sander there is a better way.

You can use Mage::getSingleton('core/app_emulation') to wrap your code in store emulation.

$appEmulation = Mage::getSingleton('core/app_emulation');

//Start environment emulation of the specified store
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);

// Your code here

//Stop environment emulation and restore original store
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);

For more information see Inchoo

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.