How to Create Custom Logs in WooCommerce? A Simple Guide

Log file is helpful for troubleshooting purposes. These files basically contain a collection of events that take place in a particular WooCommerce store. For instance, you get an easily accessible ‘fatal error’ log by WooCommerce. You can find it in WordPress > Status > Logs. The debug.log file is crucial in finding WordPress weaknesses, PHP errors, etc. So, this file seems to be purposeful in troubleshooting. In this article, you will learn to create custom logs in WooCommerce. 

In your custom-made log file, you can store crucial events that take place in your WooCommerce. Having such records, you can easily and quickly resolve the website issues. Furthermore, you will learn to create logs in case of two events. In one event, we will see how to create a log in case of a failed customer order. In another event, we will see how to create a log when there is a product price change made by the admin. Enjoy the guide!

Image describing how to create custom logs in WooCommerce

PHP Snippet 1: Create Custom Logs for Failed Customer Order

Firstly, you need to find a trigger when an order is marked as failed. You can get a hook triggered on the ‘Thank You’ page. After that, you can find the failed order details in the log using wc_get_logger() function. 

add_action( 'woocommerce_before_thankyou', 'phpsof_log_failed_orders_wc_status' );
 
function phpsof_log_failed_orders_wc_status( $order_id ) {
 
   // GET ORDER FROM ORDER ID @ THANK YOU PAGE
   $order = wc_get_order( $order_id );
 
   // EXIT IF ORDER HAS NOT FAILED
   if ( ! $order->has_status( 'failed' ) ) return;
 
   // LOAD THE WC LOGGER
   $logger = wc_get_logger();
    
   // LOG THE FAILED ORDER TO CUSTOM "failed-orders" LOG
   $logger->info( wc_print_r( $order, true ), array( 'source' => 'failed-orders' ) );
 
} 

PHP Snippet 2: Create Custom logs for Product Price Change 

We need to figure out a trigger like before. Once a product is saved, you can get the woocommerce_update_product hook trigger. After that, you can find the product price details in the log using wc_get_logger() function. 

add_action( 'woocommerce_update_product', 'phpsof_log_price_changes_wc_status', 9999, 2 );
 
function phpsof_log_price_changes_wc_status( $product_id, $product ) {
 
   // GET PRODUCT PRICE
   $price = $product->get_price();
 
   // LOAD THE WC LOGGER
   $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' ) );
 
} 

Where do you add this snippet?

So, this is how you can create custom logs in WooCommerce. Also, it is easier to activate this function with this code. So, to apply this code, just add this PHP snippet at your child theme function.php file’s bottom. It should be placed before “?>”, if you have it there. Apart from this, CSS goes in your child theme style.css file. Make sure that you are editing these files in a right manner for best results.

Is this snippet still valid?

So, this is how you can learn about create custom logs in WooCommerce. I have applied this code on the Storefront theme and WordPress friendly hosting PHP 7.3. Let me know if everything works as expected when you code. Share it further if you find this snippet useful for you and it had saved your time. 

Also Read, How to Add Shipping Notices on Checkout Page at WooCommerce?

How to Restrict Shipping to Only One State in WooCommerce?

Important links: WooCommerce – https://woocommerce.com/