corsonr
5/6/2015 - 1:10 PM

WooCommerce: create a custom button in Tools

WooCommerce: create a custom button in Tools

<?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();