woocommerce-advanced-ordernumbers.php 4.3 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?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
	load_plugin_textdomain('your-unique-name', false, basename( dirname( __FILE__ ) ) . '/languages' );

	if (!class_exists("OpenToolsOrdernumbers")) {
		class OpenToolsOrdernumbers {
			/**
			 * Construct the plugin object
			 */
			public function __construct()
			{
				add_action ('woocommerce_order_number', array(&$this, 'get_ordernumber'));
				add_action( 'admin_init', array(&$this, 'admin_init'));
				add_action( 'admin_menu', array(&$this, 'admin_menu'));
				// register actions
			}
    
			/**
			* 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
				register_setting('opentools-ordernumber-group', 'customize_ordernumber');
				register_setting('opentools-ordernumber-group', 'ordernumber_format');
				register_setting('opentools-ordernumber-group', 'ordernumber_counter_scope');
			}
			
			/**
			 * add a menu
			 */     
			public function add_menu()
			{
				add_options_page('Advanced Ordernumber Plugin Settings', 'OpenTools Advanced Ordernumbers', 
					'manage_options', 'opentools-ordernumbers', array(&$this, 'plugin_settings_page'));
			} // END public function add_menu()

			/**
			 * Menu Callback
			 */     
			public function plugin_settings_page()
			{
// 				if(!current_user_can('manage_options'))
// 				{
// 					wp_die(__('You do not have sufficient permissions to access this page.'));
// 				}

				// 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) {
				$stored_number = get_post_meta( $orderid, 'ordernumber', 'true');
				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);
					update_post_meta( $orderid, 'ordernumber', $number );
					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();
		
		// Add a link to the settings page onto the plugin page
		if(isset($ordernumber_plugin)) {
			// Add the settings link to the plugins page
			function plugin_settings_link($links)
			{ 
				$settings_link = '<a href="options-general.php?page=wp_plugin_template">Settings</a>'; 
				array_unshift($links, $settings_link); 
				return $links; 
			}

			$plugin = plugin_basename(__FILE__); 
			add_filter("plugin_action_links_$plugin", 'plugin_settings_link');
		}	
	}
 
}