Skip to content

Instantly share code, notes, and snippets.

@dancameron
Created May 10, 2017 21:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dancameron/dcf6882b8aa4bd64aed967afde07b5d7 to your computer and use it in GitHub Desktop.
Save dancameron/dcf6882b8aa4bd64aed967afde07b5d7 to your computer and use it in GitHub Desktop.
Mass Invoices Updater, Example updates GST and Tax
<?php // don't include this in your functions.php, since the start of the file should already have it
function _maybe_update_invoice_line_items_with_gst( $invoice_id ) {
$invoice = SI_Invoice::get_instance( $invoice_id );
if ( ! is_a( $invoice, 'SI_Invoice' ) ) {
return false;
}
$line_items = $invoice->get_line_items();
foreach ( $line_items as $position => $data ) {
$line_items[ $position ]['tax_gst'] = 5;
$line_items[ $position ]['tax'] = 0;
if ( 0 === (int) $data['rate'] && 0 === (int) $data['qty'] ) {
unset( $line_items[ $position ] );
}
}
$invoice->set_line_items( $line_items );
return true;
}
function run_si_gst_updater( $offset = 0 ) {
$args = array(
'post_type' => 'sa_invoice',
'post_status' => 'any',
'posts_per_page' => 20,
'fields' => 'ids',
'offset' => $offset,
);
$post_ids = get_posts( $args );
if ( empty( $post_ids ) ) {
wp_die( 'importer possibly done.' );
exit();
}
foreach ( $post_ids as $post_id ) {
error_log( 'post_id: ' . print_r( $post_id, true ) );
_maybe_update_invoice_line_items_with_gst( $post_id );
}
$new_offset = $offset + 20;
wp_redirect( add_query_arg( array( 'gst_updater' => true, 'offset' => $new_offset ) ) );
exit();
}
// go to url http://yoursite.com/wp-admin/admin.php?gst_updater=1
if ( is_admin() && current_user_can( 'edit_posts' ) ) {
if ( isset( $_GET['gst_updater'] ) && $_GET['gst_updater'] ) {
$page = ( isset( $_GET['offset'] ) ) ? $_GET['offset'] : 0 ;
run_si_gst_updater( $page );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment