0

enter image description here

Hello How are you? I dont know how to bulk this action "email invoice /order details to customer" sometimes i have 70 orders that I need to send the email, and i have to do it one by one, i found some codes here but for other actions and i dont know how i can add that one, thanks a lot for the help!

function write_to_file($date_initial, $date_final) {
    global $attach_download_dir, $attach_download_file;

    // Opens/creates file
    $myfile = fopen($attach_download_dir . '/' . $attach_download_file, "w") or die("Unable to open file!");

    // Populates first line
    fwrite($myfile, 'Date; Parent Order ID; Order ID' . PHP_EOL);

    // Retrieves orders data
    if ( isset($date_initial) && isset($date_final) ) $args = array( 'date_created' => $date_initial . '...' . $date_final );
    if ( isset($date_initial) && empty($date_final) ) $args = array( 'date_created' => '>=' . $date_initial );
    if ( empty($date_initial) && isset($date_final) ) $args = array( 'date_created' => '<=' . $date_final );
    if ( empty($date_initial) && empty($date_final) ) $args = array( );
    $orders = wc_get_orders( $args );

    // Populates file with orders data
    foreach ($orders as $order) {
        $order_data = $order->get_data();
        fwrite($myfile,
            // Date of order creation
            $order_data['date_created']->date('d/M/Y') . '; ' .

            // Parent Order ID
            '#' . ( ( $order->get_type() === 'shop_order' ) ? $order->get_id() : $order->get_parent_id() ) . '; ' .

            // Order ID
            '#' . $order->get_id()
        )
    }
}

1 Answers1

2

I was looking for how to bulk email woocommerce order emails to customers, and came across this post. I tried asking a similar question - but thanks to 7uc1f3r for kicking my butt to do more work and find this answer myself.

With a bit of perseverance I figured it out by cobbling together a few bits and pieces from various posts.

I started with this post and used the email trigger code from this post.

I've tested this successfully on my production WooCommerce store.

The code below will add a new Bulk action to the WooCommerce -> Orders page called "Email Invoice / Order Details to Customers".

Select all the orders you want to receive the email, and choose this option. It will cycle through and send the WooCommerce template for Invoice/Order Email to each customer for each selected order. It gives a status update of how many emails were sent.

To use it, copy this code into your Wordpress theme's functions.php file:

/* Add to admin order list bulk dropdown a custom action 'Email Invoice / Order Details to Customers' */
add_filter( 'bulk_actions-edit-shop_order', 'email_invoice_bulk_action_orders_list', 20, 1 );
function email_invoice_bulk_action_orders_list( $actions ) {
    $actions['send_emails'] = __( 'Email Invoice / Order Details to Customers', 'woocommerce' );
    return $actions;
}

// Make the action from selected orders
add_filter( 'handle_bulk_actions-edit-shop_order', 'email_invoice_handle_bulk_action_orders_list', 10, 3 );
function email_invoice_handle_bulk_action_orders_list( $redirect_to, $action, $post_ids ) {
    if ( $action !== 'send_emails' )
        return $redirect_to; // Exit

    $processed_ids = array();

    foreach ( $post_ids as $orderid ) {
        // Send customer order email
        WC()->mailer()->emails['WC_Email_Customer_Invoice']->trigger($orderid);

        // update count
        $processed_ids[] = $post_id;
    }

    return $redirect_to = add_query_arg( array(
        'send_emails' => '1',
        'processed_count' => count( $processed_ids ),
        'processed_ids' => implode( ',', $processed_ids ),
    ), $redirect_to );
}

// The results notice from bulk action on orders
add_action( 'admin_notices', 'email_invoice_bulk_action_admin_notice' );
function email_invoice_bulk_action_admin_notice() {
    if ( empty( $_REQUEST['send_emails'] ) ) return; // Exit

    $count = intval( $_REQUEST['processed_count'] );

    printf( '<div id="message" class="updated fade"><p>' .
        _n( 'Sent %s Order Invoice Email.',
        'Sent %s Order Invoice Emails.',
        $count,
        'send_emails'
    ) . '</p></div>', $count );
}

Please test on a staging site first - just in case!

I hope this helps.

Israel.

Israel
  • 21
  • 4