0

I wrote a module which actions get triggered by a cronjob. The action says that I lookup the orders made between X days ago and NOW.

With the information I want to send out an feedback mail to the customer to ask how they felt how the order process went through.

However, since I have a multilang shop (fr,en,de,ru,it,es) I'd like to have only one email template for that and use the translation feature within that template.

The tricky part is, to get any PHP code running in the template. I named it customer_feedback_email_template.phtml, its in locale/de_DE/template/email/ and its setup in my modules config.xml.

I extended the Mage_Core_Model_Email_Template class to overwrite its sendMail method like this:

    /**
     * Send email to recipient
     *
     * @param string $templateId template identifier (see config.xml to know it)
     * @param array  $sender     sender name with email ex. array('name' => 'John D.', 'email' => 'email@ex.com')
     * @param string $email      recipient email address
     * @param string $name       recipient name
     * @param string $subject    email subject
     * @param array  $params     data array that will be passed into template
     */
    public function sendEmail($templateId, $sender, $email, $name, $subject, $params = array())
    {
        /*
         * To enable translating
         */
        Mage::app()->getTranslator()->init('frontend', TRUE);
        $this->setDesignConfig(array('area' => 'frontend', 'store' => $this->getDesignConfig()->getStore()))
            ->setTemplateSubject($subject)
            ->sendTransactional(
                $templateId,
                $sender,
                $email,
                $name,
                $params
            );
    }

$params contains some customer and order informationen I'd like to use in the template.

Using the template with this content

{{var customer_first_name}}

is just fine, but if I add

<? echo "Blablabla"; ?>

I will only see the customers first name again, but not anything else (so the echo wont be executed?).

How may I enable PHP execution (and therefore translation) in email templates?

//Edit My config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mscg_FeedbackCustomer>
            <version>0.1.0</version>
        </Mscg_FeedbackCustomer>
    </modules>
    <global>
        <sales>
            <order>
                <statuses>
                    <sent_feedback translate="label">
                        <label>Sent Feedback</label>
                    </sent_feedback>
                </statuses>
                <states>
                    <complete>
                        <statuses>
                            <sent_feedback/>
                        </statuses>
                    </complete>
                </states>
            </order>
        </sales>
        <models>
            <mscg_feedback_customer>
                <class>Mscg_FeedbackCustomer_Model</class>
                <resourceModel>mscg_feedback_customer_resource</resourceModel>
            </mscg_feedback_customer>
        </models>
        <helpers>
            <feedbackcustomer>
                <class>Mscg_FeedbackCustomer_Helper</class>
            </feedbackcustomer>
        </helpers>
        <models>
            <feedbackcustomer>
                <class>Mscg_FeedbackCustomer_Model</class>
                <resourceModel>feedbackcustomer_mysql4</resourceModel>
            </feedbackcustomer>
        </models>
        <template>
            <email>
                <mscg_customerfeedback_email_template translate="label" module="mscgfeedbackcustomer">
                    <label>Mscg Customer Feedback Email</label>
                    <file>mscg_customer_feedback_email_template.phtml</file>
                    <type>html</type>
                </mscg_customerfeedback_email_template>
            </email>
        </template>
        <translate>
            <modules>
                <Mscg_FeedbackCustomer>
                    <files>
                        <default>feedback_en.csv</default>
                        <de>feedback_de.csv</de>
                        <fr>feedback_fr.csv</fr>
                        <en>feedback_en.csv</en>
                        <it>feedback_it.csv</it>
                        <ru>feedback_ru.csv</ru>
                        <es>feedback_es.csv</es>
                    </files>
                </Mscg_FeedbackCustomer>
            </modules>
        </translate>
    </global>
    <default>
        <feedbackoptions>
            <orders_older_than_days>100</orders_older_than_days>
            <email_from_name>MSCG Feedback</email_from_name>
            <email_from_emailaddress>xxxx</email_from_emailaddress>
        </feedbackoptions>
    </default>
    <crontab>
        <jobs>
            <feedbackcustomer_sendfeedbackmail>
                <schedule>
                    <cron_expr>*/5 * * * *</cron_expr>
                </schedule>
                <run>
                    <model>feedbackcustomer/cron::sendFeedbackMail</model>
                </run>
            </feedbackcustomer_sendfeedbackmail>
        </jobs>
    </crontab>
</config> 

//Edit This solution works for me:

config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Mscg_FeedbackCustomer>
            <version>0.1.0</version>
        </Mscg_FeedbackCustomer>
    </modules>
    <global>
        <sales>
            <order>
                <statuses>
                    <sent_feedback translate="label">
                        <label>Sent Feedback</label>
                    </sent_feedback>
                </statuses>
                <states>
                    <complete>
                        <statuses>
                            <sent_feedback/>
                        </statuses>
                    </complete>
                </states>
            </order>
        </sales>
        <models>
            <mscg_feedback_customer>
                <class>Mscg_FeedbackCustomer_Model</class>
                <resourceModel>mscg_feedback_customer_resource</resourceModel>
            </mscg_feedback_customer>
        </models>
        <helpers>
            <feedbackcustomer>
                <class>Mscg_FeedbackCustomer_Helper</class>
            </feedbackcustomer>
        </helpers>
        <models>
            <feedbackcustomer>
                <class>Mscg_FeedbackCustomer_Model</class>
                <resourceModel>feedbackcustomer_mysql4</resourceModel>
            </feedbackcustomer>
        </models>
        <resources>
            <mscg_feedback_customer_setup>
                <setup>
                    <module>Mscg_FeedbackCustomer</module>
                    <class>Mage_Sales_Model_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </mscg_feedback_customer_setup>
            <mscg_feedback_customer_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </mscg_feedback_customer_write>
            <mscg_feedback_customer_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </mscg_feedback_customer_read>
        </resources>
        <template>
            <email>
                <mscg_customerfeedback_email_template translate="label" module="mscgfeedbackcustomer">
                    <label>Mscg Customer Feedback Email</label>
                    <file>mscg_customer_feedback_email_template.phtml</file>
                    <type>html</type>
                </mscg_customerfeedback_email_template>
            </email>
        </template>
    </global>
    <frontend>
        <translate>
            <modules>
                <Mscg_FeedbackCustomer>
                    <files>
                        <default>feedback_en.csv</default>
                        <de>feedback_de.csv</de>
                        <fr>feedback_fr.csv</fr>
                        <en>feedback_en.csv</en>
                        <it>feedback_it.csv</it>
                        <ru>feedback_ru.csv</ru>
                        <es>feedback_es.csv</es>
                    </files>
                </Mscg_FeedbackCustomer>
            </modules>
        </translate>
    </frontend>
    <default>
        <mscg_feedbackcustomer>
            <feedbackoptions>
                <orders_older_than_days>100</orders_older_than_days>
                <email_from_name>MSCG Feedback</email_from_name>
                <email_from_emailaddress>xxx</email_from_emailaddress>
            </feedbackoptions>
            <orderattributes>
                <customer_informed>x_customerfeedback</customer_informed>
            </orderattributes>
        </mscg_feedbackcustomer>
    </default>
    <crontab>
        <jobs>
            <feedbackcustomer_sendfeedbackmail>
                <schedule>
                    <cron_expr>*/5 * * * *</cron_expr>
                </schedule>
                <run>
                    <model>feedbackcustomer/cron::sendFeedbackMail</model>
                </run>
            </feedbackcustomer_sendfeedbackmail>
        </jobs>
    </crontab>
</config>

And load the correct translations

Mage::getSingleton('core/translate')->setLocale($countryKey)->init('frontend', TRUE);

$countryKey = standard country code for localization in magento (eg. fr_FR, de_DE, en_US ..)

template is using {{var variableName}} which I pass along $params when calling my sendMail method which I extend from Mage_Core_Model_Email_Template

paskl
  • 165
  • 7
  • it doesn't work because html templates don't understand PHP. You must pass your variables {{var test}} via PHP in the $params array as follows : ...,$name,array('test' => $this->__('Your string')) ... which can be translated – Julien Lachal Oct 16 '14 at 08:20
  • @JulienLachal Okay, that works okay. But how would I know now what csv to use to translate the keys? Can I force to load a specific csv when I know the customers country_code is "IT" for example? – paskl Oct 16 '14 at 09:59
  • Mage::getSingleton('core/translate')->init('fr_FR', true); didnt work, but I do have a .csv configured for that and its in the app/locale/fr_FR/ - I also have it in my config.xml registered. <translate> <modules> <Mscg_FeedbackCustomer> <files> <default>feedback_en.csv</default> <fr>feedback_fr.csv</fr> </files> </Mscg_FeedbackCustomer> </modules> </translate> – paskl Oct 16 '14 at 10:05
  • @Marius I dont think its a duplicate, because I dont have the templates available in the backend but just inside the module. Or do you think its better to "create" a email template with this module and use it from whatever is in the backend? Sounds way to hard for my needs. – paskl Oct 16 '14 at 11:36
  • You can try to take the long way I described in my answer from that question. It's clean and extensible but it requires some work, or you can try the answer from Alex: http://magento.stackexchange.com/a/18454/146. It requires a lot less code – Marius Oct 16 '14 at 11:38
  • I'd like to answer my own question now, but its been marked as duplicate (when its really not in my opinion). Too bad. – paskl Oct 17 '14 at 07:01
  • Added my solution to question text since I cant answer it anymore. – paskl Oct 17 '14 at 12:53

0 Answers0