How to disable email sending when programatically creating an order in Magento

Featured Image

Here is a little snippet of code that can come in handy if you are like me handling a task of programatically creating an order in Magento. My issue was that I had to disable emails beeing sent out to the customers, while at the same time leave the normal frontend process. Thus I could not just go under Magento admin System > Congifuration and disable email sending from there.

Here is the sample line code you just need to place above final $order->save() or possibly $checkout->saveOrder():

Mage::app()->getStore()->setConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, "0");
8
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

Vedran Subotic

Ex. Inchooer

Vedran worked in Inchoo from 2009 to 2012 as a Senior backend developer and as a team leader.

Other posts from this author

Discussion 8 Comments

Add Comment
  1. nice. I assume it needs to be turned back on again after saving the order?

  2. @Jonathan
    Config is not saved so you don’t need to turn anything back. This code is merely used to overide saved config value at the moment you need it.

  3. David

    Interestingly this doesn’t seem to work with the newsletter subscription. The email is still sent.

    Mage::app()->getStore()->setConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_ENABLED, "0");
    Mage::getModel('newsletter/subscriber')->subscribe('example@example.com');
    

    This is on CE 1.4.

  4. nico

    Hi,

    thanks for sharing this tip, but I havea slightly different problem and maybe you can help.

    When I’m enabling the ordermail system-> configuration -> Sales Email -> Order
    both the customer and the admin get the same email.

    But I just want the admin to get the email because theres another backend sending the emails to the customer.

    Do you have an idea?

  5. -G-

    Can I disable the Order Confirmation email only ?

    Customers receive 3 auto emails.

    Order confirmation, order complete & Order shipped.

    latter are processed at the same time and are redundant.

    Thanks -

  6. robin

    Can I disable the welcome e-mail?
    I have a silent account creation so customers don’t need to register explicitly, but an account is made so they can check their order status.
    They receive 3 e-mails when placing an order: welcome email, separate password email and confirmation email

  7. @robin

    sure you can…
    try to trace the code in the class:

    Mage_Customer_Model_Customer

    and trace which XML_PATH you need

  8. Kurtis

    @robin

    This worked for me

    Mage::app()->getStore()->setConfig(Mage_Customer_Model_Customer::XML_PATH_REGISTER_EMAIL_TEMPLATE, "0");
    				Mage::app()->getStore()->setConfig(Mage_Customer_Model_Customer::XML_PATH_REGISTER_EMAIL_IDENTITY, "0"); 

    in a custom customer.php file we have. Worked for my english store, but not in my french store. ;P

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top