woocommerce-advanced-ordernumbers.php 4.35 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
<?php
/**
 * Plugin Name: Advanced Ordernumbers
 * Plugin URI: http://open-tools.net/WooCommerce/Advanced-Ordernumbers.php
 * Description: Lets the user freely configure the order numbers in WooCommerce.
 * Version: 0.1.0
 * Author: Open Tools, Reinhold Kainhofer
 * Author URI: http://open-tools.net
 * Text Domain: woocommerce-advanced-ordernumbers
 * Domain Path: 
 * License: GPL2+
 WC requires at least: 2.2
WC tested up to: 2.3
*/

/**
 * The structure of this plugin follows the tutorial:
 * http://www.yaconiello.com/blog/how-to-write-wordpress-plugin/
 */
 
if ( ! defined( 'ABSPATH' ) ) { 
	exit; // Exit if accessed directly
}
/**
 * Check if WooCommerce is active
 **/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
    // Put your plugin code here
    // Load the language files
30
	load_plugin_textdomain('opentools-ordernumbers', false, basename( dirname( __FILE__ ) ) . '/languages' );
31 32 33 34 35 36 37 38

	if (!class_exists("OpenToolsOrdernumbers")) {
		class OpenToolsOrdernumbers {
			/**
			 * Construct the plugin object
			 */
			public function __construct()
			{
39
				$plugin = plugin_basename(__FILE__); 
40
				// register actions
41 42 43 44 45
				add_action( 'admin_init', array(&$this, 'admin_init'));
				add_action( 'admin_menu', array(&$this, 'add_menu'));
				// register filters
				add_filter("plugin_action_links_$plugin", array(&$this, 'plugin_settings_link'));
				add_filter ('woocommerce_order_number', array(&$this, 'get_ordernumber'), 10, 2/*<= Also get the order object! */);
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
			}
    
			/**
			* Activate the plugin
			*/
			public static function activate()
			{
				// Do nothing
			}
    
			/**
			* Deactivate the plugin
			*/     
			public static function deactivate()
			{
				// Do nothing
			}

			/**
			 * hook into WP's admin_init action hook
			 */
			public function admin_init()
			{
				// Set up the settings for this plugin
				$this->init_settings();
				// Possibly do additional admin_init tasks
			}
			
			/**
			* Initialize some custom settings
			*/     
			public function init_settings()
			{
				// register the settings for this plugin
80 81 82 83
				// TODO: Properly design the set of options needed
				register_setting('opentools-ordernumbers-group', 'customize_ordernumbers');
				register_setting('opentools-ordernumbers-group', 'ordernumber_format');
				register_setting('opentools-ordernumbers-group', 'ordernumber_counter_scope');
84 85 86 87 88 89 90
			}
			
			/**
			 * add a menu
			 */     
			public function add_menu()
			{
91
				// TODO: Move this inside the WooCommerce configuration!
92 93 94 95
				add_options_page('Advanced Ordernumber Plugin Settings', 'OpenTools Advanced Ordernumbers', 
					'manage_options', 'opentools-ordernumbers', array(&$this, 'plugin_settings_page'));
			} // END public function add_menu()

96 97 98 99 100 101 102
			function plugin_settings_link($links)
			{ 
				$settings_link = '<a href="options-general.php?page=opentools-ordernumbers">Settings</a>'; 
				array_unshift($links, $settings_link); 
				return $links; 
			}

103 104
			/**
			 * Menu Callback
105
			 */
106 107
			public function plugin_settings_page()
			{
108 109 110 111
				if(!current_user_can('manage_options'))
				{
					wp_die(__('You do not have sufficient permissions to access this page.'));
				}
112 113 114 115 116 117 118 119 120 121 122

				// Render the settings template
				include(sprintf("%s/templates/settings.php", dirname(__FILE__)));
			}
			
			
			
			/** 
			 * The hook to customize order numbers (requests the order number from the database; creates a new ordernumber if no entry exists in the database)
			 */
			function get_ordernumber($orderid, $order) {
123
				$stored_number = get_post_meta( $orderid, 'Order Number', 'true');
124 125 126 127 128
				if (!empty($stored_number)) {
					return $stored_number;
				} else {
					// create a new one (if this is a new order, otherwise we don't have/generate a number!)
					$number = $this->create_ordernumber($orderid, $order);
129
					update_post_meta( $orderid, 'Order Number', $number );
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
					return $number;
				}
			}

			function create_ordernumber($orderid, $order) {
				$rnd=rand(100, 1000000);
				return "Order-$orderid-Number-$rnd";
			}
	
		}
	}

	if (class_exists("OpenToolsOrdernumbers")) {
		// Installation and uninstallation hooks
		register_activation_hook(__FILE__, array('OpenToolsOrdernumbers', 'activate'));
		register_deactivation_hook(__FILE__, array('OpenToolsOrdernumbers', 'deactivate'));

		// instantiate the plugin class
		$ordernumber_plugin = new OpenToolsOrdernumbers();
	}
 
}