/
collivery.php
274 lines (229 loc) · 8.34 KB
/
collivery.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
<?php
define('_MDS_DIR_', __DIR__);
define('MDS_VERSION', "2.2.4");
include('autoload.php');
/**
* Plugin Name: MDS Collivery
* Plugin URI: http://www.collivery.co.za/
* Description: Plugin to add support for MDS Collivery in WooCommerce.
* Version: 2.2.4
* Author: Bryce Large
* License: GNU/GPL version 3 or later: http://www.gnu.org/licenses/gpl.html
*/
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
/**
* Register Install function
*/
register_activation_hook(__FILE__, 'install');
/**
* When the plugin is installed we check if the mds collivery table exists and if not creates it.
*/
function install()
{
global $wpdb;
// Creates our table to store our accepted deliveries
$table_name = $wpdb->prefix . 'mds_collivery_processed';
$sql = "CREATE TABLE IF NOT EXISTS `$table_name` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`waybill` int(11) NOT NULL,
`order_id` int(11) NOT NULL,
`validation_results` TEXT NOT NULL,
`status` int(1) NOT NULL DEFAULT 1,
PRIMARY KEY (`id`)
);";
$wpdb->query($sql);
add_option("mds_db_version", MDS_VERSION);
}
add_action('plugins_loaded', 'init_mds_collivery', 0);
/**
* Instantiate the plugin
*/
function init_mds_collivery()
{
global $wpdb;
// Check if 'WC_Shipping_Method' class is loaded, else exit.
if (!class_exists('WC_Shipping_Method')) {
return;
}
// If this plugin is sub 2.0.1 then alter the mds table to include the order_id column
$testForColumn = $wpdb->get_row("SELECT * FROM " . $wpdb->prefix . "mds_collivery_processed", ARRAY_A);
if(!empty($testForColumn) && !array_key_exists('order_id', $testForColumn)) {
$wpdb->query("ALTER TABLE " . $wpdb->prefix . "mds_collivery_processed ADD order_id INT NULL AFTER id");
}
require_once('WC_Mds_Shipping_Method.php');
require_once('mds_admin.php'); // Admin scripts
require_once('mds_checkout_fields.php'); // Checkout fields.
/**
* Load JS file throughout
*/
function load_js()
{
wp_register_script('mds_js', plugins_url('script.js', __FILE__), array('jquery'));
wp_enqueue_script('mds_js');
}
add_action('wp_enqueue_scripts', 'load_js');
/**
* Check for updates
*/
if(is_admin()) {
new GitHubPluginUpdater( __FILE__, 'Collivery', "Collivery-WooCommerce" );
}
}
/**
* Register Chipping Plugin with WooCommerce
*
* @param $methods
* @return array
*/
function add_mds_shipping_method($methods)
{
$methods[] = 'WC_Mds_Shipping_Method';
return $methods;
}
add_filter('woocommerce_shipping_methods', 'add_mds_shipping_method');
/**
* WooCommerce caches pricing information.
* This adds our unique fields to the hash to update pricing cache when changed.
*
* @param $packages
* @return mixed
*/
function mds_collivery_cart_shipping_packages($packages)
{
/** @var \MdsSupportingClasses\MdsColliveryService $mds */
$mds = MdsColliveryService::getInstance();
$settings = $mds->returnPluginSettings();
if ($settings['enabled'] == 'no' || !$defaults = $mds->returnDefaultAddress()) {
return false;
}
$collivery = $mds->returnColliveryClass();
$towns = $collivery->getTowns();
$location_types = $collivery->getLocationTypes();
$package = $mds->buildPackageFromCart(WC()->cart->get_cart());
$cart = $mds->getCartContent($package);
if(!is_array($cart) || !isset($cart['total'])) {
return false;
}
if(isset($_POST['post_data'])) {
parse_str($_POST['post_data'], $post_data);
if(!isset($post_data['ship_to_different_address']) || $post_data['ship_to_different_address'] != TRUE) {
$to_town_id = $post_data['billing_state'];
$to_town_type = $post_data['billing_location_type'];
} else {
$to_town_id = $post_data['shipping_state'];
$to_town_type = $post_data['shipping_location_type'];
}
} elseif(isset($_POST['ship_to_different_address'])) {
if (!isset($_POST['ship_to_different_address']) || $_POST['ship_to_different_address'] != TRUE) {
$to_town_id = $_POST['billing_state'];
$to_town_type = $_POST['billing_location_type'];
} else {
$to_town_id = $_POST['shipping_state'];
$to_town_type = $_POST['shipping_location_type'];
}
} elseif(isset($packages[0]['destination'])) {
$to_town_id = $packages[0]['destination']['state'];
if (!isset($_POST['ship_to_different_address']) || $_POST['ship_to_different_address'] != TRUE) {
$to_town_type = $_POST['billing_location_type'];
}else {
$to_town_type = $_POST['shipping_location_type'];
}
}
$package['cart'] = $cart;
$package['method_free'] = $settings["method_free"];
$package['free_min_total'] = $settings["free_min_total"];
$package['free_local_only'] = $settings["free_local_only"];
if (!isset($_POST['ship_to_different_address']) || $_POST['ship_to_different_address'] != TRUE) {
$package['destination'] = array(
"from_town_id" => (int) $defaults['address']['town_id'],
"from_location_type" => (int) $defaults['address']['location_type'],
"to_town_id" => (int) array_search($to_town_id, $towns),
"to_location_type" => (int) array_search($to_town_type, $location_types),
'country' => WC()->customer->get_country(),
'state' => WC()->customer->get_state(),
'postcode' => WC()->customer->get_postcode(),
'city' => WC()->customer->get_city(),
'address' => WC()->customer->get_address(),
'address_2' => WC()->customer->get_address_2()
);
} else {
$package['destination'] = array(
"from_town_id" => (int) $defaults['address']['town_id'],
"from_location_type" => (int) $defaults['address']['location_type'],
"to_town_id" => (int) array_search($to_town_id, $towns),
"to_location_type" => (int) array_search($_POST['shipping_location_type'], $location_types),
'country' => WC()->customer->get_shipping_country(),
'state' => WC()->customer->get_shipping_state(),
'postcode' => WC()->customer->get_shipping_postcode(),
'city' => WC()->customer->get_shipping_city(),
'address' => WC()->customer->get_shipping_address(),
'address_2' => WC()->customer->get_shipping_address_2()
);
}
if ($settings["method_free"] == 'yes' && $cart['total'] >= $settings["free_min_total"]) {
$package['service'] = 'free';
if($settings["free_local_only"] == 'yes') {
$data = array(
"num_package" => 1,
"service" => 2,
"exclude_weekend" => 1,
) + $package['destination'];
// Query the API to test if this is a local delivery
$response = $collivery->getPrice($data);
if(isset($response['delivery_type']) && $response['delivery_type'] == 'local') {
$package['local'] = 'yes';
if($mds->validPackage($package)) {
$packages[0] = $package;
}
} else {
$package['local'] = 'no';
}
} else {
if($mds->validPackage($package)) {
$packages[0] = $package;
}
}
} else {
if($mds->validPackage($package)) {
$packages[0] = $package;
}
}
return $packages;
}
add_filter('woocommerce_cart_shipping_packages', 'mds_collivery_cart_shipping_packages');
/**
* Automatically send MDS Collivery the delivery request when payment gateways call $order->payment_complete()
*
* @param $order_id
* @return string
*/
function automated_add_collivery_payment_complete($order_id)
{
/** @var \MdsSupportingClasses\MdsColliveryService $mds */
$mds = MdsColliveryService::getInstance();
$settings = $mds->returnPluginSettings();
if ($settings['enabled'] == 'yes' && $settings["toggle_automatic_mds_processing"] == 'yes') {
$mds->automatedAddCollivery($order_id);
}
}
add_action('woocommerce_payment_complete', 'automated_add_collivery_payment_complete');
/**
* Automatically send MDS Collivery the delivery request when status changes to processing for cod, eft's and cheque
*
* @param $order_id
* @return string
*/
function automated_add_collivery_status_processing($order_id)
{
/** @var \MdsSupportingClasses\MdsColliveryService $mds */
$mds = MdsColliveryService::getInstance();
$settings = $mds->returnPluginSettings();
if ($settings['enabled'] == 'yes' && $settings["toggle_automatic_mds_processing"] == 'yes') {
$order = new WC_Order($order_id);
if($order->payment_method === "cod" || $order->payment_method === "cheque" || $order->payment_method === "bacs"){
$mds->automatedAddCollivery($order_id, true);
}
}
}
add_action( 'woocommerce_order_status_processing', 'automated_add_collivery_status_processing' );
}