0

there are around 2000 completed orders in woocommerce, and our customer didn't want to send invoices to them on completing the order, so I disabled invoices, but now after a few months he wants to send all those invoices to customers, and sadly woocommerce doesnt have an option to do it in bulk.

I've turned to chat GPT and it provided me with this code to resend invoices, can someone confirm or deny will this work or no, or if there is a better way to do this

<?php
// Make sure you have the necessary WooCommerce and WordPress files included/loaded.

// Resend invoices to all past orders
function resendInvoicesToPastOrders() {
    global $wpdb;

    // Get all completed orders
    $orders = wc_get_orders(array(
        'status' => 'completed',
        'limit' => -1,
    ));

    // Loop through each order and resend the invoice
    foreach ($orders as $order) {
        // Retrieve the order ID
        $order_id = $order->get_id();

        // Get the billing email of the order
        $billing_email = $order->get_billing_email();

        // Generate the invoice resend URL
        $invoice_resend_url = wc_get_endpoint_url('view-order', $order_id, wc_get_page_permalink('myaccount'));

        // Send the invoice email
        $subject = 'Resend Invoice - Order #' . $order_id;
        $message = 'Please find the invoice attached.';
        $attachments = array(WC()->mailer()->wrap_message_attachment($invoice_resend_url, 'invoice.html'));
        wp_mail($billing_email, $subject, $message, array(), $attachments);
    }
}

// Run the function to resend invoices to all past orders
resendInvoicesToPastOrders();
?>

1 Answers1

0

EDIT: the problem has been solved with the code from this post