// Allow customers to cancel orders after payment
add_filter('woocommerce_valid_order_statuses_for_cancel', 'custom_valid_order_statuses_for_cancel', 10, 2);
function custom_valid_order_statuses_for_cancel($statuses, $order) {
// Set the statuses that you allow customers to cancel
$custom_statuses = array('completed', 'pending', 'processing', 'on-hold', 'failed');
// Set the number of days to be allowed to cancel since placing an order
$duration = 1; // Number of days
// Get Order ID and WC_Order
if (isset($_GET['order_id'])) {
$order = wc_get_order(absint($_GET['order_id']));
}
$delay = $duration * 24 * 60 * 60; // Duration in seconds
$date_created_time = strtotime($order->get_date_created()); // Creation date timestamp
$date_modified_time = strtotime($order->get_date_modified()); // Modified date timestamp
$now = strtotime("now"); // Current timestamp
// Using Creation date timestamp
if (($date_created_time + $delay) >= $now) {
return $custom_statuses;
} else {
return $statuses;
}
}