How to Create Custom Logs in WooCommerce

Create Custom Logs in WooCommerceAre you looking for a way to create custom logs in WooCommerce? In today’s brief tutorial, we will show you how to create a custom log for a failed order and product price update. We will be using custom code snippets to implement this solution. This means that you need to have some coding experience. However, we will try to explain each step in detail to help you implement this solution.

If you are a serious WooCommerce developer or store manager, you must have come across the log files. These files automatically generate files when an event occurs in your store, based on the logging criteria.

For example, WooCommerce creates a fatal error log that can be easily accessed by navigating to WooCommerce > Status > Logs.

The debug.log file is very important when it comes to WordPress troubleshooting. In addition, it is important when you are trying to identify website weaknesses, PHP errors, or white screen of death. This is why it is important to know how to create custom logs for your store.

How to Create Custom Logs in WooCommerce

By the end of this post, you will be able to create a custom log for a failed order and price update. As mentioned earlier, we will be using custom code snippets. This means that you need to install or create a child theme to ensure your changes are not lost during an update.

Without wasting much time, let us get right into it.

Steps to Create Custom Logs in WooCommerce

Here are the simple steps you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file to add the function to create a failed order log.
  3. Add the following code to the functions.php file:
add_action( 'woocommerce_before_thankyou', 'njengah_log_failed_orders_wc_status' );
 function njengah_log_failed_orders_wc_status( $order_id ) {
   $order = wc_get_order( $order_id );
   if ( ! $order->has_status( 'failed' ) ) return;
   $logger = wc_get_logger();
       // LOG THE FAILED ORDER TO CUSTOM "failed-orders" LOG
   $logger->info( wc_print_r( $order, true ), array( 'source' => 'failed-orders' ) );
  1. If you want to create a product price update log, add the following code snippet to the same file:
add_action( 'woocommerce_update_product', 'njengah_log_price_changes_wc_status', 9999, 2 );
 function njengah_log_price_changes_wc_status( $product_id, $product ) {
   $price = $product->get_price();
   $logger = wc_get_logger();
       // LOG NEW PRICE TO CUSTOM "price-changes" LOG
   $logger->info( 'Product ID ' . $product_id . ' price changed to: ' . $price, array( 'source' => 'price-changes' ) );

Wrapping Up

In summary, you have seen how you can create a custom log in your WooCommerce store. The first thing you need to do is identify the trigger and get the log you want to create. This will help you to record whatever event happens on your WooCommerce website

If you need any custom solution, feel free to contact us. This will ensure that you do not break down your site.

Similar Articles

Customer Reviews


Customer Reviews

Jeffrey Todd 04/07/2020

Exceptional Plugin & Service

I'm in the process of building my new website, and I had a question about changing some text within the WooCommerce plugin. I was spinning my wheels trying to figure it out. Joe reached back and worked with me - going above and beyond! - until I fully understood how to effectively change what I wanted. How can you ask for more than that? I highly recommend WooCommerce (and Joe!), the plugin and service are exceptional!
-Jeffrey Todd (