Home / Archive / MemberPress: Generate All Invoices for the Specific Period (e.g. A Year)
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Generate All Invoices for the Specific Period (e.g. A Year)

If the MemberPress PDF Invoice add-on is activated on the website, MemberPress will start generating invoices for all MemberPress-related transactions. Invoices won’t be generated for any transactions prior to the initial add-on activations or in any periods when the PDF Invoice add-on is not active.

This code snippet will bulk create invoices for all transactions created in certain period and store the invoices as PDF files in the wp-content/uploads/mepr/mpdf/ folder. The example code generates all invoices for transactions created in year 2025.

Once the code snippet is active on the website, the invoices are generated by visiting the generation link. The generation link is created by adding /wp-admin/?generate-invoices to the website URL (e.g. https://yourdomain.com/wp-admin/?generate-invoices).

To download the generated PDF files, access the website folder with the wp-content/uploads/mepr/mpdf/ path, using cPanel or via FTP client.

Modify the start and end dates and times on the following line:

To modify the period this code snippet applies to, replace the start date of 2025-01-01 00:00:00 with the starting date and time of the desired custom period, on this line:

$query = "SELECT id FROM {$wpdb->prefix}mepr_transactions WHERE created_at > '2025-01-01 00:00:00' AND created_at < '2025-12-31 23:59:59' AND status IN ('complete', 'confirmed', 'refunded')";

Next, replace the end date of 2025-12-31 23:59:59 with the desired end date and time.

The format of the date and time must remain the same for both the start and end dates.

The process needs to be repeated for each time period. Thus, for example, the dates and times should be adjusted for the first period, followed by visiting the generation link. These files should be then downloaded before the next generation round, as they will be overridden. Next, the code can be modified for the second period, and the whole process can be repeated.

Code Preview
php
<?php
function generate_bulk_invoices() {
  if(isset($_REQUEST['generate-invoices'])) {
    global $wpdb;
    $query = "SELECT id FROM {$wpdb->prefix}mepr_transactions WHERE created_at > '2025-01-01 00:00:00' AND created_at < '2025-12-31 23:59:59' AND status IN ('complete', 'confirmed', 'refunded')";
    $txn_ids = $wpdb->get_results($query);
    foreach($txn_ids as $txn_id) {
      $invoices = new MePdfInvoicesCtrl();
      $txn = new MeprTransaction($txn_id);
      $path = $invoices->create_receipt_pdf($txn);
    }
    echo('<p>Invoices generated.</p>');
    die();
  }
}
add_action('admin_init', 'generate_bulk_invoices');

Comments

Add a Comment