Avoid repetitive emails notification on some auto

2019-02-19 12:40发布

问题:

I'm using this little peace of code on WooCommerce from this answer to auto-complete paid processing orders based on payment gateways:

/**
 * AUTO COMPLETE PAID ORDERS IN WOOCOMMERCE
 */
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1 );
function custom_woocommerce_auto_complete_paid_order( $order_id ) {
    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );

  // No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
  if ( ( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) || ( get_post_meta($order->id, '_payment_method', true) == 'cod' ) || ( get_post_meta($order->id, '_payment_method', true) == 'cheque' ) ) {
    return;
    } 
  // "completed" updated status for paid Orders with all others payment methods
    else {
        $order->update_status( 'completed' );
    }
} 

This is working mostly perfect

Mainly using a special payment gateway by SMS which API is bridged on 'cod' payment method and that can process payment after 'woocommerce_thankyou, out side frontend. In that case the ON HOLD status orders are passed afterward to PROCESSING status. To automate an autocomplete behavior on those cases, I use this other peace of code from this answer and it works:

function auto_update_orders_status_from_processing_to_completed(){
    // Get all current "processing" customer orders
    $processing_orders = wc_get_orders( $args = array(
        'numberposts' => -1,
        'post_status' => 'wc-processing',
    ) );
    if(!empty($processing_orders))
        foreach($processing_orders as $order)
            $order->update_status( 'completed' );
}
add_action( 'init', 'auto_update_orders_status_from_processing_to_completed' );

THE PROBLEM: I am getting repetitive emails notifications concerning the new completed orders.

How can I avoid this repetitive email notifications cases?

Thanks

回答1:

To avoid this strange fact of repetitive email notifications, is possible to create a custom meta key/value for each processed order, when changing order status to completed, using WordPress update_post_meta() function. Then we will test before in a condition, if this custom meta data key/value exist with get_post_meta() function for each processed order.

So your two code snippets will be now:

1) AUTO COMPLETE PAID ORDERS IN WOOCOMMERCE (Based on payment methods)

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 20, 1 );
function custom_woocommerce_auto_complete_paid_order( $order_id ) {
    if ( ! $order_id )
        return;

    // Getting the $order object from the ID
    $order = wc_get_order( $order_id );

    // Getting the custom meta value regarding this autocomplete status process
    $order_processed = get_post_meta($order->id, '_order_processed', true);

    // No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
    if ( ( get_post_meta($order->id, '_payment_method', true) == 'bacs' ) || ( get_post_meta($order->id, '_payment_method', true) == 'cod' ) || ( get_post_meta($order->id, '_payment_method', true) == 'cheque' ) ) {
        return;
    }
    else
    {
        // We check if order has a status different than completed AND if the custom meta value hasen't been already set
        if (!$order->has_status( 'completed' ) && $order_processed != 'yes'){
            // setting the custom meta data value to yes (order updated)
            update_post_meta($order->id, '_order_processed', 'yes');
            $order->update_status( 'completed' ); // Update order status to completed
        } else {
            return;
        }
    }
}

2) SCAN ALL "processing" orders status (to auto-complete them).

add_action( 'init', 'auto_update_orders_status_from_processing_to_completed' );
function auto_update_orders_status_from_processing_to_completed(){

    // Get all current "processing" customer orders
    $processing_orders = wc_get_orders( array(
        'numberposts' => -1,
        'post_status' => 'wc-processing',
    ) );

    if(!empty($processing_orders)){
        foreach($processing_orders as $order) {
            // Checking if this custom field value is set in the order meta data
            $order_processed = get_post_meta($order->id, '_order_processed', true);
            if (!$order->has_status( 'completed' ) && $order_processed != 'yes') {
                // Setting (updating) custom meta value in the order metadata to avoid repetitions
                update_post_meta($order->id, '_order_processed', 'yes');
                $order->update_status( 'completed' ); // Updating order status
            }
        }
    }
}

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.

I have test this code and it seem working. But as I haven't the same payment settings due to your particular SMS bridged payment method, I can't be completely sure. I hope this will work.