OpenMage / magento-lts

Official OpenMage LTS codebase | Migrate easily from Magento Community Edition in minutes! Download the source code for free or contribute to OpenMage LTS | Security vulnerability patches, bug fixes, performance improvements and more.
https://www.openmage.org
Open Software License 3.0
870 stars 436 forks source link

Overriding email templates location reveals inconsistency in accessing email template files #1773

Open f1-outsourcing opened 3 years ago

f1-outsourcing commented 3 years ago

When testing with a module to override the location where email templates are stored (want to keep them within the theme tree) I noticed that files are gotten from different locations.

This is code that compiles the path

        $designPackage = Mage::getModel('core/design_package');
        $filePath = Mage::getBaseDir('design')  . DS . 'frontend' . DS
                  . $designPackage->getPackageName() . DS . $designPackage->getTheme('frontend') . DS . 'locale' . DS
                  . $localeCode . DS . 'template' . DS . $type . DS . $file;

        Mage::log('emailtemplatestheme: '.$filePath);

This is being logged to system.log when I choose 'send email' on a order in the admin menu.

2021-08-08T15:44:05+00:00 DEBUG (7): emailtemplatestheme: /home/shop.example.com/app/design/frontend/default/default/locale/en_GB/template/email/sales/order_new.html
2021-08-08T15:44:05+00:00 DEBUG (7): emailtemplatestheme: did not find file!
2021-08-08T15:44:05+00:00 DEBUG (7): emailtemplatestheme: /home/shop.example.com/app/design/frontend/rwd/customname/locale/en_GB/template/email/html/header.html
2021-08-08T15:44:05+00:00 DEBUG (7): emailtemplatestheme: /home/shop.example.com/app/design/frontend/rwd/customname/locale/en_GB/template/email/html/footer.html

The module is working correctly otherwise there would be no logging however the order_new.html is grabbed still from 'frontend/default/default' while the header.html/footer.html are grabbed from the correct location '/rwd/customname'

I am not familiar with magento internals, but I would assume this is an inconsistency how files are gotten. maybe some environment is not set when the order_new.html is requested?

Preconditions (*)

git clone 1.9.4.x

Steps to reproduce (*)

1. 2.

Expected result (*)

  1. [Screenshots, logs or description]

Actual result (*)

  1. [Screenshots, logs or description]
fballiano commented 3 years ago

i don't think this is an issue caused by openmage

Flyingmana commented 3 years ago

Could be caused by the emulation, analog to how the translations work during email sending. Can you get a full backtrace for this case?

f1-outsourcing commented 3 years ago

Hmmm, I hope this is what you mean, I am not really a magento developer.

I added this

echo Varien_Debug::backtrace(true, true); 
exit;   

And it generated this:

#1 F1outsourcing_EmailTemplatesTheme_Model_Translate#000000001bbcf7ec00000000679e3f78#->getTemplateFile('sales/order_new_...', 'email', 'en_GB') called at [app/code/core/Mage/Core/Model/Email/Template.php:183]
#2 Mage_Core_Model_Email_Template#000000001bbcf7a800000000679e3f78#->loadDefault('sales_email_orde...', 'en_GB') called at [app/code/core/Mage/Core/Model/Email/Template.php:507]
#3 Mage_Core_Model_Email_Template#000000001bbcf7a800000000679e3f78#->sendTransactional('sales_email_orde...', 'sales', array('asdfa@asdfadfs.c...'), array('dasdfasdf asdfad...'), array('order' => &Mage_Sales_Model_Order#000000001bbcf6f400000000679e3f78#, 'total' => '€1.00', 'billing' => &Mage_Sales_Model_Order_Address#000000001bbcf7d300000000679e3f78#, 'payment_html' => '

Bank Transfer...'), 2) called at [app/code/core/Mage/Core/Model/Email/Template/Mailer.php:83]
#4 Mage_Core_Model_Email_Template_Mailer#000000001bbcf7d700000000679e3f78#->send() called at [app/code/core/Mage/Sales/Model/Order.php:1461]
#5 Mage_Sales_Model_Order#000000001bbcf6f400000000679e3f78#->queueNewOrderEmail(true) called at [app/code/core/Mage/Sales/Model/Order.php:1476]
#6 Mage_Sales_Model_Order#000000001bbcf6f400000000679e3f78#->sendNewOrderEmail() called at [app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php:141]
#7 Mage_Adminhtml_Sales_OrderController#000000001bbcf6f300000000679e3f78#->emailAction() called at [app/code/core/Mage/Core/Controller/Varien/Action.php:437]
#8 Mage_Adminhtml_Sales_OrderController[Mage_Core_Controller_Varien_Action]#000000001bbcf6f300000000679e3f78#->dispatch('email') called at [app/code/core/Mage/Core/Controller/Varien/Router/Standard.php:262]
#9 Mage_Core_Controller_Varien_Router_Admin[Mage_Core_Controller_Varien_Router_Standard]#000000001bbcf6f900000000679e3f78#->match(&Mage_Core_Controller_Request_Http#000000001bbcf6c200000000679e3f78#) called at [app/code/core/Mage/Core/Controller/Varien/Front.php:192]
#10 Mage_Core_Controller_Varien_Front#000000001bbcf6fc00000000679e3f78#->dispatch() called at [app/code/core/Mage/Core/Model/App.php:381]
#11 Mage_Core_Model_App#000000001bbcf6e300000000679e3f78#->run(array('scope_code' => '', 'scope_type' => 'store', 'options' => array())) called at [app/Mage.php:744]
#12 Mage::run('', 'store') called at [index.php:88]

From a logical point of view it is just strange to me, that template files are gotten from different locations when you click the 'send email' button on a order in the admin.

f1-outsourcing commented 3 years ago

Can something be done about this, or can someone explain me a work-a-round? I am going to put one shop online soon (I hope). But it would be nice if I start using a 2nd template, I can get different email templates for that. I would say even it is nice to have by default the email templates in the theme tree. But maybe that is also due to a lack of expert experience, wanting such a thing.

f1-outsourcing commented 3 years ago

@Flyingmana anything new on this?

elidrissidev commented 2 years ago

I just dug into this for some time, and I found that indeed the emulation is the culprit here because it ends before the templates are loaded. It's not really a problem currently because by default they don't depend on theme so... I'll see.

f1-outsourcing commented 1 year ago

@elidrissidev

Hi Mohamed, thanks for looking into this. Do you have a temporary fix for this? I still do not have my store online for what I was planning to use it. But something else came up and thought about using the current magento for it. But I need to have this working, otherwise I need to create separate install. I am willing to financially contribute ;)

elidrissidev commented 1 year ago

@f1-outsourcing If memory serves me right, it should be as easy as updating/overriding the core file and making template loading happen when the store is still being emulated. I'll recheck in a few hours.

elidrissidev commented 1 year ago

Briefly checked, wrapping the line below with the two method calls should make it work, however there could be a better solution.

https://github.com/OpenMage/magento-lts/blob/3e7be1102e6cd0d33913daae9788a21e726efea0/app/code/core/Mage/Core/Model/Email/Template.php#L168-L172

Wrap it like this:

+        $this->_applyDesignConfig();
         $templateText = Mage::app()->getTranslator()->getTemplateFile(
             $data['file'],
             'email',
             $locale
         );
+        $this->_cancelDesignConfig();

Instead of modifying the core you can rewrite the file using your custom extension, or override it fully by copying it to app/code/local/Mage/Core/Model/Email/Template.php and then applying the changes.

f1-outsourcing commented 1 year ago

@elidrissidev

Thanks! Much appreciated :) I think this works, I have to test a bit more. I looked at it such a long time ago.