How to Make

WooCommerce: How to Create a Custom Log File?

WooCommerce: How to Create a Custom Log File?

Logs or custom log files are a must for WooCommerce developers and store administrators. Log files are the basic as is the number of events that take place in a particular store according to certain logging criteria. is an automatically generated list of events . For example, WooCommerce is a platform for you that you can easily access and read from the WordPress dashboard ( WooCommerce > Status > Logs ) already generates a "fatal error" log.

WordPress troubleshooting If you are familiar with when trying to identify website weaknesses, PHP bugs debug.log file you know how important it is. All right, today I'll give you your own diary How that you will create so you can record events that happen on your WooCommerce website and easily check the logs for troubleshooting purposes.

http://gurmehub.com/woocommerce/en-iyi-15-woocoomerce-eklentisi/

In particular, I will show you how to create a log every time there is a failed customer order and every time there is a product price change made by the admin. 

PHP Snippet 1: Create Custom Log File for Failed Orders

First of all we need to set a trigger (after an order is marked as failed, we will select a triggering "hook" on the Thank you page) and after that we will take the log and select the failed order details wc_get_logger () WooCommerce functionality.

custom-daily-files-wwoocommerce
/**
 * @snippet Log Failed Orders @ WC Status
 * @how-to Get GourmetWoo.com FREE
 * @author Ahmet Kaptan
 * @testedwith WooCommerce 4.4
 * @donate $9 http://gurmehub.com/
 */
 
add_action( 'woocommerce_before_thankyou', 'gurmewoo_failed_orders_wc_status' );
 
function gurmewoo_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 Daily File for Product Price Updates

First of all we have to set a trigger (after a product is registered woocommerce_update_product hook trigger) and then we will get the log and wc_get_logger () WooCommerce function allows us to log product price details.

/**
 * @snippet Log Failed Orders @ WC Status
 * @how-to Get GourmetWoo.com FREE
 * @author Ahmet Kaptan
 * @testedwith WooCommerce 4.4
 * @donate $9 http://gurmehub.com/
 */
 
add_action( 'woocommerce_update_product', 'gurmewoo_log_price_changes_wc_status', 9999, 2 );
 
function gurmewoo_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 to Add Snippet?

You can place PHP snippets under your child theme functions.php file.

Leave a Reply

Your email address will not be published. Required fields are marked *