/
jigoshop-order-locator.php
307 lines (229 loc) · 7.7 KB
/
jigoshop-order-locator.php
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
/*
Plugin Name: Jigoshop Order Locator
Plugin URI: http://wordpress.org/extend/plugins/jigoshop-coupon-products
Description: Extends JigoShop adding an unique locator per order+product
Version: 0.1
Author: Carlos Sanz García
Author URI: http://codingsomething.wordpress.com/
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
/**
* Adds news settings to Jigoshop
*
* @package Jigoshop
* @subpackage Jigoshop Order Locator
* @since 0.1
*
**/
function jigoshop_locator_access() {
// gettext
load_plugin_textdomain( 'jigoshop', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
// dependeces
$active_plugins_ = apply_filters( 'active_plugins', get_option( 'active_plugins' ) );
if ( in_array( 'jigoshop/jigoshop.php', $active_plugins_ ) && JIGOSHOP_VERSION >= 1207160 ):
Jigoshop_Base::get_options()->install_external_options_after_id( 'jigoshop_disable_fancybox', jigoshop_locator_settings() );
add_action('jigoshop_after_email_order_info', 'jigoshop_locator_after_email_order_info', 10 );
// admin hooks
if (is_admin()) {
add_action( 'jigoshop_admin_order_item_headers', 'jigoshop_locator_order_item_headers' );
add_action( 'jigoshop_admin_order_item_values', 'jigoshop_locator_order_item_values', 10, 2 );
//add_action( 'jigoshop_process_shop_order_meta', 'jigoshop_store_locator', 10 );
};
else:
if (is_admin())
add_action( 'admin_notices', 'jigoshop_locator_dependences');
endif;
}
add_action('plugins_loaded', 'jigoshop_locator_access');
/**
* Adds locator length in settings
*
* @package Jigoshop
* @subpackage Jigoshop Order Locator
* @since 0.1
*
**/
/*
function jigoshop_store_locator($post_id = false) {
if ( !$post_id ) return;
$order_items = get_post_meta($post_id, 'order_items', true);
foreach ($order_items as $row => $item):
$row++;
$locators = array();
if ( $item['qty'] > 1) {
for ( $n=1; $n < ($item['qty']+1); $n++)
$locators[] = jigoshop_generate_locator($post_id, $item['id'], $row, $n);
} else {
$locators[] = jigoshop_generate_locator($post_id, $item['id'], $row );
};
$order_items[$row-1]['locator_id'] = $locators;
endforeach;
update_post_meta($post_id, 'order_items', $order_items);
}
*/
/**
* Adds locator length in settings
*
* @package Jigoshop
* @subpackage Jigoshop Order Locator
* @since 0.1
*
**/
function jigoshop_locator_settings() {
$settings = array();
$settings[] = array(
'name' => __('Order locator', 'jigoshop'),
'type' => 'title',
'desc' => ''
);
$settings[] = array(
'name' => __('Locator length', 'jigoshop'),
'desc' => __('Default = 6', 'jigoshop'),
'tip' => __('Setting up the locator digits length; min: 6 digits, max: 32 digits.', 'jigoshop'),
'id' => 'jigoshop_locator_length',
'std' => '6',
'type' => 'range',
'extra' => array(
'min' => 6,
'max' => 32,
'step' => 1
)
);
return $settings;
}
/**
* Adds locators in order email
*
* @param int $order_id: id of order reference
*
* @package Jigoshop
* @subpackage Jigoshop Order Locator
* @since 0.1
*
**/
function jigoshop_locator_after_email_order_info($order_id) {
echo '=====================================================================' . PHP_EOL;
echo __('LOCATORS', 'jigoshop') . PHP_EOL;
echo '=====================================================================' . PHP_EOL;
$order = new jigoshop_order($order_id);
foreach ($order->items as $row => $item) {
$row++;
if ( isset($item['qty']) && !empty($item['qty']) && $item['qty'] > 1 ):
for ($n=1; $n < ($item['qty']+1); $n++) {
$locator = jigoshop_generate_locator($order_id, $item['id'], $row, $n);
$print = $locator .' - ' .$item['name'];
echo apply_filters('jigoshop_email_item_locator', $print, $locator, $order_id, $item, $row );
}
else:
$locator = jigoshop_generate_locator($order_id, $item['id'], $row);
$print = $locator .' - ' .$item['name'];
echo apply_filters('jigoshop_email_item_locator', $print, $locator, $order_id, $item, $row );
endif;
}
echo PHP_EOL;
}
/**
* Adds locators in order email
*
* @param int $order_id: id of order reference
*
* @package Jigoshop
* @subpackage Jigoshop Order Locator
* @since 0.1
*
**/
function jigoshop_get_item_id_by_locator( $order_id = false, $locator = false) {
if ( !$order_id || !$locator ) return;
$order = new jigoshop_order($order_id);
$row = 0;
foreach ($order->items as $item) {
$row++;
if ( isset($item['qty']) && !empty($item['qty']) && $item['qty'] > 1 ):
for ($n=1; $n < ($item['qty']+1); $n++) {
if ( $locator == jigoshop_generate_locator($order_id, $item['id'], $row, $n) ) return $item['id'];
}
else:
if ( $locator == jigoshop_generate_locator($order_id, $item['id'], $row) ) return $item['id'];
endif;
}
return false;
}
/**
* prints locator column in header of table of product lists on order page
*
* @package Jigoshop
* @subpackage Jigoshop Order Locator
* @since 0.1
*
**/
function jigoshop_locator_order_item_headers() {
global $post;
// dirty trick
jigoshop_session::instance()->current_admin_order = $post;
jigoshop_session::instance()->current_admin_order_item_count = 0;
?><th class="variation"><?php _e('Locators', 'jigoshop'); ?></th><?php
}
/**
* prints locator column in body of table of product lists on order page
*
* @package Jigoshop
* @subpackage Jigoshop Order Locator
* @since 0.1
*
**/
function jigoshop_locator_order_item_values($_product, $item = false) {
global $post;
if (!isset($post)) $post = jigoshop_session::instance()->current_admin_order;
// dirty trick
$row = jigoshop_session::instance()->current_admin_order_item_count += 1;
echo '<td>' . PHP_EOL;
if ( $item && !empty($item) ):
for ( $n=1; $n < ($item['qty']+1); $n++ ):
$locator = jigoshop_generate_locator($post->ID, $item['id'], $row, $n);
if (!empty($locator)) echo "<code style=\"font-size: 13px; line-height:2em\">$locator</code><br>";
endfor;
else:
$locator = jigoshop_generate_locator($post->ID, $_product->ID, $row);
if (!empty($locator)) echo "<code style=\"font-size: 13px; line-height:2em\">$locator</code><br>";
endif;
echo '</td>' . PHP_EOL;
}
/**
* generates locator hash
*
* @param int $order_ID id reference of order
* @param int $product_ID id reference of product
* @param int $row position of current item inside order
* @param int $qty quantity number inside position
*
* @return ecripted string
*
* @package Jigoshop
* @subpackage Jigoshop Order Locator
* @since 0.1
*
**/
function jigoshop_generate_locator($order_ID = false, $product_ID = false, $row = 1, $qty = 1) {
if (!$order_ID || !$product_ID ) return '';
$locator = "#$order_ID#$product_ID#$row#$qty";
$length = Jigoshop_Base::get_options()->get_option('jigoshop_locator_length');
$output = md5($locator, false);
return substr($output, 0, $length);
}
/**
* admin notice: depencendes
*
* @package Jigoshop
* @subpackage Jigoshop Order Locator
* @since 0.1
*
**/
function jigoshop_locator_dependences() {
global $current_screen;
echo "<div class=\"error\">" . PHP_EOL;
echo "<p><strong>Jigoshop Order Locator:</strong></p>" . PHP_EOL;
echo "<p>" . __('This plugin requires at least <strong>Jigoshop 1.3</strong> active.', 'jigoshop') . "</p>" . PHP_EOL;
echo "</div>" . PHP_EOL;
}