I have added custom functionality to send invoice email to some other mail address(excluding customer & admin). For this I have called custom function in observers
<sales_order_invoice_save_after>
<observers>
<test>
<type>singleton</type>
<class>test/observer</class>
<method>sentMailToothers</method>
</test>
</observers>
Observer code
$template_id = 'sales_email_invoice_template';
$emailTemplate = Mage::getModel('core/email_template')->loadDefault($template_id);
$storeId = Mage::app()->getStore()->getStoreId();
$invoice = $observer->getEvent()->getInvoice();
$order = $observer->getEvent()->getInvoice()->getOrder();
if ($order->hasInvoices())
{
foreach ($order->getInvoiceCollection() as $inv)
{
$paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())->setIsSecureMode(true);
$paymentBlockHtml = $paymentBlock->toHtml();
$email_to = 'test#test.com';//dynamic email address
$customer_name = 'Test'
$email_template_variables = array(
'order' => $order,
'invoice' => $invoice,
'payment_html' => $paymentBlockHtml
);
$sender_name = Mage::getStoreConfig(Mage_Core_Model_Store::XML_PATH_STORE_STORE_NAME);
$sender_email = Mage::getStoreConfig('trans_email/ident_general/email');
$emailTemplate->setSenderName($sender_name);
$emailTemplate->setSenderEmail($sender_email);
$processedTemplate = $emailTemplate->getProcessedTemplate($email_template_variables);
echo $processedTemplate;die;
//Send the email!
$emailTemplate->send($email_to, $customer_name, $email_template_variables);
}
}
Everything works fine except , product information not showing in templates.
Can somebody figure out what I am doing wrong?
No need this code. In magento have default functionality for sending invoice email to Bcc.
Goto Admin->System(main nav)->configuration(sub nav)->Sales emails->invoice(tab)-> field "Send Invoice Email Copy To" .
please check below screenshots.
Related
I am facing a problem while saving Magento custom variable on order item table by observer.
The Email fires before the custom attribute is saved on the table by magento observer sales_order_place_after. But after the order place I login in admin panel and click on the send mail button at that point of time the order mail has that custom attribute. It seem that the order attribute is saved but the email fires before that.
Also from admin order creation I am not using and observer but I have overwritten Mage_Adminhtml_Sales_Order_CreateController but same issues is also arsing on it.
Below are code that i have used:-
config.xml
<events>
<sales_order_place_after>
<observers>
<itemlevelcomment>
<type>singleton</type>
<class>Company_ItemlevelComment_Model_Observer</class>
<method>addOrderComment</method>
</itemlevelcomment>
</observers>
</sales_order_place_after>
</events>
class Company_ItemlevelComment_Model_Observer extends Varien_Event_Observer
{
public function addOrderComment($observer)
{
$order = $observer->getEvent()->getOrder();
if (!$order) {
return $this;
}
$orderId = $order->getData('entity_id');
$quote = Mage::getModel('sales/quote')->load($order->getData('quote_id'));
$cartItems = $quote->getAllVisibleItems();
$resource = Mage::getSingleton('core/resource');
$writeConnection = $resource->getConnection('core_write');
foreach ($cartItems as $item) {
$quoteItemId = $item->getId();
$itemComment = Mage::getModel('sales/quote_item')->load($quoteItemId)->getItemComment();
$query = "UPDATE `sales_flat_order_item` SET item_comment = '".$itemComment."' WHERE quote_item_id = ". (int)$quoteItemId." and order_id = " . $orderId;
$writeConnection->query($query);
}
}
}
I have also tried with loading object of Mage_Sales_Order_Item and save the object but email is sending without the custom fields.
I have dispalyed the custom attribute on email on following path app\design\frontend\base\default\template\email\order\items\order\default.phtml
<?php $comment =$_item->getItemComment(); ?>
<?php //if(!empty($comment)): ?>
<p class="sku"><?php echo $this->__('Comment'); ?>: <?php echo $this->escapeHtml($_item->getItemComment()) ?></p>
<?php //endif; ?>
Any thought regarding this?
You can use fieldsets as below:
<global>
<fieldsets>
<sales_convert_quote_item>
<item_comment>
<to_order_item>*</to_order_item>
</item_comment>
</sales_convert_quote_item>
<sales_convert_order_item>
<item_comment>
<to_quote_item>*</to_quote_item>
<to_invoice_item>*</to_invoice_item>
<to_cm_item>*</to_cm_item>
</item_comment>
</sales_convert_order_item>
</fieldsets>
<global>
Hi I have a custom Module built that sends an email when certain order statues are being created.
I have an observer that hooks into sales_order_place_after and my order object sinde observer.php is
public function getOrderStatus($observer)
{
$order = $observer->getEvent()->getOrder();
$status = $order->getStatus();
$enabled = Mage::getStoreConfig(self::XML_PATH_EMAIL_ENABLE);
if($enabled == 1){
if($status === "fraud")
{
$this->sendFraudEmail($observer);
}
}
}
but inside the transactional emails {{var order.increment_id}} is not working, even though in the observer I have : $order = $observer->getEvent()->getOrder();
what am I missing? Thanks.
Normally to send a custom transactional email you do something like the following;
$templateId = 16;
$sender = array(
'name' => Mage::getStoreConfig('trans_email/ident_support/name', Mage::app()->getStore()->getId()),
'email' => Mage::getStoreConfig('trans_email/ident_support/email', Mage::app()->getStore()->getId())
);
$vars = array('order' => $observer->getEvent()->getOrder());
Mage::getModel('core/email_template')->sendTransactional($templateId, $sender, $customerEmail, $customerName, $vars, $storeId);
I am working on a module that will send an email after 7 days of order completion. I'm stuck on sending emails. I can see the email template in transactional emails drop down in admin. But the email is not being sent.
Here is my confix.xml part for including email template.
<template>
<email>
<recurring_order_email_template translate="label">
<label>Recurring order email</label>
<file>coeus_recurring_order_email.html</file>
<type>html</type>
</recurring_order_email_template>
</email>
</template>
and this is how I am sending email in controller action
$emailTemplate = Mage::getModel('core/email_template')
->loadDefault('coeus_recurring_order_email');
$emailTemplateVariables = array();
$emailTemplateVariables['var1'] = 'var1 value';
$emailTemplateVariables['var2'] = 'var 2 value';
$emailTemplateVariables['var3'] = 'var 3 value';
$emailTemplate->getProcessedTemplate($emailTemplateVariables);
$emailTemplate->setSenderName('sender name');
$emailTemplate->setSenderEmail('sender#test.com');
try {
$emailTemplate->send('myemail#gmail.com', 'bla bla',$emailTemplateVariables);
} catch (Exception $e) {
echo $e->getMessage();
}
I don't know why its not working.
$emailTemplate = Mage::getModel('core/email_template')->loadDefault('recurring_order_email_template');
//Getting the Store E-Mail Sender Name.
$senderName = Mage::getStoreConfig('trans_email/ident_general/name');
//Getting the Store General E-Mail.
$senderEmail = Mage::getStoreConfig('trans_email/ident_general/email');
//Variables for Confirmation Mail.
$emailTemplateVariables = array();
$emailTemplateVariables['name'] = $customerName;
$emailTemplateVariables['email'] = $customerEmail;
//Appending the Custom Variables to Template.
$processedTemplate = $emailTemplate->getProcessedTemplate($emailTemplateVariables);
//Sending E-Mail to Customers.
$mail = Mage::getModel('core/email')
->setToName($senderName)
->setToEmail($customerEmail)
->setBody($processedTemplate)
->setSubject('Subject :')
->setFromEmail($senderEmail)
->setFromName($senderName)
->setType('html');
try{
//Confimation E-Mail Send
$mail->send();
}
catch(Exception $error)
{
Mage::getSingleton('core/session')->addError($error->getMessage());
return false;
}
Change your etc/config.xml code to below:
<template>
<email>
<recurring_order_email_template>
<label>Recurring order email</label>
<file>coeus_recurring_order_email.html</file>
<type>html</type>
</recurring_order_email_template>
</email>
</template>
Change your controller code to below:
$emailTemplate = Mage::getModel('core/email_template')
->loadDefault('recurring_order_email_template');
$emailTemplateVariables = array();
$emailTemplateVariables['var1'] = 'var1 value';
$emailTemplateVariables['var2'] = 'var 2 value';
$emailTemplateVariables['var3'] = 'var 3 value';
$emailTemplate->getProcessedTemplate($emailTemplateVariables);
$emailTemplate->setSenderName('sender name');
$emailTemplate->setSenderEmail('sender#test.com');
try {
$emailTemplate->send($recipientEmail, $senderName, $emailTemplateVariables);
} catch (Exception $e) {
echo $e->getMessage();
}
Change your $recipientEmail, $senderName & $emailTemplateVariables as per your need.
To load a email template, you must specifiy the tag name after
<template>
<email>
</email>
</template>
that you provided in the config.xml
I think you made a mistake here.
$emailTemplate = Mage::getModel('core/email_template')->loadDefault('coeus_recurring_order_email');
try this
$emailTemplate = Mage::getModel('core/email_template')
->loadDefault('recurring_order_email_template');
to load a email template you have to give the tag name that you provide in the config.xml
eg: in you code
use
<recurring_order_email_template>
to load email template
As we send shippment emails to customers, in this shippment email, we get tracking number from track.phtml and now the subject part of the email is like, order number and shippment number. I would like to change the subject of the email in which we are senidng as shippment emails. The subject has to be with tracking number. For order number we can get with "order.increment_id", But I dont know how to get the tracking number. SO how can I display tracking number in subject of email?
This cannot be done only by using a variable name like "order.increment_id" in the email template. You have to send the tracking data to the email template processor to achieve this with an example in 4 steps.
Step1>> Add Module configuration
In app/etc/modules/Eglobe_Sales.xml
<?xml version="1.0"?>
<config>
<modules>
<Eglobe_Sales>
<active>true</active>
<codePool>local</codePool>
</Eglobe_Sales>
</modules>
</config>
Step2>> Add config.xml(app/code/local/Eglobe/Sales/etc/config.xml)
<?xml version="1.0"?>
<config>
<modules>
<Eglobe_Sales>
<version>0.1.0</version>
</Eglobe_Sales>
</modules>
<global>
<models>
<sales>
<rewrite>
<order_shipment>Eglobe_Sales_Model_Order_Shipment</order_shipment>
</rewrite>
</sales>
</models>
</global>
</config>
Step3>> Override Mage_Sales_Model_Order_Shipment::sendEmail()
<?php
class Eglobe_Sales_Model_Order_Shipment extends Mage_Sales_Model_Order_Shipment {
/**
* Send email with shipment data
*
* #param boolean $notifyCustomer
* #param string $comment
* #return Mage_Sales_Model_Order_Shipment
*/
public function sendEmail($notifyCustomer = true, $comment = '')
{
$order = $this->getOrder();
$storeId = $order->getStore()->getId();
if (!Mage::helper('sales')->canSendNewShipmentEmail($storeId)) {
return $this;
}
// Get the destination email addresses to send copies to
$copyTo = $this->_getEmails(self::XML_PATH_EMAIL_COPY_TO);
$copyMethod = Mage::getStoreConfig(self::XML_PATH_EMAIL_COPY_METHOD, $storeId);
// Check if at least one recepient is found
if (!$notifyCustomer && !$copyTo) {
return $this;
}
// Start store emulation process
$appEmulation = Mage::getSingleton('core/app_emulation');
$initialEnvironmentInfo = $appEmulation->startEnvironmentEmulation($storeId);
try {
// Retrieve specified view block from appropriate design package (depends on emulated store)
$paymentBlock = Mage::helper('payment')->getInfoBlock($order->getPayment())
->setIsSecureMode(true);
$paymentBlock->getMethod()->setStore($storeId);
$paymentBlockHtml = $paymentBlock->toHtml();
} catch (Exception $exception) {
// Stop store emulation process
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
throw $exception;
}
// Stop store emulation process
$appEmulation->stopEnvironmentEmulation($initialEnvironmentInfo);
// Retrieve corresponding email template id and customer name
if ($order->getCustomerIsGuest()) {
$templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
$customerName = $order->getBillingAddress()->getName();
} else {
$templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);
$customerName = $order->getCustomerName();
}
$mailer = Mage::getModel('core/email_template_mailer');
if ($notifyCustomer) {
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($order->getCustomerEmail(), $customerName);
if ($copyTo && $copyMethod == 'bcc') {
// Add bcc to customer email
foreach ($copyTo as $email) {
$emailInfo->addBcc($email);
}
}
$mailer->addEmailInfo($emailInfo);
}
// Email copies are sent as separated emails if their copy method is 'copy' or a customer should not be notified
if ($copyTo && ($copyMethod == 'copy' || !$notifyCustomer)) {
foreach ($copyTo as $email) {
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($email);
$mailer->addEmailInfo($emailInfo);
}
}
// Set all required params and send emails
$mailer->setSender(Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY, $storeId));
$mailer->setStoreId($storeId);
$mailer->setTemplateId($templateId);
$mailer->setTemplateParams(array(
'order' => $order,
'shipment' => $this,
'comment' => $comment,
'billing' => $order->getBillingAddress(),
'payment_html' => $paymentBlockHtml,
//setting the `track number here, A shihpment can have more than one track numbers so seperating by comma`
'tracks' => new Varien_Object(array('tracking_number' => implode(',', $this->getTrackingNumbers())))
)
);
$mailer->send();
return $this;
}
//Creating track number array
public function getTrackingNumbers()
{
$tracks = $this->getAllTracks();
$trackingNumbers = array();
if (count($tracks)) {
foreach ($tracks as $track) {
$trackingNumbers[] = $track->getNumber();
}
}
return $trackingNumbers;
}
}
Step4>> Modify your shipment email tempate subject by adding {{var tracks.track_number}}
I develop my store in magento community edition 1.5.0.1. I need a Email template that content will be editable by admin. I create a email template through admin "Transactional Emails". Now I need to access and use that email from my custom module. How do I get it?, you have any idea let me know.
This should do it.
public function sendTransactionalEmail() {
// Transactional Email Template's ID
$templateId = 1;
// Set sender information
$senderName = Mage::getStoreConfig('trans_email/ident_support/name');
$senderEmail = Mage::getStoreConfig('trans_email/ident_support/email');
$sender = array('name' => $senderName,
'email' => $senderEmail);
// Set recepient information
$recepientEmail = 'john#example.com';
$recepientName = 'John Doe';
// Get Store ID
$storeId = Mage::app()->getStore()->getId();
// Set variables that can be used in email template
$vars = array('customerName' => 'customer#example.com',
'customerEmail' => 'Mr. Nil Cust');
$translate = Mage::getSingleton('core/translate');
// Send Transactional Email
Mage::getModel('core/email_template')
->sendTransactional($templateId, $sender, $recepientEmail, $recepientName, $vars, $storeId);
$translate->setTranslateInline(true);
}