skip to Main Content

I have searched the web and checked the WooCommerce docs for a solution to disable the "confirmation email" that is sent to the customer when they place an order in WooCoomerce.

I also want to disable the "new order" mail that goes to the admin.

But only if the order has a custom status "mystatus", which some orders are getting based on what the customers are ordering.

Tried adding it like this, but it did not work:

remove_action( 'woocommerce_order_status_mystatus_notification', array($email_class->emails['WC_Email_New_Order'], 'trigger' ) );?>

Any advice?


This is how I change the order status for specific orders:

add_action( 'woocommerce_thankyou','woocommerce_thankyou_change_order_status', 10, 1 );
function woocommerce_thankyou_change_order_status( $order_id ){
if( ! $order_id ) return;

$order = wc_get_order( $order_id );
$user_id = $order->get_user_id();

if( ($order->get_status() == 'processing'  || $order->get_status() == 'pending' || $order->get_status() == 'on-hold' ) && dokan_is_user_seller( $user_id ) && is_user_logged_in()) {
    $order->update_status( 'mystatus' );
  }
}

2

Answers


  1. Disable the mail under WooCommerce >> Settings >> Emails you want to only sent in case your order has a custom status.

    Now just sent it in case your order has the correct status:

    add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_change_order_status', 10, 1 );
    function woocommerce_thankyou_change_order_status( $order_id ) {
        if ( ! $order_id ) {
            return;
        }
    
        $order   = wc_get_order( $order_id );
        $user_id = $order->get_user_id();
    
        if ( ( $order->get_status() == 'processing' || $order->get_status() == 'pending' || $order->get_status() == 'on-hold' ) && dokan_is_user_seller( $user_id ) && is_user_logged_in() ) {
            $order->update_status( 'mystatus' );
    
            $email_oc = new WC_Email_Customer_Completed_Order();
            $email_oc->trigger($order_id);
        }
    }
    

    You can just sent every WooCommerce mail from PHP you want.

    Login or Signup to reply.
  2. No real need to make any changes to WooCommerce settings.

    No email-notifications will be sent for a custom order status, unless you effectively provide this.

    However, the default email-notifications are sent, because the woocommerce_thankyou hook is executed after the email-notifications have been sent.

    So use the woocommerce_checkout_order_created hook (that is executed, before the e-mail notifications are sent) opposite woocommerce_thankyou to change the order status, and no emails will be sent anyway.

    function action_woocommerce_checkout_order_created( $order ) {
        // Get user ID
        $user_id = $order->get_user_id();
        
        // Compare
        if ( ( $order->get_status() == 'processing'  || $order->get_status() == 'pending' || $order->get_status() == 'on-hold' ) && dokan_is_user_seller( $user_id ) && is_user_logged_in() ) {
            // Update status
            $order->update_status( 'mystatus' );
        }
    }
    add_action( 'woocommerce_checkout_order_created', 'action_woocommerce_checkout_order_created', 10, 1 );
    

    Note: If you want to disable emails in any other case, you can use the woocommerce_email_recipient_{$email_id} filter composite hook and with the right email ID set, you have the option to disable email notifications.

    For instance:

    // Admin - new order email notification
    // Customer - on hold
    // Customer - processing
    // Customer - pending
    function filter_woocommerce_email_recipient( $recipient, $order, $email ) { 
        if ( ! $order || ! is_a( $order, 'WC_Order' ) ) return $recipient;
        
        // Has order status
        if ( $order->has_status( 'your-order-status' ) ) {
            $recipient = '';
        }
    
        return $recipient;
    }
    add_filter( 'woocommerce_email_recipient_new_order', 'filter_woocommerce_email_recipient', 10, 3 );
    add_filter( 'woocommerce_email_recipient_customer_on_hold_order', 'filter_woocommerce_email_recipient', 10, 3 );
    add_filter( 'woocommerce_email_recipient_customer_processing_order', 'filter_woocommerce_email_recipient', 10, 3 );
    add_filter( 'woocommerce_email_recipient_customer_pending_order', 'filter_woocommerce_email_recipient', 10, 3 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search