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 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.

WP Activity Log can track WooCommerce activities, including store settings changes, product updates, orders, and many others straight out of the box. 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 made 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.

The Ultimate Guide to WordPress Oversight eBook

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 WP Activity Log 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 can 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.

Get WP Activity Log and start logging your WooCommerce activities today.

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
Uploading WP 2FA as a zip file in WordPress
WP 2FA in the WordPress plugin repository
Close

Installing WP 2FA Free

Congratulations on taking the first step towards enhancing your WordPress site's security with WP 2FA Free! You're now on your way to protecting your valuable data and ensuring peace of mind. No coding or technical knowledge is required.

 

Below are two ways to install WP 2FA on your website:

Go to your plugin dashboard on your site, then go to "Add New", and then search for WP 2FA.

Download the WP 2FA plugin zip, then select upload in your plugin dashboard under "Add New".

OPTION 1

OPTION 2

Uploading CAPTCHA 4WP as a zip file in WordPress
CAPTCHA 4WP in the WordPress plugin repository
Close

Installing CAPTCHA 4WP Free

Well done you. You're one step closer to safeguarding your WordPress website from spam and automated attacks with CAPTCHA 4WP. You'll be able to effortlessly integrate CAPTCHA into your forms and enjoy a website with enhanced security.

 

Below are two ways to install CAPTCHA 4WP on your website:

Go to your plugin dashboard on your site, then go to "Add New", and then search for CAPTCHA 4WP.

Download the CAPTCHA 4WP plugin zip, then select upload in your plugin dashboard under "Add New".

OPTION 1

OPTION 2

Uploading WP Activity Log as a zip file in WordPress
WP Activity Log in the WordPress plugin repository
Close

Installing WP Activity Log Free on your website

You deserve a pat on the back for choosing to record user actions and changes on your website. That is the first step towards better user accountability, easier troubleshooting of website security, and many other benefits of issues.

 

Below are the two ways to install WP Activity Log on your website:

Go to your plugin dashboard on your site, then go to "Add New" and then search for WP Activity Log.

Download the WP Activity Log plugin zip, then select upload in your plugin dashboard under "Add New".

OPTION 1

OPTION 2

Uploading Melapress Login Security as a zip file in WordPress
Melapress Login Security in the WordPress plugin repository
Close

Installing Melapress Login Security Free

Congratulations on taking control of your WordPress website's security by implementing robust login and password policies with Melapress Login Security. You can change your login page URL, limit failed login attempts, and reset passwords.

 

Below are two ways to install Melapress Login Security on your website:

Go to your plugin dashboard on your site, then go to "Add New" and then search for Melapress Login Security.

Download the Melapress Login Security plugin zip, then select upload in your plugin dashboard under "Add New".

OPTION 1

OPTION 2