阻止仅在特定条件下发送WooCommerce电子邮件

时间:2018-01-18 作者:Sefran2

通过在WooCommerce中从Amazon导入订单并以编程方式创建WooCommerce订单,可以发送新订单或订单状态更改的电子邮件。

是否可以阻止此电子邮件至少在创建新订单时发送?我可以对自定义字段值使用条件,但我必须使用哪些操作/筛选器?

EDIT

我正在测试woocommerce_email_enabled_new_order, woocommerce_email_enabled_customer_completed_orderwoocommerce_email_enabled_customer_processing_order过滤器与自定义设置一起使用,目前似乎可以正常工作。

3 个回复
SO网友:rudtek

如果您有一个自定义字段或其他字段,只需使用它来删除下面选择的操作。我不确定你想停止哪些电子邮件,所以我包括了所有内容,但显然只是选择你需要的。

如果您是以编程方式进行导入,那么您可以在运行导入时只包含所选的remove\\u操作行。

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

SO网友:Vital
/**
* 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;

}
SO网友:passatgt

有一个更好的方法可以做到这一点。您可以使用“woocommerce\\u email\\u enabled{email\\u id}”筛选是否启用。这在发送电子邮件之前运行。如果条件匹配,您只需返回no即可。第二个参数是$order,因此可以在那里检查自定义字段。

结束

相关推荐

Sending email to all users

向大量用户发送电子邮件的最佳做法是什么?我正在为一个协会的网站开发一个插件,需要做的事情之一是能够向协会的所有成员(或特定子集)发送电子邮件。这将是一件相当罕见的事情,但一旦发生,预计将发送500至1000封电子邮件。现在,我可以想出几个选择:分别发送每封电子邮件,每封50/100块发送一封电子邮件,为每个人发送一封电子邮件。选项1很好,因为我可以对电子邮件进行个性化设置(亲爱的姓名……),然而,对于这些情况,我们可以使用普通电子邮件。每个选项的缺点是什么</你能给我一些其他的策略建议吗?我只是想用