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