How to debug in WooCommerce 3+
As this is a background process on server side, don't use javascript.
1). WC Logs and the WC_Logger
Class in WooCommerce for better debugging
To access the results of the log easily from the dashboard, you can log to a WC logger rather than the error log.
You can access error logs by going to WooCommerce > System Status > Logs.
Then you will be able to choose and "view"the error log file you need, giving you the debugging details that you need. Error logs are also located in the /wc-logs folder within your site install.
Running a stack trace on a caught exception (example):
// Log any exceptions to a WC logger
$log = new WC_Logger();
$log_entry = print_r( $e, true );
$log_entry .= 'Exception Trace: ' . print_r( $e->getTraceAsString(), true );
$log->log( 'new-woocommerce-log-name', $log_entry );
Notes:
WC_Logger
methods have been updated since WooCommerce 3: So logging can be grouped by context and severity.Use
WC_Logger
log()
method instead ofadd()
method due to upcoming deprecation (thanks to @Vizz85).
For example:
$logger = wc_get_logger();
$logger->debug( 'debug message', array( 'source' => 'my-extension' ) );
Related:
- Develop WooCommerce blog (january 2017): Improved logging in WooCommerce 3
- Documentation on the
WC_Logger
available methods
2). Debugging with WordPress WP_DEBUG
Log (as an alternative)
a) First edit your wp-config.php
file adding the following lines to enable debug (if these are already defined, edit the values):
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
As errors are logged, they should appear in wp-content/debug.log
. You can open this file in a text editor.
b) On your code: Use the following (where $variable
is the variable to be displayed in the error log:
error_log( print_r( $variable, true ) );
Now you will get the data for debugging.
How to debug cart content in WooCommerce?
To add custom code to your site you first need to create a child theme. Then, you will need to insert the custom code inside the functions.php file of your active (child) theme.
If it's a staging/debug site you can use the woocommerce_before_cart
hook to print the contents of the variables. Another check that you could add is to check if the current user is an administrator, so as not to see the data to other users of the site.
So it will be something like this:
add_action( 'woocommerce_before_cart', 'wc_cart_debug' );
function wc_cart_debug( $cart ) {
if ( ! current_user_can('administrator') ) {
return;
}
// Your code here.
}
RELATED ANSWERS
- How to debug in WooCommerce 3
- PHP and WordPress: Debugging
- How to debug php code while developing wordpress plugins?
- Debugging in PHP and Wordpress
- Which is the best way to debug PHP in WordPress?
How to check the value of a WooCommerce filter hook parameter?
You can first enable Wordpress debug log by editing your wp-config.php
file adding the following lines to enable debug (if these are already defined, edit the values):
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
Now you can use error_log()
function in your code as follows:
add_filter('woocommerce_package_rates', 'check_shipping_rates', 20, 2);
function check_shipping_rates($rates, $package) {
error_log( print_r( $rates, true ) );
return $rates;
}
As errors are logged, they should appear in wp-content/debug.log. You can open this file in a text editor. Once you have finished, you can disable debug log.
Related: How to debug in WooCommerce 3
How to debug a wordpress plugin that gives timeout
I solved by asking on plugin's forum: https://wordpress.org/support/topic/plugin-page-giving-timeout-504/#post-13687667
I just needed to activate WP_DEBUG and WP_DEBUG_LOG flags. I discovered the line that was breaking the site and then I could properly debug and find the problem.
Related Topics
MySQLi_Query() Expects At Least 2 Parameters, 1 Given
How to Retrieve Images from MySQL Database and Display in an HTML Tag
Angularjs Http Post to PHP and Undefined
Replace Keys in an Array Based on Another Lookup/Mapping Array
Parse Query String into an Array
MySQL and PHP: Utf-8 With Cyrillic Characters
How to Get Parameters from a Url String
How to Close a Connection Early
How to Access This Object Property With an Illegal Name
What Are the PHP Operators "" and ":" Called and What Do They Do
Only Variables Should Be Passed by Reference
What Does the Variable $This Mean in PHP
How to Display HTML Tags as Plain Text
MySQLi Update Throwing Call to a Member Function Bind_Param() Error