Created
May 14, 2025 06:05
-
-
Save mehrshaddarzi/d21a923b17f5c81767090de5bcbb8c99 to your computer and use it in GitHub Desktop.
Delete Order Note For completed WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Setup CronJob | |
//add_action('plugins_loaded', 'add_custom_wc_cron_job_note_completed', 70); | |
function add_custom_wc_cron_job_note_completed() | |
{ | |
if (!wp_next_scheduled('delete_order_notes_completed')) { | |
wp_schedule_event(time(), 'daily', 'delete_order_notes_completed'); | |
} | |
} | |
// Delete Order Notes For Completed Orders | |
//add_action('delete_order_notes_completed', 'cronjob_delete_order_notes_completed'); | |
function cronjob_delete_order_notes_completed() { | |
global $wpdb; | |
return; | |
// @see https://www.sql-easy.com/learn/how-to-get-yesterday-s-date-in-mysql/ | |
$today = time(); | |
$dayBefore60 = strtotime('-60 day', $today); | |
$dayBefore30 = strtotime('-30 day', $today); | |
// Delete Comment | |
$sql = "DELETE c, cm | |
FROM {$wpdb->prefix}comments c | |
INNER | |
JOIN {$wpdb->prefix}commentmeta cm | |
ON cm.comment_id = c.comment_ID | |
WHERE c.comment_post_ID IN (SELECT post_id FROM `{$wpdb->prefix}postmeta` WHERE `meta_key` = '_date_completed' AND `meta_value` BETWEEN '{$dayBefore60}' AND '{$dayBefore30}');"; | |
$query = $wpdb->query($sql); | |
//$wpdb->rows_affected; | |
// Update Comment Count | |
$sql = "UPDATE `{$wpdb->prefix}posts` SET `comment_count` = '0' WHERE `post_type` = 'shop_order' AND `ID` IN (SELECT post_id FROM `{$wpdb->prefix}postmeta` WHERE `meta_key` = '_date_completed' AND `meta_value` BETWEEN '{$dayBefore60}' AND '{$dayBefore30}');"; | |
$query = $wpdb->query($sql); | |
//$wpdb->rows_affected; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment