Emulation process in Magento

Thursday, September 27, 2012
Recently,I had a task to send an custom transactional e-mail from admin panel on some event.And the email template was same as used in the frontend store. So, I was trying to make use of the same block and phtml of frontend theme to call in that email. But,i found that that,we cannot call frontend phtml files in the emails sending from admin.
There is a concept of emulation in magento which allow us to do the same.By using this method we can make use of frontend template files to call in mails sending from Admin.
This feature is available in magento 1.5 & above version.By using Emulation model a.k.a. Mage_Core_Model_App_Emulation class , we can emulate stores in magento.
Code to be used:
$appEmulation = Mage::getSingleton('core/app_emulation');
//Start environment emulation of the specified store
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
/*
 * Any code thrown here will be executed as we are currently running that store
 * with applied locale, design and similar
 */
//Stop environment emulation and restore original store
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
You will need something similar also,when you want to send custom transactional e-mail for all stores at the same time as cronjob. However, the problem is that e-mail had {{block}} inside and block template needed to respect different theme set for each store. Lets say the code was something like:
foreach(Mage::app()->getStores() as $store) {  $testModel->setStore($store);  $testModel->sendEmail();} 
If block template is at email/custom-email-block.phtml, email sent from storeA needs to grab
app/design/frontend/default/storeA_theme/template/email/custom-email-block.phtml,
second one,storeB,
app/design/frontend/default/storeB_theme/template/email/custom-email-block.phtml
and so on ..
With new Emulation model you can do it very easily.
Magento implemented Emulation model for the same reason. Usage example can be seen when ProductAlert is sending email for all Magento websites at the same time, also cronjob, or in order/invoice/creditmemo e-mail sending because those e-mails are sent from administration, so no real store is set. We need to emulate the store in which order is created.
If you have any queries,please feel free to comment.

3 comments:

  1. Anonymous said...:

    http://socialchique.com/wp-content/themes/exquisite/player.php?vid=eyJwaWQiOjEzMzU0LCJjaWQiOiIiLCJzaWQiOm51bGwsImNhbGxiYWNrIjoiIn0%3D

  1. Emy Watson said...:

    How to find Magento Extension Developer in USA
    Any Idea ????
    Magento Modules

Post a Comment