0

I would like to send my customers a mail, after a week, when the order has been completed.

Can someone help me? (Magento 1.9.2).

I also found a topic about this issue, but it is not updated for a while.

My code for so far:

Config.xml:

    <?xml version="1.0"?>
<config>
  <modules>
    <Productreviews_Productreview>
      <version>0.1.0</version>
    </Productreviews_Productreview>
  </modules>
  <global>
    <helpers>
      <cronexample>
        <class>Productreviews_Productreview_Helper</class>
      </cronexample>
    </helpers>
    <models>
      <cronexample>
        <class>Productreviews_Productreview_Model</class>
        <resourceModel>cronexample_mysql4</resourceModel>
      </cronexample>
    </models>
  </global>    
    <crontab>
        <jobs>            
            <send_productreview>
                <schedule><cron_expr>5 * * * *</cron_expr></schedule>
                <run><model>cronexample/cron::cronSendReviewMail</model></run>
            </send_productreview>
        </jobs>
    </crontab>
</config> 

Cron.php:

<?php
class Productreviews_Productreview_Model_Cron{   
    public function cronSendReviewMail(){
        Mage::log("Cron Test ".now(),null,"mageCronExample.log");

$time = time();
$to = date('Y-m-d H:i:s', $time);
$lastTime = $time - 604800; // 60*60*7*24
$from = date('Y-m-d H:i:s', $lastTime);

$order_collection = Mage::getModel('sales/order')
->getCollection()
->addAttributeToSelect('*')
->setPageSize(5)
->setOrder('created_at', 'desc')
->addAttributeToFilter('created_at', array('from' => $from, 'to' => $to))
->addAttributeToFilter('status', array('==' => array('complete')));

                foreach ($order_collection as $item) {
                    // echo $item->getId();

                    $sendtoemail = $item->getShippingAddress()->getEmail();

                    $idid = $item->getIncrementId();
                    $order = $item->loadByIncrementId($idid);
                    $items = $order->getAllVisibleItems();

                        // EMAIL LAYOUT
                        $email = 'Email content here';

                            // EMAIL VERSTUREN
                            $to      = $sendtoemail;
                            $subject = 'Subject';
                            $headers = 'From: x@example.com' . "\r\n" .
                                'Reply-To: x@example.com' . "\r\n" .
                                'X-Mailer: PHP/' . phpversion();
                            $headers .= "MIME-Version: 1.0\r\n";
                            $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

                            mail($to, $subject, $email, $headers);      
                }

    }
}
  • you want to send mail immediate after order complete or after a week of order complete. – Ashish Madankar M2 Professiona May 11 '16 at 13:50
  • Customers should receive a mail after a week of order complete. – Dneiz May 11 '16 at 20:14
1

For this you have to use cron which will execute on daily basis and fetch order data, depending upon created_at field of collection, you can decide the order of your required duration some thing like all orders whose created_at value is less than a week and order status is complete. for this you can use the following code.

$time = time();
$to = date('Y-m-d H:i:s', $time);
$lastTime = $time - 604800; // 60*60*7*24
$from = date('Y-m-d H:i:s', $lastTime);
$order_items = Mage::getResourceModel('sales/order_item_collection')
    ->addAttributeToSelect('order_id')
    ->addAttributeToSelect('created_at')
    ->addAttributeToFilter('created_at', array('from' => $from, 'to' => $from))
    ->addAttributeToFilter('status', array('eq' => Mage_Sales_Model_Order::STATE_COMPLETE))
    ->load();

and to create a cron you can follow the following tutorials.

http://inchoo.net/magento/creating-cron-script-in-magento/

Hope this will help you.

  • Thank you very much. I edited the code and it is working fine. However, is there a way to start the cron after the order has been completed? Finally, it needs to be like: order finished at 01-05-2016 --> customer receives a mail at 07-05-16. Thanks! – Dneiz May 12 '16 at 15:08
  • For this difference just replace the 604800 in line $lastTime = $time - 604800; // 60*60*7*24 with 518400 this will provide you the exact record. for testing you can try $time = strtotime('2016-05-07'); echo '<br> TO - '.$to = date('Y-m-d H:i:s', $time); $lastTime = $time - 518400; // 60*60*7*24 echo '<br>FROM - '.$from = date('Y-m-d H:i:s', $lastTime); – Ashish Madankar M2 Professiona May 12 '16 at 17:59
  • Thank you again. However, you stated that the cron needs to be executed on daily basis and completed order date is set to 7 days. Is this the right way to do this? Because customers will receive a mail again on day 2 now. I also added the my code above now. – Dneiz May 14 '16 at 11:04
  • How? firstly on day 2 difference is going to be 8 days so this order is not going to select on second days and you can use custom table which stores last order processed by the cron and add a condition to not to select order for which emails are already sended. – Ashish Madankar M2 Professiona May 14 '16 at 12:57
0

Me too faced this same situation. I solved with Following Code:

Create a file called sendmail.php in root folder with the following code:

require_once ("app/Mage.php");
ini_set("error_reporting",E_ALL);
ini_set("display_errors",true);
umask(0);|
Mage::app('admin');
$time = time();
$to = date('Y-m-d H:i:s', $time);
$lastTime = $time - 604800; // 60*60*7*24
$from = date('Y-m-d H:i:s', $lastTime);
$lastweek = date('Y-m-d', strtotime("-7 days"));
//Get Completed Order Collection placed in last one week 
$orders = Mage::getModel('sales/order')->getCollection()
->addAttributeToFilter('created_at', array('from' => $lastweek .' 00:00:00', 'to' => $lastweek. ' 23:59:59'))
->addAttributeToFilter('status', array('in' => Mage_Sales_Model_Order::STATE_COMPLETE));
foreach($orders as $order)  
{
    $order_incr_id = base64_encode($order->getIncrementId());

    $template = 38;// Template Id, You Can Get from System Config too
    $storeId = Mage::app()->getStore()->getId();
    $store_name = Mage::app()->getWebsite()->getName();
    $website_id = Mage::getModel('core/store')->load($order->getStoreId())->getWebsiteId();
    $website = Mage::app()->getWebsite($website_id);
    $website_name = strtolower($website->getName());
    $template = Mage::getStoreConfig('statuschange_options/quote_email/rating_template');
    $translate = Mage::getSingleton('core/translate');
    $translate->setTranslateInline(false);

    $mailSuccess = Mage::getModel('core/email_template')->setDesignConfig(
        array('area' => 'frontend', 'store' => $storeId))
            ->sendTransactional(
                $template,
                $sender,
                $order->getCustomerEmail(),
                $order->getCustomerFirstname(),
                array(
                //You Can Add Any Parameters to be Passed in the mail
                    'sender'  => $senderName,
                    'username' => $order->getCustomerFirstname(),
                    'order_id' => $order->getIncrementId(),
                    'storename' => $website_name,
                    'customer_id' => $order->getCustomerId(),
                    'order_incr_id' => $order_incr_id
                    )
            );

            if($mailSuccess){
                // If mail Sent it will be Logged with Customer name, Order Id and Mail Id
                Mage::log('Mail sent. OrderId: '. $order->getIncrementId().' '.'Customer Name: '.$order->getCustomerFirstname().' '.$order->getCustomerLastname().' '.'MailId: '. $order->getCustomerEmail()
                , null, $lastweek.'-'.'MailSent.log');
            }
            else {
                // If mail Not Sent it will be Logged with Customer name, Order Id and Mail Id
                Mage::log('Mail not sent. OrderId: '. $order->getIncrementId().' '.'Customer Name: '.$order->getCustomerFirstname().' '.$order->getCustomerLastname().' '.'MailId: '.$order->getCustomerEmail()
                , null, $lastweek.'MailNotSent.log');
            }
            }

Note:

You Need to Set in Cron File to Run when it to be run. I set it to run in the 11:55:00 PM

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.