2

By importing orders from Amazon in WooCommerce and creating WooCommerce orders programmatically, emails for new order or order status changing are sent.

Is it possible to prevent this email sending at least on new order creation? I could use a condition on a custom field value, but what actions/filters I have to use?

EDIT

I'm testing woocommerce_email_enabled_new_order, woocommerce_email_enabled_customer_completed_order and woocommerce_email_enabled_customer_processing_orderfilters together with custom settings and for now it seems to work.

3
  • look in the WooCommerce configuration, there is a tab "E-mails" where you can configure and disable all e-mails.
    – mmm
    Jan 18, 2018 at 9:32
  • @mmm I don't want disable completely the email sending, but not sending them when I create the WC orders programmatically, for example when I import them from Amazon marketplace.
    – Sefran2
    Jan 18, 2018 at 10:43
  • How are you creating your orders, is there any code you can share us? When we know how to distinguish regular orders from imported ones, we can help you. Also check out this similar question.
    – swissspidy
    Jan 22, 2018 at 12:11

3 Answers 3

2

Theres a better way to do this. You can filter is_enabled with 'woocommerce_email_enabled_{email_id}'. This runs before sending out the emails. You can just simply return no if conditions match. The second parameter is the $order, so you can check your custom field there.

1

if you have a custom field or etc just use it to remove the action of your choice below. I wasn't sure which emails you wanted stopped so i included all but obviously just choose what you need.

If you are doing the import programmatically then you could just include the remove_action lines of your choosing while running the import as well.

add_action( 'woocommerce_email', 'unhook_those_pesky_emails' );

function unhook_those_pesky_emails( $email_class ) {

    /**
     * Hooks for sending emails during store events
     **/
    remove_action( 'woocommerce_low_stock_notification', array( $email_class, 'low_stock' ) );
    remove_action( 'woocommerce_no_stock_notification', array( $email_class, 'no_stock' ) );
    remove_action( 'woocommerce_product_on_backorder_notification', array( $email_class, 'backorder' ) );

    // New order emails
    remove_action( 'woocommerce_order_status_pending_to_processing_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
    remove_action( 'woocommerce_order_status_pending_to_completed_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
    remove_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
    remove_action( 'woocommerce_order_status_failed_to_processing_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
    remove_action( 'woocommerce_order_status_failed_to_completed_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );
    remove_action( 'woocommerce_order_status_failed_to_on-hold_notification', array( $email_class->emails['WC_Email_New_Order'], 'trigger' ) );

    // Processing order emails
    remove_action( 'woocommerce_order_status_pending_to_processing_notification', array( $email_class->emails['WC_Email_Customer_Processing_Order'], 'trigger' ) );
    remove_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $email_class->emails['WC_Email_Customer_Processing_Order'], 'trigger' ) );

    // Completed order emails
    remove_action( 'woocommerce_order_status_completed_notification', array( $email_class->emails['WC_Email_Customer_Completed_Order'], 'trigger' ) );

    // Note emails
    remove_action( 'woocommerce_new_customer_note_notification', array( $email_class->emails['WC_Email_Customer_Note'], 'trigger' ) );
}
3
  • This code removes the email sending also if an order is created via WooCommerce checkout. If I check an order custom field in unhook_those_pesky_emails (by adding $order arg) doesn't work.
    – Sefran2
    Jan 24, 2018 at 10:45
  • In the function i provided above, sure. However I'm showing you all the actions for emails. What does your import function look like? What is the custom field? I can't get more specific without those details.
    – rudtek
    Jan 24, 2018 at 18:39
  • I'm testing woocommerce_email_enabled_new_order, woocommerce_email_enabled_customer_completed_order and woocommerce_email_enabled_customer_processing_orderfilters together with custom settings and for now it seems to work.
    – Sefran2
    Jan 25, 2018 at 8:19
1
/**
* Prevent sending emails in woocommerce based on custom logic
*/

function action_woocommerce_before_save_order_items( $order_id, $items ) {
  $_SESSION['woocommerce']['woocommerce_before_save_order_id'] = $order_id;
};
add_action( 'woocommerce_before_save_order_items', 'action_woocommerce_before_save_order_items', 10, 2 );

add_action( 'woocommerce_before_checkout_form', function($some){ // executing on checkout page
  $_SESSION['woocommerce']['woocommerce_before_save_order_id'] = false;
}, 10, 1 );



add_action( 'woocommerce_email', 'unhook_those_pesky_emails' );
function unhook_those_pesky_emails( $email_class ) {

if($_SESSION['woocommerce']['woocommerce_before_save_order_id']){

    $order = wc_get_order($_SESSION['woocommerce']['woocommerce_before_save_order_id']);
    $shipping_items = $order->get_items('shipping');
    $shipping_item  = reset($shipping_items);
    $shipping_method_id  = $shipping_item->get_method_id() . ':';
    $shipping_method_id .= $shipping_item->get_instance_id();





      if($shipping_method_id == 'flat_rate:7' && $order->get_status() == 'processing'){


      add_filter( 'woocommerce_email_enabled_new_order', function($yesno, $object){
        return false;
      }, 10, 2);
      add_filter( 'woocommerce_email_enabled_cancelled_order', function($yesno, $object){
        return false;
      }, 10, 2);
      add_filter( 'woocommerce_email_enabled_failed_order', function($yesno, $object){
        return false;
      }, 10, 2);
      add_filter( 'woocommerce_email_enabled_customer_on_hold_order', function($yesno, $object){
        return false;
      }, 10, 2);
      add_filter( 'woocommerce_email_enabled_customer_processing_order', function($yesno, $object){
        return false;
      }, 10, 2);
      add_filter( 'woocommerce_email_enabled_customer_completed_order', function($yesno, $object){
        return false;
      }, 10, 2);
      add_filter( 'woocommerce_email_enabled_customer_refunded_order', function($yesno, $object){
        return false;
      }, 10, 2);
      add_filter( 'woocommerce_email_enabled_customer_invoice', function($yesno, $object){
        return false;
      }, 10, 2);
      add_filter( 'woocommerce_email_enabled_customer_note', function($yesno, $object){
        return false;
      }, 10, 2);
      add_filter( 'woocommerce_email_enabled_customer_reset_password', function($yesno, $object){
        return false;
      }, 10, 2);
      add_filter( 'woocommerce_email_enabled_customer_new_account', function($yesno, $object){
        return false;
      }, 10, 2);


    }

}

$_SESSION['woocommerce']['woocommerce_before_save_order_id'] == false;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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