Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/*
// Restrict who can access this script.
$permitted_ips = array('12.34.56.78', '87.65.43.21');
if (in_array($_SERVER['REMOTE_ADDR'], $permitted_ips) == false) {
header('HTTP/1.0 403 Forbidden');
die();
}
*/
define('WP_USE_THEMES', false);
/** Loads the WordPress Environment */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
// Return text/plain content when running as a cron job - or ensure no output is generated.
//header("Content-Type: text/plain");
$disable_output = false; // Set to true when running as cron job.
// Get 100 oldest order ids in date ascending order.
$query = new WC_Order_Query( array(
'limit' => 100,
'orderby' => 'date',
'order' => 'ASC',
'return' => 'ids',
'status' => 'completed',
//'date_created' => '<' . ( time() - MONTH_IN_SECONDS ), // Or multiple MONTH_IN_SECONDS ago or YEAR_IN_SECONDS ago
'date_created' => '<2020-09-30, // Orders created before that date.
) );
$orders = $query->get_orders();
if ( ! $disable_output ) {
echo '<p>Showing 100 oldest orders orders before ', date('Y-m-d', time() - MONTH_IN_SECONDS ), '</p>';
echo '<table>';
echo '<tr><th>Order ID</th><th>Date</th><th>Total</th></tr>';
foreach ( $orders as $order_id ) {
$order = wc_get_order( $order_id );
//echo "<p>ID: $order_id - ", $order->get_status(), "</p>\n";
if ( 'refunded' != $order->get_status() ) {
$d = new DateTime( $order->get_date_created() );
printf( '<tr><td>%d</td><td>%s</td><td>%s</td></tr>%s', $order_id, $d->format('Y-m-d'), $order->get_formatted_order_total(), "\n" );
}
}
echo '</table>';
}
// Delete the orders.
foreach ( $orders as $order_id ) {
//$res = wp_delete_post( $order_id, true );
//if ( ! $res ) {
// echo "<p>ERROR: There was a problem deleting order $order_id.</p>";
//}
}
// This info is just for fun.
if ( ! $disable_output ) {
echo '<p>', get_num_queries (), ' SQL queries done.';
echo '<br />Page generation took ', timer_stop(), ' seconds.', '</p>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment