Home Blog WordPress Management How to add additional WooCommerce product metadata to the WordPress activity log

How to add additional WooCommerce product metadata to the WordPress activity log

How to add additional WooCommerce product metadata to the WordPress activity log

WP Activity Log includes many features designed for different use cases, allowing users to get the most out of the plugin in vastly different scenarios. Those looking to extend this functionality to fit niche use cases or integrate custom processes can also make use of the plugin’s hooks. These hooks allow WP Activity Log to extend its functionality beyond what’s included in the box.

Hooks can be used to keep track of virtually any change on your WordPress website. One notable example is WooCommerce users, who may need to track additional events and details related to their store.

Activity Log Viewer

When using the extension Activity Log for WooCommerce, WP Activity Log can track WooCommerce activities, including store settings changes, product updates, orders, and many others. By default, the plugin reports important data in the activity log, such as the product name, ID and SKU, the username and role of the user who did the change, and other relevant information. Administrators, however, may need to include additional information such as product stock quantities, for example. This can be achieved through hooks.

For a complete list of changes the plugin tracks, refer to the list of event IDs used in the WP Activity Log.

In this how-to, we will show you how to add product stock quantities to the activity log by creating a mu-plugin.

Example: Adding the product’s stock quantity in the activity log with a mu-plugin.

What is a mu-plugin?

MU stands for must-use. A must-use plugin is a type of plugin that WordPress will always load, hence why it’s called a must-use. Creating a mu-plugin is easy. All you need to do is save the below code in a PHP file, then upload it to the /wp-content/mu-plugins/ directory on your WordPress website file system.

Before you upload the mu-plugin

Kindly make sure you have the WP Activity Log WooCommerce extension installed and activated before uploading the mu-plugin since this is a requirement for this feature to work. You might also want to test the plugin in a WordPress testing environment before uploading it to your production environment to ensure that all steps have been followed correctly.

mu-plugin code example

The below code is provided on an as-is basis as an example. You can make changes to the code depending on your requirements. Having some PHP experience is recommended.

?php
​
/**
* Adds product stock quantity metadata for events 9000-9025.
*
* @param array $metadata Event data.
* @param integer $code Event ID.
*
* @since latest
*/
function wpws_emdt_change_metadata_definition( $metadata, $code ) {
if ( $code > 9000 && $code < 9025 ) {
$metadata[ __( Stock quantity, 'wpws_emdt' ) ] = '%stockQuantity%';
}
​
return $metadata;
}
​
add_filter( 'wsal_event_metadata_definition', 'wpws_emdt_change_metadata_definition', 10, 2 );
​
/**
* Populates custom metadata item product SKU with the desired value.
*
* @param array $event_data Event data.
* @param integer $event_id Event ID.
*
* @since latest
*/
function wpws_emdt_add_items_to_metadata( $event_data, $event_id ) {
if ( $event_id > 9000 && $event_id < 9025 && array_key_exists( 'PostID', $event_data ) && function_exists( 'wc_get_product' ) ) {
$product = wc_get_product( $event_data['PostID'] );
if ( $product instanceof WC_Product ) {
$event_data['stockQuantity'] = $product->get_stock_quantity();
}
}
​
return $event_data;
}
​
add_filter( 'wsal_event_data_before_log', 'wpws_emdt_add_items_to_metadata', 10, 2 );

More about hooks and WP Activity Log

Hooks offer a flexible way to extend the functionality of WP Activity Log – allowing you to add new custom events and modify existing ones. This flexibility ensures that WP Activity Log is able to cater to the most unique and demanding scenarios, helping you stay on top of your WordPress administration game.

Working with and using WordPress activity log hooks is easy, with no need for advanced development skills to get the job done.

If you’re new to WP Activity Log and would like to see what you can do with the hooks before making a purchase, we offer a free 14-day WP Activity Log trial so that you can give the plugin a good test run before deciding whether it’s the right purchase for you.

Posted inWordPress Management
Joel Farrugia
Joel Barbara

Joel is our technical writer responsible for writing the different kinds of content we need. With a background in tech and content, he has a passion for making technology accessible and understandable for everyone. You can reach Joel at joel@melapress.com.


Leave a Reply

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

Stay in the loop

Subscribe to the Melapress newsletter and receive curated WordPress management and security tips and content.

Newsletter icon

It’s free and you can unsubscribe whenever you want. Check our blog for a taste.

Envelope icon
newsletter-pop-up