What i need to do is this: I need to send an email to the customer

if his order has been pending (or some other custom status) for two days and 

cancel the order if this time becomes 3 days.

The email has to have the order and a custom message.

I have found this http://inchoo.net/magento/cancel-pending-orders/ but need some help with the email sending part.

Any help is appreciated.


Hi you a cron job service where you can sen will send email to customer on daily basic.

Need to create an extension will send fetch all order of current status pending sending email an cancel order on three day old. I suggest. Please add comment order which is two day order and send to mailt customer by order update.

You need change your inchoo extension:

public function cancelPendingOrders()

    // load custom variable for the comment
    $msg = Mage::getModel('core/variable')

    // echo $msg;

    $orders = Mage::getModel('sales/order')->getCollection()
    ->addFieldToFilter('status', 'pending')
    ->addFieldToFilter('created_at', array('lt' =>  new Zend_Db_Expr("DATE_ADD('".now()."', INTERVAL -'48' HOUR)"))) ;

    foreach ($orders as $order) {
        // Add the comment and save the order
        $order->addStatusToHistory($order->getStatus(), $msg , $isCustomerNotified = true);


  • Thank you, it was simpler this way than using a custom template. i made some changes to the code because $orderCollection = Mage::getResourceModel('sales/order_collection'); did not work for me so i changed it to $orders = Mage::getModel('sales/order')->getCollection() – Ylli Nov 14 '14 at 11:01
  • is it works.....? – Amit Bera Nov 14 '14 at 11:02
  • Yes it works, i did some tests and i think it works fine but lets wait and see if it shows me any problem in the future – Ylli Nov 14 '14 at 11:03
  • You have to change some other things in your code remove Mage::getModel('sales/order')->loadByIncrementId($eachorder->getIncrementId()); and change $order to $eachorder – Ylli Nov 14 '14 at 11:09

As always, everything can be found on the Inchoo blog (Those guys are great). This post about custom emails should help you out.

  1. Create a HTML email template
  2. Add it to your config.xml
  3. Add the email processor to your script
  4. ...
  5. $$$

In your config.xml declare a template file with the following:

<?xml version="1.0"?>
                <description_of_template translate="label" module="yourmodule">
                    <label>Template Label</label>

Create the template file at location app/locale/(en_US)/template/email/your_template.html. This can of course contain anything you like and it's probably easiest to base it on an existing template to keep formatting consistent. Here is a simple example:

<body style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
    <div style="background:#F6F6F6; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; margin:0; padding:0;">
        <table cellspacing="0" cellpadding="0" border="0" width="100%">
                <td align="center" valign="top" style="padding:20px 0 20px 0">
                    <table bgcolor="#FFFFFF" cellspacing="0" cellpadding="10" border="0" width="650" style="border:1px solid #E0E0E0;">
                            <td valign="top"><a href="{{store url=""}}"><img src="{{var logo_url}}" alt="{{var logo_alt}}" style="margin-bottom:10px;" border="0"/></a></td>
                            <td valign="top">
                                <h1 style="font-size:22px; font-weight:normal; line-height:22px; margin:0 0 11px 0;"">Hello {{htmlescape var=$customer.getName()}}</h1>
                                <p style="font-size:12px; line-height:16px; margin:0;">
                                    Here is the email for {{htmlescape var=$product.getName()}} you recently requested.<br /><br />
                                    If you have any queries or require support contact us at <a href="mailto:{{config path='trans_email/ident_support/email'}}">{{config path='trans_email/ident_support/email'}}</a>.
                            <td bgcolor="#EAEAEA" align="center" style="background:#EAEAEA; text-align:center;"><center><p style="font-size:12px; margin:0;">Thank you, <strong>{{var store.getFrontendName()}}</strong></p></center></td>

You will notice sections of the email such as {{htmlescape var=$product.getName()}} - these are email variables and will get replaced before the email is sent according to the variables you pass when calling send() which we will get to shortly.

Next the code to actually send the email:

$email = Mage::getModel('core/email_template')->loadDefault('description_of_template');
$email->setTemplateSubject('Some Subject');
$email->setSenderName(Mage::getStoreConfig('trans_email/ident_sales/name', 1));
$email->setSenderEmail(Mage::getStoreConfig('trans_email/ident_sales/email', 1));

$product = Mage::getModel('catalog/product')->load($someproductid);
$store = Mage::getModel('core/store')->load($somestoreid);
$customer = Mage::getModel('customer/customer')->load($somecustomerid);

$data = array('product' => $product, 'store' => $store, 'customer' => $customer);

$success = $email->send($customer->getEmail(), $customer->getName(), $data);

if ($success):
    Mage::log('Email sent');
    Mage::log('Could not send email');

In the above the $data array contains the variables which are used as the source of data for replacing the contents of the curly braces so for instance $customer.getName() is the equivalent of calling $customer->getName() on the $customer object passed to the template as customer in the $data array.

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.