1
|
|
|
<?php if ( !defined( 'ABSPATH' ) ) exit;
|
2
|
|
|
class wps_shipping {
|
3
|
|
|
function __construct() {
|
|
|
|
|
4
|
|
|
|
5
|
|
|
}
|
6
|
|
|
|
7
|
|
|
/**
|
8
|
|
|
* Convert fees (string format) to fees in array format
|
9
|
|
|
* @param $fees_string : fees string type by the user
|
10
|
|
|
* @return $data : fees in array format
|
|
|
|
|
11
|
|
|
*/
|
12
|
|
|
function shipping_fees_string_2_array($fees_string) {
|
|
|
|
|
13
|
|
|
$data = array();
|
14
|
|
|
if(!empty($fees_string) && !is_array($fees_string) ) {
|
15
|
|
|
if(preg_match_all('#{[^{]+}#', $fees_string, $cont)){
|
16
|
|
|
foreach($cont[0] as $c) {
|
17
|
|
|
preg_match_all('#([a-z]+) ?: ?"(.+)"#', $c, $atts);
|
18
|
|
|
$temp_data = array();
|
19
|
|
|
$country_code = '';
|
20
|
|
|
foreach($atts[1] as $key => $value) {
|
21
|
|
|
$temp_data[$value] = $atts[2][$key];
|
22
|
|
|
if($value=='destination') {
|
23
|
|
|
$country_code = $atts[2][$key];
|
24
|
|
|
}
|
25
|
|
|
elseif($value=='fees') {
|
26
|
|
|
$fees_data = array();
|
27
|
|
|
$fees = explode(',', $atts[2][$key]);
|
28
|
|
|
foreach($fees as $fee){
|
29
|
|
|
$fee_element = explode(':', $fee);
|
30
|
|
|
$fees_data[trim($fee_element[0])] = trim($fee_element[1]);
|
31
|
|
|
}
|
32
|
|
|
$number = count($fees_data);
|
33
|
|
|
|
34
|
|
|
$fees_data_1 = array();
|
35
|
|
|
preg_match_all('#([0-9]+\.?[0-9]?+) ?: ?([0-9]+\.?[0-9]?+)#', $atts[2][$key], $fees);
|
36
|
|
|
foreach($fees[1] as $_key => $_value) {
|
37
|
|
|
$fees_data_1[$_value] = $fees[2][$_key];
|
38
|
|
|
}
|
39
|
|
|
$number_1 = count($fees_data_1);
|
40
|
|
|
if ($number == $number_1) {
|
41
|
|
|
$temp_data[$value] = $fees_data;
|
42
|
|
|
}
|
43
|
|
|
else {
|
44
|
|
|
$temp_data[$value] = $fees_data_1;
|
45
|
|
|
}
|
46
|
|
|
}
|
47
|
|
|
}
|
48
|
|
|
if(!empty($country_code)) {
|
49
|
|
|
$data[$country_code] = $temp_data;
|
50
|
|
|
}
|
51
|
|
|
}
|
52
|
|
|
}
|
53
|
|
|
return $data;
|
54
|
|
|
}
|
55
|
|
|
return array();
|
56
|
|
|
}
|
57
|
|
|
|
58
|
|
|
/**
|
59
|
|
|
* Convert fees (array format) to fees in string format
|
60
|
|
|
* @param $fees_array : fees in array format
|
61
|
|
|
* @return $string : fees in string format
|
|
|
|
|
62
|
|
|
*/
|
63
|
|
|
function shipping_fees_array_2_string($fees_array) {
|
|
|
|
|
64
|
|
|
$string = '';
|
65
|
|
|
if(!empty($fees_array)) {
|
66
|
|
|
foreach($fees_array as $d) {
|
67
|
|
|
$string .= '{'."\n";
|
68
|
|
|
foreach($d as $att => $value) {
|
69
|
|
|
$val = '';
|
70
|
|
|
if($att=='fees') {
|
71
|
|
|
foreach($value as $_k=>$_value) $val .= $_k.':'.$_value.', ';
|
72
|
|
|
$val = substr($val,0,-2);
|
73
|
|
|
} else $val = $value;
|
74
|
|
|
$string .= $att.': "'.$val.'",'."\n";
|
75
|
|
|
}
|
76
|
|
|
$string = substr($string,0,-2)."\n";
|
77
|
|
|
$string .= '},'."\n";
|
78
|
|
|
}
|
79
|
|
|
$string = substr($string,0,-2);
|
80
|
|
|
return $string;
|
81
|
|
|
}
|
82
|
|
|
else return false;
|
83
|
|
|
}
|
84
|
|
|
|
85
|
|
|
/**
|
86
|
|
|
* Get the shipping cost for the current cart
|
87
|
|
|
*
|
88
|
|
|
* @param integer $nb_of_items The number of items in cart
|
89
|
|
|
* @param float $total_cart The amount of the cart
|
90
|
|
|
* @param float $total_shipping_cost The amount of the shipping cost calculate from the sum of shipping cost for each product in cart
|
91
|
|
|
* @param float $total_weight The total weight of all product in cart
|
92
|
|
|
*
|
93
|
|
|
* @return number|string The sipping cost for the current cart
|
94
|
|
|
*/
|
95
|
|
|
function get_shipping_cost( $nb_of_items, $total_cart, $total_shipping_cost, $total_weight, $selected_method = '' ) {
|
|
|
|
|
96
|
|
|
global $wpdb;
|
97
|
|
|
|
98
|
|
|
$shipping_cost = 0;
|
99
|
|
|
|
100
|
|
|
$shipping_mode_state = get_option( 'wpshop_shipping_address_choice' );
|
101
|
|
|
if ( ! empty( $shipping_mode_state ) && isset( $shipping_mode_state['activate'] ) && ( 'on' === $shipping_mode_state['activate'] ) ) {
|
102
|
|
|
$shipping_mode_option = get_option( 'wps_shipping_mode' );
|
103
|
|
|
if( ( !empty($selected_method) || isset( $_SESSION['shipping_address'] ) && empty( $_SESSION['shipping_method'] ) ) ) {
|
104
|
|
|
if( !empty($selected_method) ) {
|
105
|
|
|
$chosen_shipping_mode = $selected_method;
|
106
|
|
|
} else {
|
107
|
|
|
$ship_mod = new wps_shipping_mode_ctr();
|
108
|
|
|
$shipping_modes = $ship_mod->get_shipping_mode_for_address( $_SESSION['shipping_address'] );
|
109
|
|
|
if( !empty( $shipping_modes ) ) {
|
110
|
|
|
$shipping_modes = $shipping_modes['modes'];
|
111
|
|
|
foreach( $shipping_modes as $key => $shipping_mode ) {
|
112
|
|
|
$chosen_shipping_mode = $key;
|
113
|
|
|
break;
|
114
|
|
|
}
|
115
|
|
|
} else {
|
116
|
|
|
$chosen_shipping_mode = 'default_choice';
|
117
|
|
|
}
|
118
|
|
|
}
|
119
|
|
|
}
|
120
|
|
|
else {
|
121
|
|
|
if( !empty( $_SESSION['shipping_method'] ) ) {
|
122
|
|
|
$chosen_shipping_mode = wpshop_tools::varSanitizer( $_SESSION['shipping_method'] );
|
123
|
|
|
} else {
|
124
|
|
|
$chosen_shipping_mode = 'default_choice';
|
125
|
|
|
}
|
126
|
|
|
}
|
127
|
|
|
|
128
|
|
|
$default_weight_unity = get_option( 'wpshop_shop_default_weight_unity' );
|
129
|
|
|
if ( !empty($default_weight_unity) ) {
|
130
|
|
|
$query = $wpdb->prepare('SELECT unit FROM ' .WPSHOP_DBT_ATTRIBUTE_UNIT. ' WHERE id = %d', $default_weight_unity);
|
131
|
|
|
$weight_unity = $wpdb->get_var( $query );
|
132
|
|
|
|
133
|
|
|
if ( !empty($weight_unity) && $weight_unity == 'kg' ) {
|
134
|
|
|
|
135
|
|
|
$total_weight = $total_weight * 1000;
|
136
|
|
|
}
|
137
|
|
|
}
|
138
|
|
|
|
139
|
|
|
if ( ( !empty($_SESSION['shipping_method']) && $_SESSION['shipping_method'] == 'shipping-partners' ) || !empty( $_SESSION['wps-pos-addon']) ) {
|
140
|
|
|
return 0;
|
141
|
|
|
}
|
142
|
|
|
|
143
|
|
|
/** Take the selected shipping mode **/
|
144
|
|
|
if( $chosen_shipping_mode == 'default_choice' ) {
|
145
|
|
View Code Duplication |
if( !empty( $shipping_mode_option['modes'][ $shipping_mode_option['default_choice'] ]) ) {
|
|
|
|
|
146
|
|
|
$selected_shipping_mode_config = $shipping_mode_option['modes'][ $shipping_mode_option['default_choice'] ];
|
147
|
|
|
} else {
|
148
|
|
|
$selected_shipping_mode_config = '';
|
149
|
|
|
}
|
150
|
|
View Code Duplication |
} else {
|
|
|
|
|
151
|
|
|
if( !empty( $shipping_mode_option['modes'][$chosen_shipping_mode]) ) {
|
152
|
|
|
$selected_shipping_mode_config = $shipping_mode_option['modes'][$chosen_shipping_mode];
|
|
|
|
|
153
|
|
|
} else {
|
154
|
|
|
$selected_shipping_mode_config = '';
|
155
|
|
|
}
|
156
|
|
|
}
|
157
|
|
|
$shipping_cost = $total_shipping_cost;
|
158
|
|
|
|
159
|
|
|
/** Free Shipping **/
|
160
|
|
|
if ( ( !empty($selected_shipping_mode_config) && !empty($selected_shipping_mode_config['free_shipping']) ) || ( $selected_method == 'is_downloadable_' ) ) {
|
161
|
|
|
$shipping_cost = 0;
|
162
|
|
|
}
|
163
|
|
|
/** Free Shipping From **/
|
164
|
|
|
elseif( !empty($selected_shipping_mode_config) && !empty($selected_shipping_mode_config['free_from']) && $selected_shipping_mode_config['free_from'] >= 0 && $selected_shipping_mode_config['free_from'] <= number_format( $total_cart, 2, '.', '') ) {
|
165
|
|
|
$shipping_cost = 0;
|
166
|
|
|
}
|
167
|
|
|
else {
|
168
|
|
|
/** Check Custom Shipping Cost **/
|
169
|
|
|
|
170
|
|
|
if ( !empty($selected_shipping_mode_config['custom_shipping_rules']) && !empty($selected_shipping_mode_config['custom_shipping_rules']['active']) ) {
|
171
|
|
|
$address_infos = '';
|
172
|
|
|
if( !empty( $_SESSION['shipping_address'] ) ) {
|
173
|
|
|
$address_infos = get_post_meta($_SESSION['shipping_address'],'_wpshop_address_metadata', true);
|
174
|
|
|
}
|
175
|
|
|
$country = ( !empty($address_infos['country']) ) ? $address_infos['country'] : '';
|
176
|
|
|
/** Check Active Postcode option **/
|
177
|
|
|
if ( !empty($selected_shipping_mode_config['custom_shipping_rules']['active_cp']) ) {
|
178
|
|
|
$postcode = $address_infos['postcode'];
|
179
|
|
|
if ( array_key_exists($country.'-'.$postcode, $selected_shipping_mode_config['custom_shipping_rules']['fees']) ) {
|
180
|
|
|
$country = $country.'-'.$postcode;
|
181
|
|
|
}
|
182
|
|
|
elseif( array_key_exists($country.'-OTHERS', $selected_shipping_mode_config['custom_shipping_rules']['fees']) ) {
|
183
|
|
|
$country = $country.'-OTHERS';
|
184
|
|
|
}
|
185
|
|
|
}
|
186
|
|
|
$shipping_cost += $this->calculate_custom_shipping_cost($country, array('weight'=>$total_weight,'price'=> $total_cart), $selected_shipping_mode_config['custom_shipping_rules']['fees'], $chosen_shipping_mode);
|
|
|
|
|
187
|
|
|
}
|
188
|
|
|
|
189
|
|
|
/** Min- Max config **/
|
190
|
|
|
if ( !empty($selected_shipping_mode_config['min_max']) && !empty($selected_shipping_mode_config['min_max']['activate']) ) {
|
191
|
|
|
if ( !empty($selected_shipping_mode_config['min_max']['min']) && $shipping_cost < $selected_shipping_mode_config['min_max']['min'] ) {
|
192
|
|
|
$shipping_cost = $selected_shipping_mode_config['min_max']['min'];
|
193
|
|
|
}
|
194
|
|
|
elseif( !empty($selected_shipping_mode_config['min_max']['max']) &&$shipping_cost > $selected_shipping_mode_config['min_max']['max']) {
|
195
|
|
|
$shipping_cost = $selected_shipping_mode_config['min_max']['max'];
|
196
|
|
|
}
|
197
|
|
|
|
198
|
|
|
}
|
199
|
|
|
}
|
200
|
|
|
}
|
201
|
|
|
return $shipping_cost;
|
202
|
|
|
}
|
203
|
|
|
|
204
|
|
|
|
205
|
|
|
/**
|
206
|
|
|
* Return s custom shipping cost for a cart
|
207
|
|
|
* @param string $dest Shipping area zone
|
208
|
|
|
* @param array $data
|
209
|
|
|
* @param array $fees
|
210
|
|
|
* @return number|boolean|Ambigous <number, unknown>
|
211
|
|
|
*/
|
212
|
|
|
function calculate_custom_shipping_cost($dest='', $data, $fees, $shipping_method = false) {
|
|
|
|
|
213
|
|
|
$fees_table = array();
|
|
|
|
|
214
|
|
|
$key = ''; $price = 0;
|
215
|
|
|
|
216
|
|
|
if ( !empty($_SESSION['shipping_partner_id']) ) {
|
217
|
|
|
return 0;
|
218
|
|
|
}
|
219
|
|
|
|
220
|
|
|
if($shipping_method === false) {
|
221
|
|
|
$shipping_method = $_SESSION['shipping_method'];
|
222
|
|
|
}
|
223
|
|
|
|
224
|
|
|
if(!empty($fees) || !empty($dest) ) {
|
225
|
|
|
$custom_shipping_option = get_option( 'wpshop_custom_shipping', true );
|
|
|
|
|
226
|
|
|
$shipping_modes = get_option( 'wps_shipping_mode' );
|
227
|
|
|
|
228
|
|
|
if ( !empty($shipping_method) ) {
|
229
|
|
|
if ( !empty($shipping_modes) && !empty($shipping_modes['modes']) && !empty($shipping_modes['modes'][ $shipping_method ]) ) {
|
230
|
|
|
$custom_shipping_option = $shipping_modes['modes'][ $shipping_method ]['custom_shipping_rules'];
|
231
|
|
|
}
|
232
|
|
|
}
|
233
|
|
|
$found_active_cp_rule = $found_active_departement_rule = false;
|
234
|
|
|
$shipping_address_def = get_post_meta( $_SESSION['shipping_address'], '_wpshop_address_metadata', true );
|
235
|
|
|
$postcode = '';
|
236
|
|
|
if ( !empty($shipping_address_def) ) {
|
237
|
|
|
$postcode = $shipping_address_def['postcode'];
|
238
|
|
|
}
|
239
|
|
|
|
240
|
|
|
/** Search Postcode custom fees **/
|
241
|
|
|
if ( !empty($custom_shipping_option) && !empty($custom_shipping_option['active_cp']) ) {
|
242
|
|
|
$key = $dest.'-'.$postcode;
|
243
|
|
View Code Duplication |
if ( array_key_exists($key, $fees) ) {
|
|
|
|
|
244
|
|
|
foreach ($fees[$key]['fees'] as $k => $shipping_price) {
|
245
|
|
|
if ( $data['weight'] <= $k) {
|
246
|
|
|
break;
|
247
|
|
|
}
|
248
|
|
|
}
|
249
|
|
|
$found_active_cp_rule = true;
|
250
|
|
|
}
|
251
|
|
|
}
|
252
|
|
|
|
253
|
|
|
/** Search Department custom fees **/
|
254
|
|
|
if( !empty($custom_shipping_option) && !empty($custom_shipping_option['active_department']) && !$found_active_cp_rule ) {
|
255
|
|
|
$department = substr( $postcode, 0,2 );
|
256
|
|
|
$key = $dest.'-'.$department;
|
257
|
|
|
/** Check if a rule exists **/
|
258
|
|
View Code Duplication |
if ( array_key_exists($key, $fees) ) {
|
|
|
|
|
259
|
|
|
foreach ($fees[$key]['fees'] as $k => $shipping_price) {
|
260
|
|
|
if ( $data['weight'] <= $k) {
|
261
|
|
|
break;
|
262
|
|
|
}
|
263
|
|
|
}
|
264
|
|
|
$found_active_departement_rule = true;
|
265
|
|
|
}
|
266
|
|
|
}
|
267
|
|
|
|
268
|
|
|
/** Search general custom fees **/
|
269
|
|
|
if( !$found_active_cp_rule && !$found_active_departement_rule ){
|
270
|
|
|
if ( array_key_exists($dest, $fees) ) {
|
271
|
|
|
$key = $dest;
|
272
|
|
|
}
|
273
|
|
|
elseif( array_key_exists( 'OTHERS', $fees) ) {
|
274
|
|
|
$key = 'OTHERS';
|
275
|
|
|
}
|
276
|
|
|
else {
|
277
|
|
|
return false;
|
278
|
|
|
}
|
279
|
|
|
}
|
280
|
|
|
|
281
|
|
|
//Search fees
|
282
|
|
|
if ( !empty($key) ) {
|
283
|
|
|
ksort( $fees[$key]['fees'] );
|
284
|
|
|
foreach ($fees[$key]['fees'] as $k => $shipping_price) {
|
285
|
|
|
if ( $data['weight'] <= $k) {
|
286
|
|
|
$price = $shipping_price;
|
287
|
|
|
break;
|
288
|
|
|
}
|
289
|
|
|
if ($shipping_price === end($fees[$key]['fees'])) {
|
290
|
|
|
$price = $shipping_price;
|
291
|
|
|
}
|
292
|
|
|
}
|
293
|
|
|
}
|
294
|
|
|
else {
|
295
|
|
|
return false;
|
296
|
|
|
}
|
297
|
|
|
return $price;
|
298
|
|
|
}
|
299
|
|
|
return false;
|
300
|
|
|
}
|
301
|
|
|
|
302
|
|
|
/**
|
303
|
|
|
* Return Amount of Shipping Cost for Cart items
|
304
|
|
|
* @param array $cart_items
|
305
|
|
|
* @return number
|
306
|
|
|
*/
|
307
|
|
|
function calcul_cart_items_shipping_cost( $cart_items ) {
|
|
|
|
|
308
|
|
|
$shipping_cost = 0;
|
309
|
|
|
if( !empty($cart_items) ) {
|
310
|
|
|
foreach( $cart_items as $cart_item ) {
|
311
|
|
|
$product_data = get_post_meta( $cart_item['item_id'], '_wpshop_product_metadata', true );
|
312
|
|
|
|
313
|
|
|
if ( !empty( $product_data ) && !empty( $product_data['cost_of_postage'] ) ) {
|
314
|
|
|
$shipping_cost += ( $product_data['cost_of_postage'] * $cart_item['item_qty'] );
|
315
|
|
|
}
|
316
|
|
|
}
|
317
|
|
|
}
|
318
|
|
|
|
319
|
|
|
return $shipping_cost;
|
320
|
|
|
}
|
321
|
|
|
|
322
|
|
|
/**
|
323
|
|
|
* Return the cart total weight
|
324
|
|
|
* @param array $cart_items
|
325
|
|
|
* @return number
|
326
|
|
|
*/
|
327
|
|
|
function calcul_cart_weight( $cart_items ) {
|
|
|
|
|
328
|
|
|
$cart_weight = 0;
|
329
|
|
|
if ( !empty( $cart_items) ) {
|
330
|
|
|
foreach( $cart_items as $id_item => $cart_item ) {
|
331
|
|
|
$id_item = wpshop_products::get_id_variation($id_item);
|
332
|
|
|
if ( get_post_type($id_item) == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION ) {
|
333
|
|
|
$product_data = get_post_meta( $id_item, '_wpshop_product_metadata', true );
|
334
|
|
|
if ( !empty($product_data) && !empty($product_data['product_weight']) ) {
|
335
|
|
|
$cart_weight += ( $product_data['product_weight'] * $cart_item['item_qty'] );
|
336
|
|
|
}
|
337
|
|
View Code Duplication |
else {
|
|
|
|
|
338
|
|
|
$parent_def = wpshop_products::get_parent_variation( $id_item );
|
339
|
|
|
if ( !empty($parent_def) && !empty( $parent_def['parent_post_meta']) && !empty($parent_def['parent_post_meta']['product_weight']) ) {
|
340
|
|
|
$cart_weight += ( $parent_def['parent_post_meta']['product_weight'] * $cart_item['item_qty'] );
|
341
|
|
|
}
|
342
|
|
|
}
|
343
|
|
|
}
|
344
|
|
View Code Duplication |
else {
|
|
|
|
|
345
|
|
|
$product_data = get_post_meta( $cart_item['item_id'], '_wpshop_product_metadata', true );
|
346
|
|
|
if ( !empty($product_data) && !empty($product_data['product_weight']) && !empty($cart_item['item_qty']) ) {
|
347
|
|
|
$cart_weight += ( $product_data['product_weight'] * $cart_item['item_qty'] );
|
348
|
|
|
}
|
349
|
|
|
}
|
350
|
|
|
}
|
351
|
|
|
}
|
352
|
|
|
$cart_weight = apply_filters( 'extra_cart_weight_calcul', $cart_weight, $cart_items );
|
353
|
|
|
return $cart_weight;
|
354
|
|
|
}
|
355
|
|
|
|
356
|
|
|
|
357
|
|
|
}
|
358
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.