最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Preventing WooCommerce email sending only on particular conditions

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Jan 25, 2018 at 8:21 Sefran2 asked Jan 18, 2018 at 9:26 Sefran2Sefran2 791 silver badge7 bronze badges 3
  • look in the WooCommerce configuration, there is a tab "E-mails" where you can configure and disable all e-mails. – mmm Commented 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 Commented 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 Commented Jan 22, 2018 at 12:11
Add a comment  | 

3 Answers 3

Reset to default 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.

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' ) );
}
/**
* 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;

}
发布评论

评论列表(0)

  1. 暂无评论