forked from MontaServices/woocommerce-monta-checkout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
montapacking-checkout.php
executable file
·377 lines (300 loc) · 16.6 KB
/
montapacking-checkout.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
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
<?php
/**
* Plugin Name: Monta Checkout
* Plugin URI: https://github.com/Montapacking/woocommerce-monta-checkout
* Description: Monta Check-out extension
* Version: 1.57.1
* Author: Monta
* Author URI: https://www.monta.nl/
* Developer: Monta
* Developer URI: https://www.monta.nl/
* Text Domain: montapacking-checkout-woocommerce-extension
* Domain Path: /languages
*
* WC requires at least: 4.0.1
* WC tested up to: 6.1.1
*
* Copyright: © 2009-2021 WooCommerce.
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
include('montapacking-config.php');
include('montapacking-class.php');
if (esc_attr(get_option('monta_logerrors'))) {
define('WC_LOG_HANDLER', 'WC_Log_Handler_DB');
}
## Add config actions
add_action('admin_menu', 'montacheckout_init_menu');
add_action('admin_init', function () {
register_setting('montapacking-plugin-settings', 'monta_shop');
register_setting('montapacking-plugin-settings', 'monta_username');
register_setting('montapacking-plugin-settings', 'monta_password');
register_setting('montapacking-plugin-settings', 'monta_google_key');
register_setting('montapacking-plugin-settings', 'monta_logerrors');
register_setting('montapacking-plugin-settings', 'monta_pickupname');
register_setting('montapacking-plugin-settings', 'monta_shippingcosts_fallback_woocommerce');
register_setting('montapacking-plugin-settings', 'monta_shippingcosts');
register_setting('montapacking-plugin-settings', 'monta_shippingcosts_start');
register_setting('montapacking-plugin-settings', 'monta_leadingstock');
register_setting('montapacking-plugin-settings', 'monta_disablepickup');
register_setting('montapacking-plugin-settings', 'monta_disablecollect');
register_setting('montapacking-plugin-settings', 'monta_checkproductsonsku');
register_setting('montapacking-plugin-settings', 'monta_standardshipmentname');
register_setting('montapacking-plugin-settings', 'monta_max_pickuppoints');
});
// Include installed Language packs
load_plugin_textdomain('montapacking-checkout', false, dirname(plugin_basename(__FILE__)) . '/languages/');
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", 'montacheckout_plugin_add_settings_link');
## Check of we in woocommerce zijn
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
## Standaard woocommerce verzending uitschakelen
add_filter('woocommerce_shipping_calculator_enable_postcode', false);
add_filter('woocommerce_shipping_calculator_enable_city', false);
## Shipping form in checkout plaatsen
add_action('woocommerce_before_order_notes', array('montapacking', 'shipping'), 10);
## Shipping package toevoegen
add_action('woocommerce_cart_shipping_packages', array('montapacking', 'shipping_package'), 10);
## Shipping cost calculation
add_action('woocommerce_package_rates', array('montapacking', 'shipping_calculate'), 10);
## Shipping cost calculation
add_action('woocommerce_review_order_before_shipping', array('montapacking', 'shipping_calculate'), 10);
add_filter('woocommerce_cart_get_total', array('montapacking', 'shipping_total'), 10, 1);
add_filter('woocommerce_cart_get_shipping_total', array('montapacking', 'shipping_total'), 10, 1);
## Shipping cost calculation
update_option('woocommerce_enable_shipping_calc', 'no');
update_option('woocommerce_shipping_cost_requires_address', 'no');
## Validation rules
add_action('woocommerce_after_checkout_validation', array('montapacking', 'checkout_validate'), 10, 2);
## Shipment data opslaan bij order
add_action('woocommerce_checkout_create_order', array('montapacking', 'checkout_store'), 10, 2);
// CSS/JS scripts registreren
add_action('wp_enqueue_scripts', 'montacheckout_enqueue_scripts');
## Ajax actions
add_action('wp_ajax_monta_shipping_options', array('montapacking', 'shipping_options'));
add_action('wp_ajax_nopriv_monta_shipping_options', array('montapacking', 'shipping_options'));
## Init session usage#
add_action('init', 'montacheckout_register_session');
//add_filter('woocommerce_order_shipping_to_display_shipped_via', 'filter_woocommerce_order_shipping_to_display_shipped_via', 10, 2);
add_filter('woocommerce_order_shipping_method', 'filter_woocommerce_order_shipping_method', 10, 2);
add_filter('woocommerce_cart_needs_shipping_address', 'filter_woocommerce_cart_needs_shipping_address', 10, 1);
add_filter('woocommerce_cart_totals_order_total_html', array('montapacking', 'taxes'), 20, 1 );
add_action( 'woocommerce_cart_totals_before_shipping', 'filter_review_order_before_shipping' );
add_action("woocommerce_removed_coupon", 'updatecheckout');
add_action("woocommerce_applied_coupon", 'updatecheckout');
} else {
add_action('woocommerce_checkout_create_order', 'checkout_create_order', 20, 2);
add_action('woocommerce_before_checkout_form', 'before_checkout_form', 20, 2);
add_action('woocommerce_package_rates', 'overrule_package_rates', 20, 2);
}
function filter_review_order_before_shipping($needs_shipping_address)
{
do_action( 'woocommerce_review_order_before_shipping' );
}
function filter_woocommerce_cart_needs_shipping_address($needs_shipping_address)
{
$cart_needs_shipping_address = true;
if (empty($_POST['montapacking']) || !is_array($_POST['montapacking'])) {
return $cart_needs_shipping_address;
}
$postdata = sanitize_post($_POST['montapacking']);
if ($postdata['shipment']['type'] == 'pickup') {
$cart_needs_shipping_address = false;
}
return $cart_needs_shipping_address;
}
function updateCheckout()
{
echo '<script type="text/javascript">jQuery("#billing_postcode").trigger("change");</script>';
}
function montacheckout_plugin_add_settings_link($links)
{
$settings_link[] = '<a href="options-general.php?page=montapacking-settings">' . __('Settings') . '</a>';
#array_push( $links, $settings_link );
return array_merge($settings_link, $links);
}
function montacheckout_enqueue_scripts()
{
// CSS
if (is_cart() || is_checkout()) {
wp_enqueue_style('montapacking_checkout_plugin', plugins_url('montapacking-checkout-woocommerce-extension/assets/css/monta-shipping.css'), array(), date("h:i:s"));
// Javascript
wp_enqueue_script('montapacking_checkout_plugin_map', 'https://maps.google.com/maps/api/js?key=' . esc_attr(get_option('monta_google_key')), ['jquery']);
wp_enqueue_script('montapacking_checkout_plugin_handlebars', plugins_url('montapacking-checkout-woocommerce-extension/assets/js/monta-handlebars.js'), ['jquery'], date("h:i:s"));
wp_enqueue_script('montapacking_checkout_plugin_storelocator_js', plugins_url('montapacking-checkout-woocommerce-extension/assets/js/monta-storelocator.js'), ['jquery'], date("h:i:s"));
wp_enqueue_script('montapacking_checkout_plugin_monta', plugins_url('montapacking-checkout-woocommerce-extension/assets/js/monta-shipping.js'), ['jquery'], date("h:i:s"));
wp_enqueue_script('montapacking_checkout_plugin_popper', plugins_url('montapacking-checkout-woocommerce-extension/assets/js/popper.min.js'), date("h:i:s"));
}
}
function montacheckout_register_session()
{
if (!session_id()) {
session_start();
}
}
function montacheckout_init_menu()
{
add_submenu_page('options-general.php', 'Montapacking', 'Montapacking', 'manage_options', 'montapacking-settings', 'montacheckout_render_settings');
}
function montacheckout_render_settings()
{
// Check that the user is allowed to update options
if (!current_user_can('manage_options')) {
wp_die('You do not have sufficient permissions to access this page.');
}
?>
<div class="wrap">
<form action="options.php" method="post">
<?php
settings_fields('montapacking-plugin-settings');
do_settings_sections('montapacking-plugin-settings');
?>
<h1>Monta Checkout</h1>
<table class="form-table">
<tr>
<th scope="row"><label for="monta_shop">Shop * </label></th>
<td><input required type="text" name="monta_shop"
value="<?php echo esc_attr(get_option('monta_shop')); ?>" size="50"/>
<br><i style="font-size:12px">The name of the webshop in Monta Portal. Name can be found <a
target="_new" href="https://montaportal.nl/Home/CustomerSettings#CheckoutOptions">here</a>.</i>
</td>
</tr>
<tr>
<th scope="row"><label for="monta_username">Username * </label></th>
<td><input required type="text" name="monta_username"
value="<?php echo esc_attr(get_option('monta_username')); ?>" size="50"/>
<br><i style="font-size:12px">The username of Monta REST API provided by Montapacking.</i>
</td>
</tr>
<tr>
<th scope="row"><label for="monta_password">Password * </label></th>
<td><input required type="password" name="monta_password"
value="<?php echo esc_attr(get_option('monta_password')); ?>" size="50"/>
<br><i style="font-size:12px">The password of Monta REST API provided by Montapacking.</i>
</td>
</tr>
<tr>
<th scope="row"><label for="monta_shippingcosts_fallback_woocommerce">Use WooCommerce shipping costs as fallback *</label></th>
<td>
<input type="checkbox" name="monta_shippingcosts_fallback_woocommerce"
value="1" <?php checked('1', get_option('monta_shippingcosts_fallback_woocommerce')); ?>/>
<br><i style="font-size:12px">Use shipping costs set in WooCommerce settings instead of amount set below as fallback if API connection is unsuccessful.</i>
</td>
</tr>
<tr>
<th scope="row"><label for="monta_shippingcosts">Shipping Costs *</label></th>
<td>
<input required type="number" name="monta_shippingcosts" step="0.01"
value="<?php echo esc_attr(get_option('monta_shippingcosts')); ?>" size="5"/>
<br><i style="font-size:12px">The base shipping costs used when there is no API connection
available.</i>
</td>
</td>
</tr>
<tr>
<th scope="row"><label for="monta_shippingcosts_start">Start shipping Costs *</label></th>
<td>
<input required type="number" name="monta_shippingcosts_start" step="0.01"
value="<?php echo esc_attr(get_option('monta_shippingcosts_start')); ?>" size="5"/>
<br><i style="font-size:12px">The start shipping costs which is used in the shopping cart
available.</i>
</td>
</td>
</tr>
<tr>
<th scope="row"><label for="monta_checkproductsonsku">Check products on SKU</label></th>
<td><input type="checkbox" name="monta_checkproductsonsku"
value="1" <?php checked('1', get_option('monta_checkproductsonsku')); ?>/>
<br><i style="font-size:12px">If this option is active, the stock, sizes and weights of the SKUs are checked with the data known in the Montaportal.</i>
</td>
</tr>
<tr>
<th scope="row"><label for="monta_logerrors">Log errors</label></th>
<td><input type="checkbox" name="monta_logerrors"
value="1" <?php checked('1', get_option('monta_logerrors')); ?>/>
<br><i style="font-size:12px">Turn on logs which are shown <a
href=/wp-admin/admin.php?page=wc-status&tab=logs">here</a>.</i>
</td>
</tr>
<tr>
<th scope="row"><label for="monta_disablepickup">Disable pickup points</label></th>
<td><input type="checkbox" name="monta_disablepickup"
value="1" <?php checked('1', get_option('monta_disablepickup')); ?>/>
<br><i style="font-size:12px">When disabled no pickup points are shown.</i>
</td>
</tr>
<tr>
<th scope="row"><label for="monta_disablecollect">Disable collect in store</label></th>
<td><input type="checkbox" name="monta_disablecollect"
value="1" <?php checked('1', get_option('monta_disablecollect')); ?>/>
<br><i style="font-size:12px">When disabled no store collect options are shown.</i>
</td>
</tr>
<tr>
<th scope="row"><label for="monta_max_pickuppoints">Max pickup points *</label></th>
<td>
<input required type="number" name="monta_max_pickuppoints" step="1" min="1" max="10"
value="<?php echo esc_attr(get_option('monta_max_pickuppoints')) <= 0 ? 3 : esc_attr(get_option('monta_max_pickuppoints')) ; ?>" size="5"/>
<br><i style="font-size:12px">The number of pickupoints shown in the overview view</i>
</td>
</td>
</tr>
<input type="hidden" name="monta_leadingstock" value="woocommerce">
<tr>
<th scope="row"><label for="monta_pickupname">Pickup name</label></th>
<td><input type="text" name="monta_pickupname"
value="<?php echo esc_attr(get_option('monta_pickupname')); ?>" size="50"/>
<br><i style="font-size:12px">In some situations you want to change the company name of the option 'AFH'. Here you can override this name.</i>
</td>
</tr>
<tr>
<th scope="row"><label for="monta_standardshipmentname">Standard shipment name</label></th>
<td><input type="text" name="monta_standardshipmentname"
value="<?php echo esc_attr(get_option('monta_standardshipmentname')); ?>" size="50"/>
<br><i style="font-size:12px">We have a standard shipment option in the Montaportal. Here you can override this name.</i>
</td>
</tr>
</table>
<h1>Google API Settings</h1>
<table class="form-table">
<tr>
<th scope="row"><label for="monta_google_key">API Key</label></th>
<td><input type="text" name="monta_google_key"
value="<?php echo esc_attr(get_option('monta_google_key')); ?>" size="50"/>
<br><i style="font-size:12px">A Google API key is required if you want to make use of the world map. A Google key can be created
<a target="_new" href="https://console.cloud.google.com/">here</a>.</i>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
function filter_woocommerce_order_shipping_method($html, $instance)
{
$json = json_decode($instance);
$order = wc_get_order($json->id);
$line_items_shipping = $order->get_items('shipping');
foreach ($line_items_shipping as $item_id => $item) {
$meta_data = $item->get_meta_data();
foreach ($meta_data as $value) {
if ($value->key == 'Shipmentmethod') {
$method = strip_tags($value->value);
if ($value->value == 'SEL,SELBuspakje') {
$method = strip_tags('DHL');
}
return $method;
}
if ($value->key == 'Pickup Data') {
$method = strip_tags($value->value['description']);
return $method;
}
}
}
$names = array();
foreach ($order->get_shipping_methods() as $shipping_method) {
$names[] = $shipping_method->get_name();
}
return implode(', ', $names);
}