woocommerce-es.php
11.2 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/*
Plugin Name: WooCommerce (ES)
Plugin URI: http://www.closemarketing.es/portafolio/plugin-woocommerce-espanol/
Description: Extends the WooCommerce plugin for Spanish needs: EU VAT included in form and order, translations and optimization in checkout.
Version: 1.3
Requires at least: 4.6
Author: closemarketing, davidperez
Author URI: https://www.closemarketing.es/
Text Domain: woocommerce-es
Domain Path: /languages/
License: GPL
*/
class WooCommerceESPlugin {
/**
* The current langauge
*
* @var string
*/
private $language;
/**
* Flag for the spanish langauge, true if current langauge is spanish, false otherwise
*
* @var boolean
*/
private $is_spa;
////////////////////////////////////////////////////////////
/**
* Bootstrap
*/
public function __construct( $file ) {
$this->file = $file;
// Filters and actions
add_action( 'plugins_loaded', array( $this, 'wces_plugins_loaded' ) );
add_filter( 'load_textdomain_mofile', array( $this, 'wces_load_mo_file' ), 10, 2 );
/* EU VAT */
add_filter( 'woocommerce_billing_fields' , array( $this, 'wces_add_billing_fields' ) );
add_filter( 'woocommerce_shipping_fields' , array( $this, 'wces_add_shipping_fields' ) );
add_filter( 'woocommerce_admin_billing_fields', array( $this, 'wces_add_billing_shipping_fields_admin') );
add_filter( 'woocommerce_admin_shipping_fields', array( $this, 'wces_add_billing_shipping_fields_admin') );
add_filter( 'woocommerce_load_order_data', array( $this, 'wces_add_var_load_order_data') );
add_filter( 'woocommerce_email_order_meta_keys', array( $this, 'woocommerce_email_notification'));
add_filter( 'wpo_wcpdf_billing_address', array( $this, 'wces_add_vat_invoices') );
add_filter( 'woocommerce_general_settings', array( $this, 'wces_add_vat_option') );
/* Optimizes Checkout */
add_filter( 'woocommerce_general_settings', array( $this, 'wces_add_opt_option') );
$op_checkout = get_option( 'wces_opt_checkout', 1 );
if($op_checkout=='yes') {
add_filter( 'woocommerce_locate_template', array($this,'wces_woocommerce_locate_template'), 10, 3 );
}
/*
* WooThemes/WooCommerce don't execute the load_plugin_textdomain() in the 'init'
* action, therefor we have to make sure this plugin will load first
*
* @see http://stv.whtly.com/2011/09/03/forcing-a-wordpress-plugin-to-be-loaded-before-all-other-plugins/
*/
add_action( 'activated_plugin', array( $this, 'activated_plugin' ) );
}
////////////////////////////////////////////////////////////
/**
* Activated plugin
*/
public function activated_plugin() {
$path = str_replace( WP_PLUGIN_DIR . '/', '', $this->file );
if ( $plugins = get_option( 'active_plugins' ) ) {
if ( $key = array_search( $path, $plugins ) ) {
array_splice( $plugins, $key, 1 );
array_unshift( $plugins, $path );
update_option( 'active_plugins', $plugins );
}
}
}
////////////////////////////////////////////////////////////
/**
* Plugins loaded
*/
public function wces_plugins_loaded() {
$rel_path = dirname( plugin_basename( $this->file ) ) . '/languages/';
// Load plugin text domain - WooCommerce ES
// WooCommerce mixed use of 'wc_gf_addons' and 'wc_gravityforms'
load_plugin_textdomain( 'woocommerce-es', false, $rel_path );
}
////////////////////////////////////////////////////////////
/**
* Load text domain MO file
*
* @param string $moFile
* @param string $domain
*/
public function wces_load_mo_file( $mo_file, $domain ) {
if ( $this->language == null ) {
$this->language = get_option( 'WPLANG', WPLANG );
$this->is_spa = ( $this->language == 'es' || $this->language == 'es_ES' );
}
// The ICL_LANGUAGE_CODE constant is defined from an plugin, so this constant
// is not always defined in the first 'load_textdomain_mofile' filter call
if ( defined( 'ICL_LANGUAGE_CODE' ) ) {
$this->is_spa = ( ICL_LANGUAGE_CODE == 'es' );
}
if ( $this->is_spa ) {
$domains = array(
// @see https://github.com/woothemes/woocommerce/tree/v2.0.5
'woocommerce-shipping-table-rate' => array(
'languages/woocommerce-shipping-table-rate-es_ES.mo' => 'woocommerce-shipping-table-rate/es_ES.mo'
),
'woocommerce-product-enquiry-form' => array(
'languages/woothemes-es_ES.mo' => 'woocommerce-product-enquiry-form/es_ES.mo'
),
'email-cart' => array(
'email-cart-es_ES.mo' => 'woocommerce-email-cart/es_ES.mo'
),
'wcva' => array(
'languages/wcva-es_ES.mo' => 'woocommerce-colororimage-variation-select/es_ES.mo'
),
'wc_brands' => array(
'languages/wc_brands-es_ES.mo' => 'woocommerce-brands/wc_brands-es_ES.mo'
),
'woocommerce-memberships' => array(
'languages/woocommerce-memberships-es_ES.mo' => 'woocommerce-memberships/woocommerce-memberships-es_ES.mo'
),
'woocommerce-subscriptions' => array(
'languages/woocommerce-subscriptions-es_ES.mo' => 'woocommerce-subscriptions/woocommerce-subscriptions-es_ES.mo'
),
'woocommerce-events' => array(
'languages/woocommerce-events-es_ES.mo' => 'woocommerce-events/woocommerce-events-es_ES.mo'
),
'woocommerce-brands' => array(
'languages/woocommerce-brands-es_ES.mo' => 'woocommerce-brands/woocommerce-brands-es_ES.mo'
),
);
if ( isset( $domains[$domain] ) ) {
$paths = $domains[$domain];
foreach ( $paths as $path => $file ) {
if ( substr( $mo_file, -strlen( $path ) ) == $path ) {
$new_file = dirname( $this->file ) . '/languages/' . $file;
if ( is_readable( $new_file ) ) {
$mo_file = $new_file;
}
}
}
}
}
return $mo_file;
}
//EU VAT
/**
* Insert element before of a specific array position
*
* @return array
* @since 1.0.0
*/
public function array_splice_assoc( &$source, $need, $previous ) {
$return = array();
foreach( $source as $key => $value ) {
if( $key == $previous ) {
$need_key = array_keys( $need );
$key_need = array_shift( $need_key );
$value_need = $need[$key_need];
$return[$key_need] = $value_need;
}
$return[$key] = $value;
}
$source = $return;
}
public function wces_add_billing_fields( $fields ) {
$fields['billing_company']['class'] = array('form-row-first');
$fields['billing_company']['clear'] = false;
//$fields['billing_country']['clear'] = true;
$vatinfo_mandatory = get_option( 'wces_vat_mandatory', 1 );
if($vatinfo_mandatory=='yes') $mandatory= true; else $mandatory = false;
$field = array('billing_vat' => array(
'label' => apply_filters( 'vatssn_label', __('VAT No', 'woocommerce-es') ),
'placeholder' => apply_filters( 'vatssn_label_x', __('VAT No', 'placeholder', 'woocommerce-es') ),
'required' => $mandatory,
'class' => array('form-row-last'),
'clear' => true
));
$this->array_splice_assoc( $fields, $field, 'billing_address_1');
return $fields;
}
public function wces_add_shipping_fields( $fields ) {
$fields['shipping_company']['class'] = array('form-row-first');
$fields['shipping_company']['clear'] = false;
//$fields['shipping_country']['clear'] = true;
$vatinfo_mandatory = get_option( 'wces_vat_mandatory', 1 );
if($vatinfo_mandatory=='yes') $mandatory= true; else $mandatory = false;
$field = array('shipping_vat' => array(
'label' => apply_filters( 'vatssn_label', __('VAT No', 'woocommerce-es') ),
'placeholder' => apply_filters( 'vatssn_label_x', __('VAT No', 'placeholder', 'woocommerce-es') ),
'required' => $mandatory,
'class' => array('form-row-last'),
'clear' => true
));
$this->array_splice_assoc( $fields, $field, 'shipping_address_1');
return $fields;
}
public function wces_add_billing_shipping_fields_admin( $fields ) {
$fields['vat'] = array(
'label' => apply_filters( 'vatssn_label', __('VAT No', 'woocommerce-es') )
);
return $fields;
}
public function wces_add_var_load_order_data( $fields ) {
$fields['billing_vat'] = '';
$fields['shipping_vat'] = '';
return $fields;
}
/**
* Adds NIF in email notification
*/
public function woocommerce_email_notification( $keys ) {
$keys[] = 'billing_vat';
return $keys;
}
/**
* Adds VAT info in WooCommerce PDF Invoices & Packing Slips
*/
public function wces_add_vat_invoices( $address ){
global $wpo_wcpdf;
echo $address . '<p>';
$wpo_wcpdf->custom_field( 'billing_vat', __('VAT info:', 'woocommerce-es') );
echo '</p>';
}
/* END EU VAT*/
/**
* Add option to make mandatory VAT info
*/
public function wces_add_vat_option( $settings ) {
$updated_settings = array();
foreach ( $settings as $section ) {
// at the bottom of the General Options section
if ( isset( $section['id'] ) && 'general_options' == $section['id'] &&
isset( $section['type'] ) && 'sectionend' == $section['type'] ) {
$updated_settings[] = array(
'name' => __( 'VAT info mandatory?', 'woocommerce-es' ),
'desc' => __( 'This controls if VAT info would be mandatory in checkout.', 'woocommerce-es' ),
'id' => 'wces_vat_mandatory',
'std' => 'no', // WooCommerce < 2.0
'default' => 'no', // WooCommerce >= 2.0
'type' => 'checkbox'
);
}
$updated_settings[] = $section;
}
return $updated_settings;
}
//* Optimize Checkout
/**
* Add option to optimize Checkout
*/
public function wces_add_opt_option( $settings ) {
$updated_settings = array();
foreach ( $settings as $section ) {
// at the bottom of the General Options section
if ( isset( $section['id'] ) && 'general_options' == $section['id'] &&
isset( $section['type'] ) && 'sectionend' == $section['type'] ) {
$updated_settings[] = array(
'name' => __( 'Optimize Checkout?', 'woocommerce-es' ),
'desc' => __( 'Optimizes your checkout to better conversion.', 'woocommerce-es' ),
'id' => 'wces_opt_checkout',
'std' => 'no', // WooCommerce < 2.0
'default' => 'no', // WooCommerce >= 2.0
'type' => 'checkbox'
);
}
$updated_settings[] = $section;
}
return $updated_settings;
}
function wces_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
$_template = $template;
if ( ! $template_path ) $template_path = $woocommerce->template_url;
$plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/woocommerce/';
// Look within passed path within the theme - this is priority
$template = locate_template(
array(
$template_path . $template_name,
$template_name
)
);
// Modification: Get the template from this plugin, if it exists
if ( ! $template && file_exists( $plugin_path . $template_name ) )
$template = $plugin_path . $template_name;
// Use default template
if ( ! $template )
$template = $_template;
// Return what we found
return $template;
}
} //from class
global $wces_plugin;
$wces_plugin = new WooCommerceESPlugin( __FILE__ );