| 1 |
<?php |
| 2 |
|
| 3 |
namespace MakeCommerce\Shipping; |
| 4 |
|
| 5 |
/** |
| 6 |
* All functionality that has to do with shipping and orders |
| 7 |
* |
| 8 |
* @since 3.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
class Order extends \MakeCommerce\Shipping { |
| 12 |
|
| 13 |
private $loader; |
| 14 |
|
| 15 |
/** |
| 16 |
* Constructs Order class, defines hooks |
| 17 |
* |
| 18 |
* @since 3.0.0 |
| 19 |
*/ |
| 20 |
public function __construct( \MakeCommerce\Loader $loader ) { |
| 21 |
|
| 22 |
$this->loader = $loader; |
| 23 |
|
| 24 |
$this->define_hooks(); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Define all wordpress hooks |
| 29 |
* |
| 30 |
* @since 3.0.0 |
| 31 |
*/ |
| 32 |
public function define_hooks() { |
| 33 |
|
| 34 |
$this->loader->add_filter( 'woocommerce_order_details_after_customer_details', $this, 'parcel_machine_details' ); |
| 35 |
$this->loader->add_filter( 'woocommerce_admin_order_data_after_shipping_address', $this, 'parcel_machine_changing', 10, 1 ); |
| 36 |
$this->loader->add_filter( 'manage_shop_order_posts_custom_column', $this, 'shipping_method_order_view', 3 ); |
| 37 |
$this->loader->add_filter( 'woocommerce_email_customer_details_fields', $this, 'shipping_email_details', 10, 3 ); |
| 38 |
$this->loader->add_filter( 'restrict_manage_posts', $this, 'filter_orders' ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Add parcel machine information to order view (Customer) |
| 43 |
* |
| 44 |
* @since 3.0.0 |
| 45 |
*/ |
| 46 |
public function parcel_machine_details( $order ) { |
| 47 |
|
| 48 |
$machine_id = get_post_meta( $order->get_id(), '_parcel_machine', true ); |
| 49 |
|
| 50 |
if ( empty( $machine_id ) ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
list( $carrier, $machine ) = explode( '||', $machine_id ); |
| 55 |
|
| 56 |
if ( empty( $machine ) ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
|
| 60 |
$machine = self::mk_get_machine( $carrier, $machine ); |
| 61 |
|
| 62 |
if ( !$machine ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
echo ' |
| 67 |
<tr> |
| 68 |
<th>'.__( 'Parcel machine', 'wc_makecommerce_domain' ).' ('.ucfirst( $carrier ).') </th> |
| 69 |
<td>'.$machine['name'].'<br/>'.$machine['address'].'</td> |
| 70 |
</tr> |
| 71 |
'; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Allow changing parcel machine in admin and show data in order view |
| 76 |
* |
| 77 |
* @since 3.0.0 |
| 78 |
*/ |
| 79 |
public function parcel_machine_changing( $order ) { |
| 80 |
|
| 81 |
$order_id = $order->get_id(); |
| 82 |
$machine_id = get_post_meta( $order_id, '_parcel_machine', true ); |
| 83 |
|
| 84 |
if ( !empty( $machine_id ) ) { |
| 85 |
|
| 86 |
list( $carrier, $machine ) = explode( '||', $machine_id ); |
| 87 |
|
| 88 |
if ( !empty( $machine ) ) { |
| 89 |
|
| 90 |
$machine = self::mk_get_machine( $carrier, $machine ); |
| 91 |
if ( $machine ) { |
| 92 |
|
| 93 |
echo ' |
| 94 |
<h4>'.__( 'Selected parcel machine', 'wc_makecommerce_domain' ).'</h4> |
| 95 |
<div class="edit_address"> |
| 96 |
<p class="form-field form-field-wide _mk_machine_id" style="padding-bottom: 10px !important;"> |
| 97 |
<label for="_mk_machine_id">'.__( 'Parcel machine', 'wc_makecommerce_domain' ).'</label> |
| 98 |
<select id="_mk_machine_id" name="_mk_machine_id" class="wc-enhanced-select select"> |
| 99 |
'; |
| 100 |
|
| 101 |
$res = ''; |
| 102 |
$machines = self::mk_get_machines( $carrier, method_exists( $order, 'get_shipping_country' ) ? $order->get_shipping_country() : $order->shipping_country , []); |
| 103 |
|
| 104 |
$res .= '<option value="">'.__( '-- select parcel machine --', 'wc_makecommerce_domain' ).'</option>'; |
| 105 |
|
| 106 |
$pcity = false; |
| 107 |
foreach ( $machines as $smachine ) { |
| 108 |
|
| 109 |
$city = strtolower( $smachine['city'] ); |
| 110 |
|
| 111 |
if ( $city !== $pcity ) { |
| 112 |
|
| 113 |
if ( $pcity ) { |
| 114 |
$res .= '</optgroup>'; |
| 115 |
} |
| 116 |
|
| 117 |
$res .= '<optgroup label="'.$smachine['city'].'">'; |
| 118 |
} |
| 119 |
|
| 120 |
$mname = $smachine['name']; |
| 121 |
$res .= '<option value="'.esc_attr( $smachine['carrier'].'||'.$smachine['id'] ).'" '.( $machine['id'] === $smachine['id'] ? ' selected="selected"' : '' ).'>'.$mname.'</option>'; |
| 122 |
$pcity = $city; |
| 123 |
} |
| 124 |
|
| 125 |
if ( $pcity ) { |
| 126 |
$res .= '</optgroup>'; |
| 127 |
} |
| 128 |
|
| 129 |
echo $res; |
| 130 |
|
| 131 |
echo ' |
| 132 |
</select> |
| 133 |
</p> |
| 134 |
</div> |
| 135 |
|
| 136 |
<div class="address"> |
| 137 |
<p>'.ucfirst($carrier).'</strong><br/>'.$machine['name'] . '<br/><small>' . $machine['address'] . '</small></p> |
| 138 |
</div> |
| 139 |
'; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
if ( empty( $carrier ) ) { |
| 145 |
$carrier = ''; |
| 146 |
} |
| 147 |
|
| 148 |
$shipment_id = get_post_meta( $order_id, '_parcel_machine_shipment_id', true ); |
| 149 |
$shipment_id_error = get_post_meta( $order_id, '_parcel_machine_error', true ); |
| 150 |
$shipment_manifest = get_post_meta( $order_id, '_parcel_machine_manifest', true ); |
| 151 |
|
| 152 |
//currently the only delivery time option exists for smartpost courier shipping method. |
| 153 |
foreach ( $order->get_shipping_methods() as $shippingMethod ) { |
| 154 |
|
| 155 |
if ( $shippingMethod["method_id"] == "courier_smartpost" ) { |
| 156 |
|
| 157 |
$delivery_time = get_post_meta( $order_id, '_delivery_time', true ); |
| 158 |
|
| 159 |
if ( !$shipment_id_error ) { |
| 160 |
|
| 161 |
if ( $delivery_time === '1' ) { |
| 162 |
|
| 163 |
echo '<p><strong>'.__( 'Delivery time', 'wc_makecommerce_domain' ).'</strong> - '.__( 'Any time', 'wc_makecommerce_domain' ).'</p>'; |
| 164 |
} |
| 165 |
|
| 166 |
if ( $delivery_time === '2' ) { |
| 167 |
|
| 168 |
echo '<p><strong>'.__( 'Delivery time', 'wc_makecommerce_domain' ).'</strong> - 09:00..17:00</p>'; |
| 169 |
} |
| 170 |
|
| 171 |
if ( $delivery_time === '3' ) { |
| 172 |
|
| 173 |
echo '<p><strong>'.__( 'Delivery time', 'wc_makecommerce_domain' ).'</strong> - 17:00..21:00</p>'; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if ( $shipment_id ) { |
| 180 |
$trackinglink = '<p><strong>'.__( 'Shipment tracking code', 'wc_makecommerce_domain' ).'</strong>'; |
| 181 |
|
| 182 |
//check if its omniva courier |
| 183 |
if ( $carrier == "" ) { |
| 184 |
$carrier = $this->get_order_shipment_carrier( $order ); |
| 185 |
} |
| 186 |
|
| 187 |
$theLink = $this->get_tracking_link( $carrier, $order_id, true ); |
| 188 |
if ( $carrier != "" && $theLink != "" ) { |
| 189 |
$trackinglink .= ' ('. ucfirst( $shippingMethod["method_title"] ) .'): <br/> <a target="_blank" href="' . $theLink . $shipment_id . '">' . $shipment_id . '</a>'; |
| 190 |
} else { |
| 191 |
$trackinglink .= ': <br/>'.$shipment_id; |
| 192 |
} |
| 193 |
|
| 194 |
echo $trackinglink.'</p>'; |
| 195 |
} |
| 196 |
|
| 197 |
if ( $shipment_manifest ) { |
| 198 |
echo '<p><strong>'.__( 'Shipments pickup manifest' ).':</strong><br/><a href="' . $shipment_manifest . '" target="_blank">link</a></small></p>'; |
| 199 |
} |
| 200 |
|
| 201 |
if ( $shipment_id_error ) { |
| 202 |
echo '<p><strong style="color: red;">'.__( 'Shipment registration error' ).':</strong><br/>' . $shipment_id_error . '</small></p>'; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Add shipping information on order list view |
| 208 |
* |
| 209 |
* @since 3.0.0 |
| 210 |
*/ |
| 211 |
public function shipping_method_order_view( $column ) { |
| 212 |
|
| 213 |
global $post, $woocommerce, $the_order; |
| 214 |
|
| 215 |
if ( !$the_order ) { |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
$the_order_id = $the_order->get_id(); |
| 220 |
|
| 221 |
if ( empty( $the_order ) || $the_order_id != $post->ID ) { |
| 222 |
|
| 223 |
$the_order = wc_get_order( $post->ID ); |
| 224 |
} |
| 225 |
|
| 226 |
if ( $column === 'shipping_address' ) { |
| 227 |
|
| 228 |
$machine_id = get_post_meta( $the_order_id, '_parcel_machine', true ); |
| 229 |
$shipment_id = get_post_meta( $the_order_id, '_parcel_machine_shipment_id', true ); |
| 230 |
$shipment_id_error = get_post_meta( $the_order_id, '_parcel_machine_error', true ); |
| 231 |
|
| 232 |
if ( $shipment_id ) { |
| 233 |
|
| 234 |
echo __( 'Shipment tracking code', 'wc_makecommerce_domain' ) . ': ' . $shipment_id . '<br/>'; |
| 235 |
echo '<span class="has_shipment_id"></span>'; |
| 236 |
} else if ( $shipment_id_error ) { |
| 237 |
echo '<span style="color: red;">'.__( 'Package shipment generation error:', 'wc_makecommerce_domain' ) . '</span><br/>' .$shipment_id_error; |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Fires before the Filter button on the Posts and Pages list tables. |
| 244 |
* Allows filtering of orders by shipping method |
| 245 |
* |
| 246 |
* @since 3.0.0 |
| 247 |
*/ |
| 248 |
public function filter_orders() { |
| 249 |
|
| 250 |
global $typenow; |
| 251 |
|
| 252 |
if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ) ) ) { |
| 253 |
|
| 254 |
$selected_method = !empty( $_REQUEST['_shipping_method'] ) ? $_REQUEST['_shipping_method'] : false; |
| 255 |
$methods = WC()->shipping->load_shipping_methods(); |
| 256 |
|
| 257 |
echo '<select name="_shipping_method" id="shipping_type" class="enhanced">'; |
| 258 |
echo '<option value="">'.__( '-- filter by shipping method', 'wc_makecommerce_domain' ) . '</option>'; |
| 259 |
|
| 260 |
foreach ( $methods as $method ) { |
| 261 |
|
| 262 |
echo '<option value="'.$method->id.'"'.($selected_method === $method->id ? ' selected="selected"' : '').'>'.$method->title.'</option>'; |
| 263 |
} |
| 264 |
|
| 265 |
echo '</select>'; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Returns order shipment carrier |
| 271 |
* |
| 272 |
* @since 3.0.0 |
| 273 |
*/ |
| 274 |
private function get_order_shipment_carrier( $order ) { |
| 275 |
|
| 276 |
foreach ( $order->get_shipping_methods() as $shippingMethod ) { |
| 277 |
$carrier = explode( "_", $shippingMethod["method_id"] ); |
| 278 |
|
| 279 |
$carrier = $carrier[1]; |
| 280 |
} |
| 281 |
|
| 282 |
//in SCO carrier is omniva:4 instead of just omniva |
| 283 |
$carrier = explode( ":", $carrier ); |
| 284 |
$carrier = $carrier[0]; |
| 285 |
|
| 286 |
return $carrier; |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Add shipment data to email |
| 291 |
* |
| 292 |
* @since 3.0.0 |
| 293 |
*/ |
| 294 |
public function shipping_email_details( $fields, $sent_to_admin, $order ) { |
| 295 |
|
| 296 |
//check if makecommerce shipping has been used |
| 297 |
//add tracking information (if possible) |
| 298 |
$shipment_id = get_post_meta( $order->get_id(), "_parcel_machine_shipment_id", true ); |
| 299 |
|
| 300 |
$machine_id = get_post_meta( $order->get_id(), '_parcel_machine', true ); |
| 301 |
|
| 302 |
$machine = ""; |
| 303 |
$carrier = ""; |
| 304 |
|
| 305 |
$machineIdArr = explode( '||', $machine_id ); |
| 306 |
|
| 307 |
if ( count( $machineIdArr ) == 2 ) { |
| 308 |
|
| 309 |
$carrier = $machineIdArr[0]; |
| 310 |
$machine = $machineIdArr[1]; |
| 311 |
} else if ( count( $machineIdArr ) == 1 ) { |
| 312 |
|
| 313 |
$carrier = $machineIdArr[0]; |
| 314 |
} |
| 315 |
|
| 316 |
$machine = self::mk_get_machine( $carrier, $machine ); |
| 317 |
|
| 318 |
//do something about the language only if we even have something to add to the email |
| 319 |
if ( $shipment_id || ( $machine != "" && $carrier != "" ) ) { |
| 320 |
|
| 321 |
//this only needs to be done for polylang. Don't do it for wpml, that won't work. |
| 322 |
if ( \MakeCommerce\i18n::is_polylang_active() ) { |
| 323 |
|
| 324 |
$orderLang = get_locale(); |
| 325 |
|
| 326 |
if ( $orderLang != "" ) { |
| 327 |
|
| 328 |
if ( substr( $orderLang, 0 ,2 ) == "en" ) { |
| 329 |
|
| 330 |
switch_to_locale( "et" ); |
| 331 |
do_action( 'wpml_switch_language', "et" ); |
| 332 |
} else { |
| 333 |
|
| 334 |
switch_to_locale( "en_GB" ); |
| 335 |
do_action( 'wpml_switch_language', "en_GB" ); |
| 336 |
} |
| 337 |
|
| 338 |
switch_to_locale( $orderLang ); |
| 339 |
do_action( 'wpml_switch_language', $orderLang ); |
| 340 |
} |
| 341 |
|
| 342 |
load_plugin_textdomain( 'wc_makecommerce_domain', false, '/makecommerce/languages/' ); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
if ( $machine != "" && $carrier != "" ) { |
| 347 |
//add parcel machine name/location |
| 348 |
$fields[] = array( 'label' => __( 'Parcel machine', 'wc_makecommerce_domain' ).' ('.ucfirst( $carrier ).')', 'value' => $machine['name'].' - '.$machine['address']); |
| 349 |
} |
| 350 |
|
| 351 |
$shipment_id_error = get_post_meta( $order->get_id(), '_parcel_machine_error', true ); |
| 352 |
|
| 353 |
//currently the only delivery time option exists for smartpost courier shipping method. |
| 354 |
foreach ( $order->get_shipping_methods() as $shippingMethod ) { |
| 355 |
|
| 356 |
if ( $shippingMethod["method_id"] == "courier_smartpost" ) { |
| 357 |
|
| 358 |
$delivery_time = get_post_meta( $order->get_id(), '_delivery_time', true ); |
| 359 |
|
| 360 |
if ( !$shipment_id_error ) { |
| 361 |
|
| 362 |
if ( $delivery_time === '1' ) { |
| 363 |
|
| 364 |
$fields[] = array( 'label' => __( 'Delivery time', 'wc_makecommerce_domain' ), 'value' => __( 'Any time', 'wc_makecommerce_domain' ) ); |
| 365 |
} |
| 366 |
|
| 367 |
if ( $delivery_time === '2' ) { |
| 368 |
|
| 369 |
$fields[] = array( 'label' => __( 'Delivery time', 'wc_makecommerce_domain' ), 'value' => "09:00..17:00" ); |
| 370 |
} |
| 371 |
|
| 372 |
if ( $delivery_time === '3' ) { |
| 373 |
|
| 374 |
$fields[] = array( 'label' => __( 'Delivery time', 'wc_makecommerce_domain' ), 'value' => "17:00..21:00" ); |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
//add tracking information (if possible) |
| 381 |
if ( $shipment_id ) { |
| 382 |
|
| 383 |
$trackinglink = '<p><strong>'.__(' Shipment tracking code', 'wc_makecommerce_domain' ).'</strong>'; |
| 384 |
|
| 385 |
if ( $carrier == "" ) { |
| 386 |
$carrier = $this->get_order_shipment_carrier( $order ); |
| 387 |
} |
| 388 |
|
| 389 |
$href = $this->get_tracking_link( $carrier, $order->get_id() ); |
| 390 |
|
| 391 |
if ( $href != "" ) { |
| 392 |
|
| 393 |
$href .= $shipment_id; |
| 394 |
$fields[] = array( 'label' => __( 'Shipment tracking info', 'wc_makecommerce_domain' ).' ('.$carrier.')', 'value' => "<a target='_blank' title='".__( 'Shipment tracking code', 'wc_makecommerce_domain' )."' href='".$href."'>".$href."</a>" ); |
| 395 |
} else { |
| 396 |
$fields[] = array( 'label' => __( 'Shipment tracking info', 'wc_makecommerce_domain' ), 'value' => $shipment_id ); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
return $fields; |
| 401 |
} |
| 402 |
} |