Last active
October 30, 2023 20:49
-
-
Save adam-sandor/c1fc0916923c0e8a23016028f2aeb6ef to your computer and use it in GitHub Desktop.
checkout-logging.php
This file contains 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 | |
$PRIORITY=-1; | |
/** | |
* Log reaching the thank-you page with the order number | |
*/ | |
add_action( 'woocommerce_thankyou', function($order_id) { | |
printLogMessage("Thank you page reached (order_id: " . $order_id . ")"); | |
}, $PRIORITY, 1 ); | |
if (!function_exists('str_ends_with')) { | |
function str_ends_with($haystack, $needle) { | |
return substr($haystack, -strlen($needle)) === $needle; | |
} | |
} | |
/** | |
* Log all incoming HTTP requests (unf we don't know the result code at this point) | |
*/ | |
add_filter( 'parse_request', function( $query ) { | |
$uri = $_SERVER['REQUEST_URI']; | |
if (strpos($uri, "checkout") !== false) { | |
printLogMessage("ACCESS: $uri"); | |
} | |
}, $PRIORITY, 1 ); | |
/** | |
* Log all HTTP API requests made by the backend to other services like Paypal | |
*/ | |
add_filter( 'http_response', function( $response, $args, $url ) { | |
if (strpos($url, "facebook") !== false || strpos($url, "mailchimp") !== false || | |
strpos($url, "translate.wordpress.com") !== false || strpos($url, "updates.wpbakery.com") !== false | |
|| strpos($url, "upgrade") !== false || strpos($url, "update") !== false | |
|| strpos($url, "api.wordpress.org") !== false) { | |
return $response; | |
} | |
$msg = "API Response " . $response['response']['code'] . " [url=" . $url . ", method=" . $args['method'] . ", response_code=" . $response['response']['code'] . "]"; | |
printLogMessage($msg); | |
return $response; | |
}, $PRIORITY, 3 ); | |
add_action( 'woocommerce_checkout_process', function() { | |
printLogMessage('woocommerce_checkout_process action'); | |
}, $PRIORITY); | |
add_action( 'woocommerce_after_checkout_validation', function($data, $errors) { | |
printLogMessage('woocommerce_after_checkout_validation action'); | |
}, $PRIORITY, 2); | |
add_filter( 'woocommerce_add_error', function($message) { | |
printLogMessage("woocommerce_add_error (message: " . print_r($message, true) . ")"); | |
return $message; | |
}, $PRIORITY, 1); | |
add_action( 'woocommerce_checkout_order_processed', function($order_id, $posted_data, $order) { | |
printLogMessage("woocommerce_checkout_order_processed action (order_id: $order_id)"); | |
}, $PRIORITY, 3); | |
add_filter( 'woocommerce_payment_successful_result', function($result, $order_id) { | |
printLogMessage("woocommerce_payment_successful_result filter (order_id: $order_id)"); | |
return $result; | |
}, $PRIORITY, 2); | |
function printLogMessage($msg) { | |
if (!function_exists('wc_get_logger') || !function_exists('wp_get_current_user')) return; | |
$logger = wc_get_logger(); | |
try { | |
$user = wp_get_current_user(); | |
$user_id = $user->ID; | |
if ($user_id != 0) { | |
$logger->info("$user_id : $msg", | |
array('source' => 'user-tracking')); | |
} | |
} catch (Exception $e) { | |
$logger->error("Error during user tracking: " . $e->getMessage(), array('source' => 'user-tracking')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment