Skip to content

Instantly share code, notes, and snippets.

@WillBrubaker
Last active September 30, 2022 20:32
Show Gist options
  • Save WillBrubaker/7dd8182dab1e3daeff8d2bc16aa05df9 to your computer and use it in GitHub Desktop.
Save WillBrubaker/7dd8182dab1e3daeff8d2bc16aa05df9 to your computer and use it in GitHub Desktop.
Dynamically Change WooCommerce Order Date on Status Change
add_action( 'woocommerce_loaded', 'handsome_bearded_guy_wc_loaded' );
function handsome_bearded_guy_wc_loaded() {
$old_statuses = array(
'failed',
//uncomment any of the below statuses to include those statuses
//'pending',
//'processing',
//'on-hold',
//'cancelled',
//'refunded'
);
foreach ( $old_statuses as $old_status ) {
add_action( 'woocommerce_order_status_' . $old_status . '_to_completed', 'handsome_bearded_guy_change_posted_date', 99, 1 );
}
}
function handsome_bearded_guy_change_posted_date( $order_id ) {
$order = wc_get_order( $order_id );
$args = array(
'post_id' => $order_id,
//wp_insert_post (called by wp_update_post) will set the date to "now" if `post_date` is empty, likewise with `post_date_gmt`
'post_date' => '',
'post_date_gmt' => '',
);
wp_update_post( $args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment