My observer :
<?php class Gta_MerchantNotification_Model_Observer { public function merchantremainder($Observer) { $order = $Observer->getEvent()->getOrder(); $order_details = $order->getAllVisibleItems(); foreach ($order_details as $list) { $incrementid = $order->getIncrementId(); $sku = $list->getsku(); $name = $list->getName(); $price = $list->getPrice(); $Qty = $list->getQtyOrdered(); Mage::log("Id : $incrementid, Sku : $sku, Product name : $name, Price : $price, Qty Ordered : $Qty", null, 'merchant.log', true); } } } ?>
output :
Id : 100000083, Sku : #14, Product name : The Step F1004 Step Workout System, Price : 28.5, Qty Ordered : 1
How to send mail?
@Aveeva you can use the mail function for sending mail.
<?php $body = "Hi there, here is some plaintext body content"; $mail = Mage::getModel('core/email'); $mail->setToName('John Customer'); $mail->setToEmail('customer@email.com'); $mail->setBody($body); $mail->setSubject('The Subject'); $mail->setFromEmail('yourstore@url.com'); $mail->setFromName("Your Name"); $mail->setType('text');// You can use 'html' or 'text' try { $mail->send(); Mage::getSingleton('core/session')->addSuccess('Your request has been sent'); $this->_redirect(''); } catch (Exception $e) { Mage::getSingleton('core/session')->addError('Unable to send.'); $this->_redirect(''); }
Thanks
@rahul Gupta Thank you, How to add if condition for sender,
<?php class Gta_MerchantNotification_Model_Observer { public function merchantremainder($Observer) { $order = $Observer->getEvent()->getOrder(); $order_details = $order->getAllVisibleItems(); $itemData = array(); foreach ($order_details as $list) { $incrementid = $order->getIncrementId(); $sku = $list->getsku(); $name = $list->getName(); $price = $list->getPrice(); $Qty = $list->getQtyOrdered(); $message = "<tr> <td>$incrementid</td> <td>$sku</td> <td>$name</td> <td>$price</td> <td>$Qty</td> </tr>"; $itemData[$list->getId()] = $message; } $finalMessage = "<table border='1'> <tr> <th>Id</th> <th>Sku</th> <th>Product name</th> <th>Price</th> <th>Qty Ordered</th> </tr>"; if (!empty($itemData)) { foreach ($itemData as $data) { $finalMessage .= $data; } $finalMessage .= "</table>"; $this->sendMail($finalMessage); } } public function sendMail($message) { $body = "$message"; $emailTemplate = Mage::getModel('core/email'); $emailTemplate->setFromName('abc'); $emailTemplate->setBody($body); $emailTemplate->setSubject("Custom Email from observer"); $emailTemplate->setType('html'); if($sku = '223355') { $emailTemplate->setToEmail('ewq@gmail.com'); } elseif($sku = '3355') { $emailTemplate->setToEmail('qwe@gmail.com'); } elseif($sku = '993355') { $emailTemplate->setToEmail('xyz@gmail.com'); } else{ $emailTemplate->setToEmail('abc@gmail.com'); } $emailTemplate->send(); } }
Any help with my error,
Code :
```
<?php
class Gta_MerchantNotification_Model_Observer {
public function merchantremainder($Observer) {
$order = $Observer->getEvent()->getOrder();
$order_details = $order->getAllVisibleItems();
$itemData = array();
foreach ($order_details as $list) {
$incrementid = $order->getIncrementId();
$sku = $list->getsku();
$name = $list->getName();
$price = $list->getPrice();
$Qty = $list->getQtyOrdered();
$this->sendMailbasedOnSku($sku);
$message = "<tr>
<!-- <td>$incrementid</td> -->
<td>$sku</td>
<td>$name</td>
<td>$price</td>
<td>$Qty</td>
</tr>";
$itemData[$list->getId()] = $message;
}
$finalMessage = "
<p>Order Id : $incrementid</p>
<table border='1'>
<tr>
<!-- <th>Id</th> -->
<th>Sku</th>
<th>Product name</th>
<th>Price</th>
<th>Qty Ordered</th>
</tr>";
if (!empty($itemData)) {
foreach ($itemData as $data) {
$finalMessage .= $data;
}
$finalMessage .= "</table>";
$this->sendMail($finalMessage);
}
}
public function sendMail($message) {
$body = "$message";
$emailTemplate = Mage::getModel('core/email');
$emailTemplate->setFromName('Test mail');
$emailTemplate->setBody($body);
$emailTemplate->setSubject("Custom Email from observer");
$emailTemplate->setType('html');
// $emailTemplate->setToEmail('abc@gmail.com');
if($sku == '2')
{
$emailTemplate->setToEmail('abc@gmail.com');
}
elseif($sku == '3')
{
$emailTemplate->setToEmail('xyz@gmail.com');
}
elseif($sku == '4')
{
$emailTemplate->setToEmail('qwe@gmail.com');
}
else
{
$emailTemplate->setToEmail('ewq@gmail.com');
}
$emailTemplate->send();
}
public function sendMailbasedOnSku($sku)
{
// sku is 22302
$chk_sku=(int)substr($sku, 0, 1);
if($chk_sku == '2')
{
$emailTemplate->setToEmail('abc@gmail.com');
}
elseif($chk_sku == '3')
{
$emailTemplate->setToEmail('xyz@gmail.com');
}
elseif($chk_sku == '4')
{
$emailTemplate->setToEmail('qwe@gmail.com');
}
else{
$emailTemplate->setToEmail('ewq@gmail.com');
}
try{
return $emailTemplate->send();
Mage::getSingleton('core/session')->addSuccess('Your request has been sent');
}
catch (Exception $e) {
Mage::getSingleton('core/session')->addError($e->getMessage());
}
}
}
?>
```
error :
```
[09-Jan-2020 04:53:42 UTC] PHP Fatal error: Uncaught Error: Call to a member function setToEmail() on null in C:\wamp64\www\magento\app\code\local\Gta\MerchantNotification\Model\Observer.php:99
Stack trace:
#0 C:\wamp64\www\magento\app\code\local\Gta\MerchantNotification\Model\Observer.php(15): Gta_MerchantNotification_Model_Observer->sendMailbasedOnSku('22')
#1 C:\wamp64\www\magento\app\code\core\Mage\Core\Model\App.php(1358): Gta_MerchantNotification_Model_Observer->merchantremainder(Object(Varien_Event_Observer))
#2 C:\wamp64\www\magento\app\code\core\Mage\Core\Model\App.php(1337): Mage_Core_Model_App->_callObserverMethod(Object(Gta_MerchantNotification_Model_Observer), 'merchantremaind...', Object(Varien_Event_Observer))
#3 C:\wamp64\www\magento\app\Mage.php(448): Mage_Core_Model_App->dispatchEvent('checkout_submit...', Array)
#4 C:\wamp64\www\magento\app\code\core\Mage\Checkout\Model\Type\Onepage.php(872): Mage::dispatchEvent('checkout_submit...', Array)
#5 C:\wamp64\www\magento\app\code\core\Mage\Checkout\controllers\OnepageController.php(579): M in C:\wamp64\www\magento\app\code\local\Gta\MerchantNotification\Model\Observer.php on line 99
```