停止价值 0 美元产品的 WooCommerce 订阅管理员续订通知

时间:2021-06-07 18:35:56

标签: php wordpress woocommerce woocommerce-subscriptions

我发现以下插件会在价值 0 美元的订阅续订时停止向客户发送通知。这对客户来说非常好,但管理员仍然会收到通知。是否可以修改代码以消除向管理员发送续订电子邮件?

感谢您的帮助

<?php
/*
Plugin Name: WooCommerce Subscriptions No $0 Emails
Plugin URI:
Description: Do not send processing or completed renewal order emails to customers when the order or renewal is for $0.00.
Author:
Author URI:
Version: 0.1
*/
 
function eg_maybe_remove_email( $order_id ){

    $order = new WC_Order( $order_id );

    if ( 0 == $order->get_total() ) {

        switch( current_filter() ) {
            case 'woocommerce_order_status_completed_renewal_notification':
                $email_class = 'WCS_Email_Completed_Renewal_Order';
                break;
            case 'woocommerce_order_status_pending_to_processing_renewal_notification':
                $email_class = 'WCS_Email_Processing_Renewal_Order';
                break;
            case 'woocommerce_order_status_failed_renewal_notification':
                $email_class = 'WCS_Email_Customer_Renewal_Invoice';
                break;
            default:
                $email_class = '';
                break;
        }

        if ( ! empty( $email_class ) ) {
            remove_action( current_filter(), array( WC()->mailer()->emails[ $email_class ], 'trigger' ) );
        }
    }
    
    if ( 0 == $order->get_total() ) {

        switch( current_filter() ) {
            case 'woocommerce_order_status_failed_renewal_notification':
                $email_class = 'WCS_Email_New_Renewal_Order';
                break;
            default:
                $email_class = '';
                break;
        }

        if ( ! empty( $email_class ) ) {
            remove_action( current_filter(), array( WC()->mailer()->emails[ $email_class ], 'trigger' ) );
        }
    }   

}
add_action( 'woocommerce_order_status_completed_renewal_notification', 'eg_maybe_remove_email', 0, 1 );
add_action( 'woocommerce_order_status_pending_to_processing_renewal_notification', 'eg_maybe_remove_email', 0, 1 );
add_action( 'woocommerce_order_status_failed_renewal_notification', 'eg_maybe_remove_email', 0, 1 );

1 个答案:

答案 0 :(得分:1)

这些是 WooCommerce 订阅中使用的以下电子邮件类。

$email_classes['WCS_Email_New_Renewal_Order']              = new WCS_Email_New_Renewal_Order();
$email_classes['WCS_Email_New_Switch_Order']               = new WCS_Email_New_Switch_Order();
$email_classes['WCS_Email_Processing_Renewal_Order']       = new WCS_Email_Processing_Renewal_Order();
$email_classes['WCS_Email_Completed_Renewal_Order']        = new WCS_Email_Completed_Renewal_Order();
$email_classes['WCS_Email_Customer_On_Hold_Renewal_Order'] = new WCS_Email_Customer_On_Hold_Renewal_Order();
$email_classes['WCS_Email_Completed_Switch_Order']         = new WCS_Email_Completed_Switch_Order();
$email_classes['WCS_Email_Customer_Renewal_Invoice']       = new WCS_Email_Customer_Renewal_Invoice();
$email_classes['WCS_Email_Cancelled_Subscription']         = new WCS_Email_Cancelled_Subscription();
$email_classes['WCS_Email_Expired_Subscription']           = new WCS_Email_Expired_Subscription();
$email_classes['WCS_Email_On_Hold_Subscription']           = new WCS_Email_On_Hold_Subscription();

WCS_Email_New_Switch_OrderWCS_Email_New_Switch_Order 这些是唯一向管理员发送电子邮件的类。

以下是在 WCS_Email_New_Switch_OrderWCS_Email_New_Switch_Order 中用于发送电子邮件的操作。

'woocommerce_order_status_pending_to_processing_renewal_notification'
'woocommerce_order_status_pending_to_completed_renewal_notification'
'woocommerce_order_status_pending_to_on-hold_renewal_notification'
'woocommerce_order_status_failed_to_processing_renewal_notification'
'woocommerce_order_status_failed_to_completed_renewal_notification'
'woocommerce_order_status_failed_to_on-hold_renewal_notification'
'woocommerce_order_status_cancelled_to_processing_renewal_notification'
'woocommerce_order_status_cancelled_to_completed_renewal_notification'
'woocommerce_order_status_cancelled_to_on-hold_renewal_notification'
'woocommerce_subscriptions_switch_completed_switch_notification'

因此,您还必须在回调函数 eg_maybe_remove_email 中添加上述操作。检查下面的代码。

<?php
/*
Plugin Name: WooCommerce Subscriptions No $0 Emails
Plugin URI:
Description: Do not send a processing or completed renewal order emails to customers when the order or renewal is for $0.00.
Author:
Author URI:
Version: 0.1
*/

function eg_maybe_remove_email( $order_id ){

    $order = new WC_Order( $order_id );

    if ( 0 == $order->get_total() ) {

        $email_class = array();
        
        switch( current_filter() ) {
            case 'woocommerce_order_status_completed_renewal_notification':
                $email_class[] = 'WCS_Email_Completed_Renewal_Order';
                break;
            case 'woocommerce_order_status_pending_to_processing_renewal_notification':
                $email_class[] = 'WCS_Email_Processing_Renewal_Order';
                $email_class[] = 'WCS_Email_New_Renewal_Order';
                break;
            case 'woocommerce_order_status_failed_renewal_notification':
                $email_class[] = 'WCS_Email_Customer_Renewal_Invoice';
                $email_class[] = 'WCS_Email_New_Renewal_Order';
                break;
            case 'woocommerce_order_status_pending_to_completed_renewal_notification':
            case 'woocommerce_order_status_pending_to_on-hold_renewal_notification':
            case 'woocommerce_order_status_failed_to_processing_renewal_notification':
            case 'woocommerce_order_status_failed_to_completed_renewal_notification':
            case 'woocommerce_order_status_failed_to_on-hold_renewal_notification':
            case 'woocommerce_order_status_cancelled_to_processing_renewal_notification':
            case 'woocommerce_order_status_cancelled_to_completed_renewal_notification':
            case 'woocommerce_order_status_cancelled_to_on-hold_renewal_notification':
                $email_class[] = 'WCS_Email_New_Renewal_Order';
                break;
            case 'woocommerce_subscriptions_switch_completed_switch_notification':
                $email_class[] = 'WCS_Email_New_Switch_Order';
                break;
            default:
                $email_class[] = array();
                break;
        }

        if ( ! empty( $email_class ) ) {
            foreach ( $email_class  as $key => $email ) {
                remove_action( current_filter(), array( WC()->mailer()->emails[ $email ], 'trigger' ) );
            }
        }

    }
    
}

//customer
add_action( 'woocommerce_order_status_completed_renewal_notification',             'eg_maybe_remove_email', 0, 1 );
add_action( 'woocommerce_order_status_pending_to_processing_renewal_notification', 'eg_maybe_remove_email', 0, 1 );
add_action( 'woocommerce_order_status_failed_renewal_notification',                'eg_maybe_remove_email', 0, 1 );

//admin
add_action( 'woocommerce_order_status_pending_to_completed_renewal_notification',    'eg_maybe_remove_email', 0, 1  );
add_action( 'woocommerce_order_status_pending_to_on-hold_renewal_notification',      'eg_maybe_remove_email', 0, 1  );
add_action( 'woocommerce_order_status_failed_to_processing_renewal_notification',    'eg_maybe_remove_email', 0, 1  );
add_action( 'woocommerce_order_status_failed_to_completed_renewal_notification',     'eg_maybe_remove_email', 0, 1  );
add_action( 'woocommerce_order_status_failed_to_on-hold_renewal_notification',       'eg_maybe_remove_email', 0, 1  );
add_action( 'woocommerce_order_status_cancelled_to_processing_renewal_notification', 'eg_maybe_remove_email', 0, 1  );
add_action( 'woocommerce_order_status_cancelled_to_completed_renewal_notification',  'eg_maybe_remove_email', 0, 1  );
add_action( 'woocommerce_order_status_cancelled_to_on-hold_renewal_notification',    'eg_maybe_remove_email', 0, 1  );
add_action( 'woocommerce_subscriptions_switch_completed_switch_notification',        'eg_maybe_remove_email', 0, 1  );