2

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 ?

2

Here is a simple way of doing it.
Rewrite the method Mage_Sales_Model_Order::sendNewOrderEmail and make it look like this:

public function sendNewOrderEmail() {
    if (!$this->getForceSendEmail()) {
        return $this;
    }
    //do what it does by default
    parent::sendNewOrderEmail();
}

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:

$orders = ....; //retrieve orders somehow
$foreach ($orders as $order) {
    $order->setForceSendEmail(true); //tell the order to force the e-mail
    $order->sendNewOrderEmail();
}
  • Thanks for your quick reply but I am little bit confused about this code: $orders = ....; //retrieve orders somehow $foreach ($orders as $order) { $order->setForceSendEmail(true); //tell the order to force the e-mail $order->sendNewOrderEmail(); } Should I add this code in order.php in sendneworderemail() or in my cron.php ? – Triyugi Narayan Mani Aug 8 '14 at 6:52
  • @user3719788. That should go in your cron. BUt you shouldn't edit cron.php. Create a model that would do this and then set it as a cron. See here how you can create a cron – Marius Aug 8 '14 at 6:57
  • <sales_order> <schedule> <cron_expr>0,15,30,45 * * * *</cron_expr> </schedule> <run> <model>sales/order::sendNewOrderEmail</model> </run> </sales_order> Is it right? Please check – Triyugi Narayan Mani Aug 8 '14 at 7:19
  • That won't work. Because you need separate instances of orders to send e-mails. – Marius Aug 8 '14 at 7:26
  • 1
    That's a totally different question. Let's keep questions separate. It is also a large topic. You should ask a new question explaining how you plan to send the e-mails. Give as much details as possible. The more details you give the better the answers might be. – Marius Aug 8 '14 at 7:30
1

Magento is send new order mail from Mage_Sales_Model_Sales class function sendNewOrderEmail

In overrive this function using Override class make this function

 public function sendNewOrderEmail(){
return;
}

Create another function which code is replicated function CustomsendNewOrderEmail from sendNewOrderEmail() Assume

Code

setCouponCode('bal'); //$this->_getResource()->saveAttribute($this, 'coupon_code');

    return;
}
public function CustomsendNewOrderEmail(){
    parent::sendNewOrderEmail();
}

}

Then Using cron job hit this function:

$Order=Mage::getModel('sales/order')->load($orderId);
$Order->CustomsendNewOrderEmail();

Create a module:

config.xml code ((app/code/local/Bh/ZeroSubtotalpaymentmethod/etc/))

<?xml version="1.0" ?>
<config>
    <modules>
        <Bh_ZeroSubtotalpaymentmethod>
            <version>1.0.1</version>
        </Bh_ZeroSubtotalpaymentmethod>
    </modules>
    <global>
        <models>
            <zerosubtotalpaymentmethod>
            <class>Bh_ZeroSubtotalpaymentmethod_Model</class>
            </zerosubtotalpaymentmethod>
            <sales>
                <rewrite>
                    <order>Bh_ZeroSubtotalpaymentmethod_Model_Order</order>
                </rewrite>
            </sales>
        </models>
    </global>
    <crontab>
    <jobs>
        <autosend_order>
            <schedule><cron_expr>20 * * * *</cron_expr></schedule>
            <run><model>zerosubtotalpaymentmethod/observer::myordersend</model></run>
        </autosend_order>
    </jobs>
    </crontab>
</config>

**Order.php ,which rewrite Order.php**(app/code/local/Bh/ZeroSubtotalpaymentmethod/Model/)

<?php
class Bh_ZeroSubtotalpaymentmethod_Model_Order extends Mage_Sales_Model_Order{
    public function sendNewOrderEmail(){
        // $this->setCouponCode('bal');
        //$this->_getResource()->saveAttribute($this, 'coupon_code');

        return;
    }
    public function CustomsendNewOrderEmail(){
        parent::sendNewOrderEmail();
    }
}

Cron file Observer.php(app/code/local/Bh/ZeroSubtotalpaymentmethod/Model/)

<?php
class Bh_ZeroSubtotalpaymentmethod_Model_Observer
{
    public function myordersend(){

    $Order_collection = Mage::getModel("sales/order")->getCollection()->addFieldToFilter('email_sent',array('neq'=>array(1)));
            foreach($Order_collection as $Order)
            {
                try {

                $Myorder=Mage::getModel('sales/order')->load($Order->getId());
                $Myorder->CustomsendNewOrderEmail();
                }catch(Exception $e){
                }
            }
    }
}

also create module

app/etc/modules/Bh_ZeroSubtotalpaymentmethod.xml

Code:

<?xml version="1.0" ?>
<config>
<modules>
    <Bh_ZeroSubtotalpaymentmethod>
        <active>true</active>
        <codePool>local</codePool>
    </Bh_ZeroSubtotalpaymentmethod>
</modules>
</config>

Know more about cron job and it syntax check this

http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/how_to_setup_a_cron_job

  • Please tell me how can I hit the function using cron job? – Triyugi Narayan Mani Aug 8 '14 at 7:24
  • are you using magento cron system?? – Amit Bera Aug 8 '14 at 7:34
  • yes I am using magento cron system – Triyugi Narayan Mani Aug 8 '14 at 7:36
  • give you shortly – Amit Bera Aug 8 '14 at 7:48
  • 1
    Why did you copy the whole content from sendNewOrderEmail into CustomsendNewOrderEmail? Surely you could just call: return parent::sendNewOrderEmail() within your custom method? – ash Aug 8 '14 at 8:22

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.