Home / Admin / MemberPress: Generate All Invoices
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Generate All Invoices

If the MemberPress PDF Invoice add-on is activated on the website, MemberPress will start generating invoices for all MemberPress-related transactions.

This code snippet will bulk create invoices for all transactions starting from the first transaction ever recorded in MemberPress, and store the invoices as PDF files in the wp-content/uploads/mepr/mpdf/ folder.

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.

Code Preview
php
<?php
function generate_bulk_invoices() {
  if( isset( $_REQUEST[ 'generate-invoices' ] ) ) {
    global $wpdb;
    $query = "SELECT id FROM {$wpdb->prefix}mepr_transactions WHERE 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->id );
      if ( $txn->id > 0 ) {
        $path = $invoices->create_receipt_pdf( $txn );
	  }
    }
    echo( '<p>Invoices generated.</p>' );
    die();
  }
}
add_action( 'admin_init', 'generate_bulk_invoices' );

Comments

Add a Comment