Search code examples
phpwordpresswoocommercehook-woocommerceemail-notifications

Send customized new order notification only for pending order status and specific payment methods


I am trying to send notification about new order only for "pending" order status and not "cash on delivery" payment method. But admin have receive duplicated mails when client choose cash on delivery payment, because Woocommerce update this order status from "pending" to "processed".

// New order notification only for "pending" order status and not "cash on delivery" payment method

add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 10, 1 );
function pending_new_order_notification( $order_id ) {
    // Get an instance of the WC_Order object
    $order = wc_get_order( $order_id );
    $payment_title = $order->get_payment_method_title();

    // Only for "pending" order status and not Cash on delivery payment method 
    if( ! $order->has_status( 'pending' )  && ( $payment_title != 'cod' ) ) return;

    // Get an instance of the WC_Email_New_Order object
    $wc_email = WC()->mailer()->get_emails()['WC_Email_New_Order'];

    ## -- Customizing Heading, subject (and optionally add recipients)  -- ##
    // Change Subject
    $wc_email->settings['subject'] = __('{site_title} - Новый заказ ({order_number}) - {order_date} ожидает оплату');
    // Change Heading
    $wc_email->settings['heading'] = __('Новый заказ'); 
    $wc_email->settings['recipient'] .= ',name@email.com'; // Add email recipients (coma separated)

    // Send "New Email" notification (to admin)
     $wc_email->trigger( $order_id );
}

Solution

  • There are some mistakes and missing things in your code, so try the following instead:

    // Send email
    add_action( 'woocommerce_checkout_order_processed', 'pending_custom_new_order_notification', 20, 3 );
    function pending_custom_new_order_notification( $order_id, $posted_data, $order ) {
        // Only for "pending" order status and not Cash on delivery payment method 
        if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
            // Send "New Order" email
            $wc_email = WC()->mailer()->get_emails()['WC_Email_New_Order']->trigger( $order_id );
        }
    }
    
    // Custom email subject
    add_filter( 'woocommerce_email_subject_new_order', 'custom_new_order_email_subject', 20, 2 );
    function custom_new_order_email_subject( $formated_subject, $order ){
        // Only for "pending" order status and not Cash on delivery payment method 
        if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
            $formated_subject = sprintf( __('%s - Новый заказ (%s) - %s ожидает оплату', 'woocommerce'), 
                get_bloginfo( 'name' ),
                $order->get_order_number(), // Order ID (or the order number)
                $order->get_date_modified()->date_i18n('F j, Y') // Formatted date modified
            );
        }
        return $formated_subject;
    }
    
    // Custom email heading
    add_filter( 'woocommerce_email_heading_new_order', 'custom_new_order_email_heading', 20, 2 );
    function custom_new_order_email_heading( $heading_txt, $order ){
        // Only for "pending" order status and not Cash on delivery payment method 
        if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
            $heading_txt = __('Новый заказ', 'woocommerce');
        }
        return $heading_txt;
    }
    
    // Custom email recipient
    add_filter( 'woocommerce_email_recipient_new_order', 'custom_new_order_email_recipient', 20, 2 );
    function custom_new_order_email_recipient( $recipient, $order ){
        // Only for "pending" order status and not Cash on delivery payment method 
        if( $order->has_status( 'pending' ) && 'cod' !== $order->get_payment_method() ) {
            $recipient .= ',name@email.com';
        }
        return $recipient;
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works.

    Since WooCommerce 5+: Allow re-sending New Order Notification in WooCommerce 5+