Logging Errors In WooCommerce

WooCommerce Logging

If you’re a WordPress developer building complex custom e-commerce sites, you may find yourself in a situation where you need to log custom errors or information. Thankfully, WooCommerce provides an easy way to do it which we’re going to take a look at right now.

Log Levels and WooCommerce Logger

Starting from WooCommerce version 3.0, you can use a handy function called wc_get_logger. This function will return you a WC_Logger object which then can be used to log custom messages.

There 8 different levels of logging that can be used:

  • emergency
  • alert
  • critical
  • error
  • warning
  • notice
  • info
  • debug

Here is how we can store custom logs WooCommerce’s logger:

<?php

$logger = wc_get_logger();

$logger->info('test', ['source' => 'test-log']);

// log warnings
$logger->warning('Warning', ['source' => 'test-log']);

As you can see, we can use any of the 8 different logger levels as methods to store different kinds of error logs.

If we pass the 2nd parameter as an array in which we pass ‘source’ argument, the value of that argument will be used to name the log file in the same way.

You can find these logs by going to WooCommerce -> Status -> Logs Here is how it looks like in the admin dashboard:

WooCommerce Logs
  • Highlight #1 shows where you can find the logs
  • Hightlight #2 shows how different log levels are stored. If you used the info() method to store the log, you will see INFO before your log message. The same applies for other levels
  • Hightlight #3 shows the name of the file, which starts with the value you’ve passed to in the 2nd argument in the ‘source’ parameter. It also shows the dropdown where you can find your log file and switch between different ones.

WooCommerce Logging Examples

Let’s say you want to store information about completed orders on the thank you page. Here is how you can do it.

<?php

add_action('woocommerce_before_thankyou', function($orderId) {
    
    $logger = wc_get_logger();
    $order = wc_get_order($orderId);

    $logger->info("Order {$orderId} completed: " . wc_print_r($order, true), ['source' => 'orders-log']);

});

Now, let’s say that you have a custom validation on your site and don’t want to allow specific products to be added to a cart under certain conditions. You may also want to store in logs those failed products. Here is how it can be done

<?php

add_filter('woocommerce_add_to_cart_validation', function($passed, $productId, $quantity) {
    
    // Your custom validation logic
    
    if (! $passed) {
        $logger = wc_get_logger();

        $logger->info("Failed to add product {$productId} {$quantity}", ['source' => 'failed-products']);
    }

}, 10, 3);

Conclusion

Using custom logs in WooCommece can be a very good way to inspect certain parts of your e-commerce store. If you’re a developer who likes to have more info about potential issues in your project, you can start using WooCommerce logger in your projects.

About The Author

Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

The Ultimate Managed Hosting Platform

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.