Something is changing the status of orders in my woocommerce store from processing to pending. I want to log what it is to the file. I have such a code:
function log_woocommerce_order_status_change($order_id, $old_status, $new_status) {
$current_datetime = date('Y-m-d H:i:s');
$call_stack = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 5);
$plugin_info = 'Plugin info not available';
foreach ($call_stack as $trace) {
if (isset($trace['file']) && strpos($trace['file'], 'plugins/') !== false) {
$plugin_info = 'Plugin file: ' . $trace['file'] . ', Line: ' . $trace['line'];
break;
}
}
$log_message = "[$current_datetime] woocommerce_order_status_changed hook called for order ID $order_id. Old status: $old_status, New status: $new_status. Triggered by: $plugin_info";
error_log($log_message . PHP_EOL, 3, WP_CONTENT_DIR . '/woocommerce_order_status.log', FILE_APPEND);
}
add_action('woocommerce_order_status_changed', 'log_woocommerce_order_status_change', 10, 3);
But every time the status is changed it gives me the same result:
woocommerce_order_status_changed hook called for order ID 13232. Old status: processing, New status: pending. Triggered by: Plugin file: /home/maisonpa/domains/my-theme/public_html/wp-content/plugins/woocommerce/includes/class-wc-order.php, Line: 405
Is it possible to get the name of the plugin / file that directly calls the function “woocommerce_order_status_changed” and not the file in which it is stored?