| 1 |
<?php |
| 2 |
|
| 3 |
namespace MakeCommerce\Shipping\Method; |
| 4 |
|
| 5 |
/** |
| 6 |
* Smartpost parcelmachine shipping method. |
| 7 |
* Defines all Smartpost specific options. |
| 8 |
* |
| 9 |
* @since 3.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
use \MakeCommerce\Shipping\Method\ParcelMachine\Map; |
| 13 |
|
| 14 |
abstract class ParcelMachine extends \MakeCommerce\Shipping\Method { |
| 15 |
|
| 16 |
use Map; |
| 17 |
|
| 18 |
public $type = "apt"; |
| 19 |
public $identifier = "parcelmachine"; |
| 20 |
|
| 21 |
/** |
| 22 |
* Required function for all methods, initializes method type |
| 23 |
* |
| 24 |
* @since 3.0.0 |
| 25 |
*/ |
| 26 |
public function initialize() { |
| 27 |
|
| 28 |
$this->check_updates(); |
| 29 |
|
| 30 |
//load all hooks/filters for method type |
| 31 |
$this->set_method_type_hooks(); |
| 32 |
|
| 33 |
//set parcelmachine specific properties |
| 34 |
$this->prioritization = null; |
| 35 |
if ( isset( $this->settings['prioritization'] ) ) { |
| 36 |
$this->prioritization = $this->settings['prioritization']; |
| 37 |
} |
| 38 |
|
| 39 |
$this->short_office_names = null; |
| 40 |
if ( isset( $this->settings['short_office_names'] ) ) { |
| 41 |
$this->short_office_names = $this->settings['short_office_names']; |
| 42 |
} |
| 43 |
|
| 44 |
$this->use_white_apts = null; |
| 45 |
if ( isset( $this->settings['use_white_apts'] ) ) { |
| 46 |
$this->use_white_apts = $this->settings['use_white_apts']; |
| 47 |
} |
| 48 |
|
| 49 |
$this->searchable_parcelmachines = null; |
| 50 |
if ( isset( $this->settings['searchable_parcelmachines'] ) ) { |
| 51 |
$this->searchable_parcelmachines = $this->settings['searchable_parcelmachines']; |
| 52 |
} |
| 53 |
|
| 54 |
$this->mk_lpexpress_template = null; |
| 55 |
if ( isset( $this->settings['mk_lpexpress_template'] ) ) { |
| 56 |
$this->mk_lpexpress_template = $this->settings['mk_lpexpress_template']; |
| 57 |
if ( get_option( 'mk_lpexpress_template' ) === false ) { |
| 58 |
add_option( 'mk_lpexpress_template', $this->settings['mk_lpexpress_template'] ); |
| 59 |
} else { |
| 60 |
update_option( 'mk_lpexpress_template', $this->settings['mk_lpexpress_template'] ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
//needed for identifier translation |
| 65 |
$parcelmachineString = __( "parcelmachine", 'wc_makecommerce_domain' ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get method title |
| 70 |
* |
| 71 |
* @since 3.0.0 |
| 72 |
*/ |
| 73 |
public function return_method_title() { |
| 74 |
|
| 75 |
return $this->carrier_title . " " . __( "parcel machine (MC)", 'wc_makecommerce_domain' ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Initialize method type specific checkout fields |
| 80 |
* |
| 81 |
* @since 3.0.0 |
| 82 |
*/ |
| 83 |
public function initialize_method_type_checkout_fields() { |
| 84 |
|
| 85 |
$this->form_fields['prioritization'] = array( |
| 86 |
'title' => __('Prioritize', 'wc_makecommerce_domain'), |
| 87 |
'type' => 'checkbox', |
| 88 |
'label' => __('Bigger cities will be on top of list, others sorted alphabetically', 'wc_makecommerce_domain'), |
| 89 |
'default' => 'yes' |
| 90 |
); |
| 91 |
|
| 92 |
$this->form_fields['short_office_names'] = array( |
| 93 |
'title' => __('Short names', 'wc_makecommerce_domain'), |
| 94 |
'type' => 'checkbox', |
| 95 |
'label' => __('Display only parcel machine names, without addresses', 'wc_makecommerce_domain'), |
| 96 |
'default' => 'no' |
| 97 |
); |
| 98 |
|
| 99 |
$this->form_fields['searchable_parcelmachines'] = array( |
| 100 |
'title' => __('Search', 'wc_makecommerce_domain'), |
| 101 |
'type' => 'checkbox', |
| 102 |
'label' => __('Make parcel machine selection searchable', 'wc_makecommerce_domain'), |
| 103 |
'default' => 'no' |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Set method type specific hooks/filters |
| 109 |
* |
| 110 |
* @since 3.0.0 |
| 111 |
*/ |
| 112 |
public function set_method_type_hooks() { |
| 113 |
|
| 114 |
//add parcelmachines selectbox to checkout |
| 115 |
add_filter( 'woocommerce_review_order_after_shipping' , array( $this, 'add_parcelmachine_checkout_fields' ) ); |
| 116 |
|
| 117 |
//add shipping metadata to order |
| 118 |
add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'add_parcelmachine_order_meta' ) ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Set parcelmachine specific checkout fields |
| 123 |
* |
| 124 |
* @since 3.0.0 |
| 125 |
*/ |
| 126 |
public function add_parcelmachine_checkout_fields() { |
| 127 |
|
| 128 |
$this->order_country = WC()->customer->get_shipping_country(); |
| 129 |
|
| 130 |
//get all parcelmachines |
| 131 |
$aptopts = [ 'use_white_apts' => $this->use_white_apts ]; |
| 132 |
$machines = \MakeCommerce\Shipping::mk_get_machines( $this->ext, $this->order_country, $aptopts ); |
| 133 |
|
| 134 |
//sort machines |
| 135 |
$machines = $this->sort_machines( $machines ); |
| 136 |
|
| 137 |
// Create new class and placeholder for searchable parcel machines |
| 138 |
$placeholder = __( '-- select parcel machine --', 'wc_makecommerce_domain' ); |
| 139 |
$searchable_class = ''; |
| 140 |
if ( $this->searchable_parcelmachines === 'yes' ) { |
| 141 |
$searchable_class = ' parcel-machine-select-box-searchable'; |
| 142 |
$placeholder = ''; |
| 143 |
} |
| 144 |
|
| 145 |
$html = ' |
| 146 |
<tr style="display: none;" class="parcel_machine_checkout parcel_machine_checkout_parcelmachine_' . mb_strtolower( $this->ext ) . '"> |
| 147 |
<td colspan="2"> |
| 148 |
<p class="form-row" id="' . esc_attr( $this->id ) . '_field"> |
| 149 |
<select class="select parcel-machine-select-box' . $searchable_class . '" name="' . esc_attr( $this->id ) . '" id="' . esc_attr( $this->id ) . '"> |
| 150 |
<option value="">' . $placeholder . '</option> |
| 151 |
'; |
| 152 |
|
| 153 |
$html .= $this->create_parcelmachine_html( $machines, $this->short_office_names ); |
| 154 |
|
| 155 |
$html .= ' |
| 156 |
</select> |
| 157 |
</p>'; |
| 158 |
|
| 159 |
if ( self::map_enabled() ) { |
| 160 |
$html .= ' |
| 161 |
<div class="mc_pmmap_choose_button"> |
| 162 |
<img class="mc_pmmap_pin" src="' . plugin_dir_url(__FILE__) . 'images/pin.svg' . '"> |
| 163 |
<p>' . __( 'Choose machine from map', 'wc_makecommerce_domain' ) . '</p> |
| 164 |
</div> |
| 165 |
'; |
| 166 |
} |
| 167 |
|
| 168 |
$html .= ' |
| 169 |
</td> |
| 170 |
</tr> |
| 171 |
'; |
| 172 |
|
| 173 |
echo $html; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Creates html for displaying parcel machines |
| 178 |
* |
| 179 |
* @since 3.0.15 |
| 180 |
*/ |
| 181 |
public static function create_parcelmachine_html( $machines, $short_names = 'no', $selected_machine_id = null ) { |
| 182 |
|
| 183 |
$output = ''; |
| 184 |
|
| 185 |
//group machines by city and set name |
| 186 |
$city_machines = array(); |
| 187 |
foreach ( $machines as $machine ) { |
| 188 |
if ( $short_names !== 'yes' ) { |
| 189 |
// Original name for map |
| 190 |
$machine["original_name"] = $machine["name"]; |
| 191 |
$machine["name"] .= ' - ' . $machine['city'] . ', ' . $machine['address']; |
| 192 |
} |
| 193 |
|
| 194 |
$city_machines[$machine["city"]][] = $machine; |
| 195 |
} |
| 196 |
|
| 197 |
foreach ( $city_machines as $city=>$grouped_machines ) { |
| 198 |
$output .= '<optgroup label="' . $city . '">'; |
| 199 |
|
| 200 |
foreach ( $grouped_machines as $machine ) { |
| 201 |
if ( isset($machine['x']) && isset($machine['y'])) { |
| 202 |
$output .= '<option'; |
| 203 |
if ( isset( $machine["original_name"] ) ) { |
| 204 |
$output .= ' name="'.esc_attr( $machine['original_name'] ).'"'; |
| 205 |
} else { |
| 206 |
$output .= ' name="'.esc_attr( $machine['name'] ).'"'; |
| 207 |
} |
| 208 |
$output .= ' x="'.esc_attr( $machine['x'] ).'"' |
| 209 |
.' y="'.esc_attr( $machine['y'] ).'"' |
| 210 |
.' carrier="'.esc_attr( $machine['carrier'] ).'"' |
| 211 |
.' city="'.esc_attr( $machine['city'] ).'"' |
| 212 |
.' address="'.esc_attr( $machine['address'] ).'"' |
| 213 |
.' zip="'.esc_attr( $machine['zip'] ).'"' |
| 214 |
.' commentet="'.esc_attr( $machine['commentEt'] ).'"' |
| 215 |
.' commentlv="'.esc_attr( $machine['commentLv'] ).'"' |
| 216 |
.' commentlt="'.esc_attr( $machine['commentLt'] ).'"' |
| 217 |
.' commentfi="'.esc_attr( $machine['commentFi'] ).'"' |
| 218 |
.' availability="'.esc_attr( $machine['availability'] ).'"' |
| 219 |
.' value="'.esc_attr( $machine['carrier'].'||'.$machine['id'] ).'" '.( $selected_machine_id === $machine['id'] ? ' selected="selected"' : '' ).'>'.$machine["name"].'</option>'; |
| 220 |
|
| 221 |
} else { |
| 222 |
$output .= '<option value="'.esc_attr( $machine['carrier'].'||'.$machine['id'] ).'" '.( $selected_machine_id === $machine['id'] ? ' selected="selected"' : '' ).'>'.$machine["name"].'</option>'; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
$output .= '</optgroup>'; |
| 227 |
} |
| 228 |
|
| 229 |
return $output; |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Adds metadata to order about shipping |
| 234 |
* |
| 235 |
* @since 3.0.0 |
| 236 |
*/ |
| 237 |
public function add_parcelmachine_order_meta( $order_id ) { |
| 238 |
|
| 239 |
$shipping_method = !empty( $_POST['shipping_method'] ) ? $_POST['shipping_method'] : false; |
| 240 |
|
| 241 |
if ( !empty($shipping_method[0] ) ) { |
| 242 |
$shipping_method_ext = explode( ':', $shipping_method[0] ); |
| 243 |
} |
| 244 |
|
| 245 |
if ( $shipping_method_ext[0] === $this->id && !empty( $_POST[$this->id] ) ) { |
| 246 |
|
| 247 |
\MakeCommerce\Shipping::update_order_parcelmachine_meta( $_POST[$this->id], $order_id ); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Convert old methods to shipping zones if current site wc_mc_version is older than one of this module |
| 253 |
* |
| 254 |
* @since 3.0.0 |
| 255 |
*/ |
| 256 |
private function check_updates() { |
| 257 |
|
| 258 |
if ( get_site_option('wc_mc_version', 0) < WC_MC_VERSION) { |
| 259 |
|
| 260 |
update_site_option('wc_mc_version', WC_MC_VERSION); |
| 261 |
|
| 262 |
error_log("Need to update from [" . get_site_option( 'wc_mc_version', 0 ) . "] to [" . WC_MC_VERSION . "]"); |
| 263 |
|
| 264 |
if ( WC_MC_VERSION === 2.0 ) { |
| 265 |
|
| 266 |
// Check if omniva / smartpost is enabled and in which countries |
| 267 |
// Convert to shipping zone |
| 268 |
foreach ( array( |
| 269 |
'parcelmachine_omniva' => 'MakeCommerce\Shipping\Method\ParcelMachine\Omniva', |
| 270 |
'parcelmachine_smartpost' => 'MakeCommerce\Shipping\Method\ParcelMachine\Smartpost', |
| 271 |
'parcelmachine_dpd' => 'MakeCommerce\Shipping\Method\ParcelMachine\DPD', |
| 272 |
'parcelmachine_lp_express_lt' => 'MakeCommerce\Shipping\Method\ParcelMachine\LPExpress' |
| 273 |
|
| 274 |
) as $method_name => $method_class ) { |
| 275 |
|
| 276 |
$parcel_machine_method = new $method_class(); |
| 277 |
|
| 278 |
if ( $parcel_machine_method->enable === 'yes' ) { |
| 279 |
|
| 280 |
$zones = \WC_Shipping_Zones::get_zones(); |
| 281 |
|
| 282 |
//skip if there were no countries selected |
| 283 |
if ( !empty( $parcel_machine_method->countries ) ) { |
| 284 |
continue; |
| 285 |
} |
| 286 |
|
| 287 |
foreach ( $parcel_machine_method->countries as $ecountry ) { |
| 288 |
|
| 289 |
$has_ecountry = false; |
| 290 |
|
| 291 |
foreach ( $zones as $zone ) { |
| 292 |
foreach ( $zone['zone_locations'] as $location ) { |
| 293 |
|
| 294 |
if ( $location->code === $ecountry ) { |
| 295 |
|
| 296 |
$has_ecountry = $zone['zone_id']; |
| 297 |
|
| 298 |
//skip complete class if a shippingzone with this name has alread been defined |
| 299 |
foreach ( $zone['shipping_methods'] as $method ) { |
| 300 |
if ( $method->id === $method_name ) { |
| 301 |
|
| 302 |
continue 4; |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
//convert to shipping zones |
| 310 |
if ( !$has_ecountry ) { |
| 311 |
|
| 312 |
$zone = new \WC_Shipping_Zone(); |
| 313 |
$zone->set_zone_name( $ecountry ); |
| 314 |
$zone->add_location( $ecountry, 'country' ); |
| 315 |
|
| 316 |
$zone->save(); |
| 317 |
} else { |
| 318 |
$zone = new \WC_Shipping_Zone( $has_ecountry ); |
| 319 |
} |
| 320 |
|
| 321 |
$instance_id = $zone->add_shipping_method($method_name); |
| 322 |
$transport_class = new $method_class($instance_id); |
| 323 |
|
| 324 |
$price = !empty( $transport_class->settings['price_'.strtolower($ecountry)] ) ? $transport_class->settings['price_'.strtolower($ecountry)] : 0; |
| 325 |
|
| 326 |
$free_shipping_min_amount = !empty( $transport_class->settings['free_shipping_min_amount_'.strtolower( $ecountry )] ) ? $transport_class->settings['free_shipping_min_amount_'.strtolower($ecountry)] : 0; |
| 327 |
$transport_class->set_post_data( array( $transport_class->get_field_key( 'price' ) => $price, $transport_class->get_field_key( 'free_shipping_min_amount' ) => $free_shipping_min_amount ) ); |
| 328 |
|
| 329 |
$transport_class->process_admin_options(); |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Checks if there is a product that doesn't fit in parcelmachine |
| 339 |
* |
| 340 |
* @since 3.0.0 |
| 341 |
*/ |
| 342 |
private function fits_parcel_machine( $package ) { |
| 343 |
|
| 344 |
foreach ( $package['contents'] as $line ) { |
| 345 |
|
| 346 |
if ( get_post_meta( $line['product_id'], '_no_parcel_machine', true ) === 'yes' ) { |
| 347 |
return false; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
return true; |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Checks if shipping method is available. |
| 356 |
* In addition to parent is_available, also checks if package fits in parcel machine |
| 357 |
* |
| 358 |
* @since 3.0.0 |
| 359 |
*/ |
| 360 |
public function is_available( $package ) { |
| 361 |
|
| 362 |
if ( !$this->fits_parcel_machine( $package ) ) { |
| 363 |
return false; |
| 364 |
} |
| 365 |
|
| 366 |
return parent::is_available( $package ); |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* Sort machines (biggest cities first) |
| 371 |
* |
| 372 |
* @since 3.0.0 |
| 373 |
*/ |
| 374 |
private function sort_machines( $machines ) { |
| 375 |
|
| 376 |
if ( $this->prioritization === 'yes' ) { |
| 377 |
|
| 378 |
usort( $machines, function( $a, $b ) { |
| 379 |
|
| 380 |
$sortorder = array( |
| 381 |
'tallinn', 'tartu linn','tartu', 'narva', 'pärnu linn','pärnu', 'viljandi', 'kohtla-järve', 'rakvere', 'maardu', 'sillamäe', 'kuressaare', |
| 382 |
'helsinki', 'espoo', 'tampere', 'vantaa', 'oulu', 'turku', 'jüväskülä', 'lahti', 'kuopio', 'kouvola', |
| 383 |
'rīga','riga', 'daugavpils', 'liepāja','liepaja', 'jelgava', 'jūrmala','jurmala', 'ventspils', 'rezekne', 'valmiera', 'jekabpils', |
| 384 |
'vilnius', 'kaunas', 'klaipeda', 'siauliai', 'panevezys', 'alytus', 'mariampole', 'mazeikiai', 'jonava', 'utena' |
| 385 |
); |
| 386 |
|
| 387 |
$acity = mb_strtolower($a['city']); |
| 388 |
$bcity = mb_strtolower($b['city']); |
| 389 |
|
| 390 |
if ( !$acity ) { |
| 391 |
$acity = 'xxxxxxx'; |
| 392 |
} |
| 393 |
|
| 394 |
if ( !$bcity ) { |
| 395 |
$bcity = 'xxxxxxx'; |
| 396 |
} |
| 397 |
|
| 398 |
$aidx = array_search( $acity, $sortorder ); |
| 399 |
$bidx = array_search ($bcity, $sortorder ); |
| 400 |
|
| 401 |
if ( $aidx !== false ) { |
| 402 |
$acity = str_pad( $aidx, 4, "0", STR_PAD_LEFT ) . '-' . $acity; |
| 403 |
} |
| 404 |
|
| 405 |
if ( $bidx !== false ) { |
| 406 |
$bcity = str_pad( $bidx, 4, "0", STR_PAD_LEFT ) . '-' . $bcity; |
| 407 |
} |
| 408 |
|
| 409 |
$acity .= '-' . mb_strtolower($a['name']); |
| 410 |
$bcity .= '-' . mb_strtolower($b['name']); |
| 411 |
|
| 412 |
return $acity < $bcity ? -1 : 1; |
| 413 |
} ); |
| 414 |
} |
| 415 |
|
| 416 |
return $machines; |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Force creating a function for initializing method specific form fields |
| 421 |
* |
| 422 |
* @since 3.0.0 |
| 423 |
*/ |
| 424 |
abstract public function initialize_method_form_fields(); |
| 425 |
} |
| 426 |
|