I want to disable the default new order email sending functionality and instead I want to send this email with cron job only. How can I identify, if the new order is placed and send email ?
Magento 1.7 – Send New Order Email with Cron Job Only
cronmagento-1.7orders
Related Solutions
I would disable System > Configuration > Sales Email > Order > Enabled
this makes sure that during normal execution it is not send
public function sendNewOrderEmail()
{
$storeId = $this->getStore()->getId();
if (!Mage::helper('sales')->canSendNewOrderEmail($storeId)) {
return $this;
}
Then in your custom module include something like
Mage::getConfig()->setNode(
'default/'.Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, true
);
foreach(Mage::app()->getStores() as $storeCode=>$store){
Mage::getConfig()->setNode(
"stores/{$storeCode}/".Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, true
);
}
$collection = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('email_sent', 0);
foreach ($collection as $order){
$order->sendNewOrderEmail();
}
The main idea being to override the disabled configuration value at runtime. The code is not tested but should give you a starting point. Further recommended reading from Alan's blog: http://alanstorm.com/magento_loading_config_variables http://alanstorm.com/magento_config_a_critique_and_caching
One issue that you might encounter is a cached value for the above.
Second option would be to duplicate the code from sendNewOrderEmail().
You can set your cron job to execute once in a day for example mid night 11PM.
In your newpost
function
Take the current time that means 11PM.
Check the product created datetime.
Load/select obnly those product create before one day(it means product should be created yesterday after 11PM).
So now you have list of product which were created in 24 hours.
Every day at 11PM you corn will execute and send the email regarding products created in 24 hours.
What you say!!
Edit This sets your cron jobs. whcih execute your observer and mailme function
<global>
<models>
<namespace_mycron>
<class>Namespace_Mycron_Model</class>
</namespace_mycron>
</models>
</global>
<crontab>
<jobs>
<namespace_mycron_send>
<schedule><cron_expr>0 0 * * *</cron_expr></schedule>
<run><model>namespace_mycron/observer::mailme</model></run>
</namespace_mycron_send>
</jobs>
</crontab>
your observer function in model
<?php
class Namespace_Mycron_Model_Observer extends Varien_Event_Observer
{
public function mailme()
{
$fromDate = date('Y-m-d H:i:s', strtotime(date('Y-m-d', strtotime(date('Y-m-d') .' -1 day'))));
$toDate = date('Y-m-d H:i:s', strtotime(date('Y-m-d')));
$collection = $this->getCollectionWithDates($fromDate, $toDate);
if ($collection->getSize() > 0) {
//send your email
}
return $this;
}
public function getCollectionWithDates($fromDate, $toDate)
{
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')
->addWebsiteFilter(4);
$collection->addAttributeToFilter('share_mss', array('eq' => 1));//"2016-08-01 00:00:00" example format
$collection->addAttributeToFilter('updated_at', array('gteq' => $fromDate));//"2016-08-01 00:00:00" example format
//$collection->addAttributeToFilter('created_at', array('lteq' => $toDate));
$collection->addAttributeToFilter(
'status',
array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));
return $collection;
}
}
Best Answer
Here is a simple way of doing it.
Rewrite the method
Mage_Sales_Model_Order::sendNewOrderEmail
and make it look like this:This will prevent the e-mail sending from the website because
getForceSendEmail
will always return false (unless you tell it not to).Now, when running the cron to send e-mails you will have to do somthing like this: