WooCommerce: Add a Custom Action in Tools

In WooCommerce, if you go to System Status > Tools you can see a list of useful default tools such as “Clear Transients”, “Recount Terms”, or “Install WooCommerce Pages”.

But do you know that you can create your own custom buttons there? It can be very useful when you create a plugin that needs advanced tools.

In the code below you’ll see that adding a button linked to a custom action is pretty easy. To make the code easier to test, I embed the code in a plugin.

The idea is to register a new button using a hook (filter) called woocommerce_debug_tools and then create the callback function that will be triggered when the button is clicked.

The function called debug_button_action() will be where you will have to create custom coding. You can for example send an email, delete en option, create content etc… just be creative!

Final Result:

woocommerce-custom-tools-button

Code:

<?php
/*
Plugin Name: WC Tools - Custom Button
Plugin URI: http://remicorson.com
Description: A simple plugin to add a custom button to WooCommerce tools
Author: Remi Corson
Contributors: corsonr
Author URI: http://remicorson.com
Version: 1.0
/*
/**
 * WC_Tools_Custom_Button class.
 */
class WC_Tools_Custom_Button {

	/**
	 * __construct function.
	 *
	 * @access public
	 * @return void
	 */
	function __construct() {

		add_filter( 'woocommerce_debug_tools', array( $this,'debug_button' ) );

	}

	/**
	 * debug_button function.
	 *
	 * @access public
	 * @param mixed $old
	 * @return void
	 */
	function debug_button( $old ) {
		$new = array(
			'my_custom_button' => array(
				'name'		=> __( 'My custom button', '' ),
				'button'	=> __( 'Click me!', '' ),
				'desc'		=> __( 'This is my custom button description.', '' ),
				'callback'	=> array( $this, 'debug_button_action' ),
			),
		);
		$tools = array_merge( $old, $new );

		return $tools;
	}

	/**
	 * debug_button_action function.
	 *
	 * @access public
	 * @return void
	 */
	function debug_button_action() {
		// do what you want here
		echo '<div class="updated"><p>' . __( 'My custom action has been triggered!', '' ) . '</p></div>';
	}

}

$GLOBALS['WC_Tools_Custom_Button'] = new WC_Tools_Custom_Button();

That’s it! Simple and useful right?