| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Order\Bulk file. |
| 4 |
* |
| 5 |
* @package PostNLWooCommerce\Order |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace PostNLWooCommerce\Order; |
| 9 |
|
| 10 |
use PostNLWooCommerce\Utils; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Class Bulk |
| 18 |
* |
| 19 |
* @package PostNLWooCommerce\Order |
| 20 |
*/ |
| 21 |
class Bulk extends Base { |
| 22 |
|
| 23 |
/** |
| 24 |
* Field name for action confirmation option text. |
| 25 |
* |
| 26 |
* @var String |
| 27 |
*/ |
| 28 |
public $bulk_option_text_name = '_postnl_bulk_action_confirmation'; |
| 29 |
|
| 30 |
/** |
| 31 |
* Collection of hooks when initiation. |
| 32 |
*/ |
| 33 |
public function init_hooks() { |
| 34 |
add_filter( 'bulk_actions-edit-shop_order', array( $this, 'add_order_bulk_actions' ), 10, 1 ); |
| 35 |
add_filter( 'handle_bulk_actions-edit-shop_order', array( $this, 'bulk_action_create_label' ), 10, 3 ); |
| 36 |
add_filter( |
| 37 |
'handle_bulk_actions-edit-shop_order', |
| 38 |
array( |
| 39 |
$this, |
| 40 |
'bulk_action_change_shipping_options', |
| 41 |
), |
| 42 |
10, |
| 43 |
3 |
| 44 |
); |
| 45 |
|
| 46 |
add_filter( 'bulk_actions-woocommerce_page_wc-orders', array( $this, 'add_order_bulk_actions' ), 10, 1 ); |
| 47 |
add_filter( |
| 48 |
'handle_bulk_actions-woocommerce_page_wc-orders', |
| 49 |
array( |
| 50 |
$this, |
| 51 |
'bulk_action_create_label', |
| 52 |
), |
| 53 |
10, |
| 54 |
3 |
| 55 |
); |
| 56 |
add_filter( |
| 57 |
'handle_bulk_actions-woocommerce_page_wc-orders', |
| 58 |
array( |
| 59 |
$this, |
| 60 |
'bulk_action_change_shipping_options', |
| 61 |
), |
| 62 |
10, |
| 63 |
3 |
| 64 |
); |
| 65 |
|
| 66 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_bulk_assets' ) ); |
| 67 |
add_action( 'admin_footer', array( $this, 'modal_create_label' ) ); |
| 68 |
add_action( 'admin_footer', array( $this, 'modal_change_shipping_options' ) ); |
| 69 |
add_filter( 'postnl_order_meta_box_fields', array( $this, 'additional_meta_box' ), 10, 2 ); |
| 70 |
|
| 71 |
// Display admin notices for bulk actions. |
| 72 |
add_action( 'admin_notices', array( $this, 'render_messages' ) ); |
| 73 |
add_action( 'init', array( $this, 'get_bulk_file' ), 10 ); |
| 74 |
|
| 75 |
// Add 'Create Label' action button. |
| 76 |
add_action( 'wp_ajax_postnl_create_label', array( $this, 'postnl_create_label_ajax' ) ); |
| 77 |
add_filter( 'woocommerce_admin_order_actions', array( $this, 'add_create_label_actions_button' ), 10, 2 ); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Add new bulk actions. |
| 82 |
* |
| 83 |
* @param array $bulk_actions List of bulk actions. |
| 84 |
* |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public function add_order_bulk_actions( $bulk_actions ) { |
| 88 |
$base_country = Utils::get_base_country(); |
| 89 |
$bulk_actions['postnl-create-label'] = esc_html__( 'PostNL Create Label', 'postnl-for-woocommerce' ); |
| 90 |
if ( $base_country != 'BE' ) { |
| 91 |
$bulk_actions['postnl-change-shipping-options'] = esc_html__( 'PostNL Change Shipping Options', 'postnl-for-woocommerce' ); |
| 92 |
} |
| 93 |
|
| 94 |
return $bulk_actions; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Process PostNL in bulk. |
| 99 |
* |
| 100 |
* @param String $redirect Redirect URL after the bulk has been processed. |
| 101 |
* @param String $doaction The chosen action. |
| 102 |
* @param array $object_ids All chose IDs. |
| 103 |
* |
| 104 |
* @return string |
| 105 |
*/ |
| 106 |
public function bulk_action_create_label( $redirect, $doaction, $object_ids ) { |
| 107 |
if ( 'postnl-create-label' !== $doaction ) { |
| 108 |
return $redirect; |
| 109 |
} |
| 110 |
|
| 111 |
$array_messages = array( |
| 112 |
'user_id' => get_current_user_id(), |
| 113 |
); |
| 114 |
|
| 115 |
$gen_labels = array(); // Generated labels. |
| 116 |
|
| 117 |
if ( ! empty( $object_ids ) ) { |
| 118 |
foreach ( $object_ids as $order_id ) { |
| 119 |
$result = $this->generate_label_and_notes( $order_id, $_REQUEST ); |
| 120 |
if ( isset( $result['message'] ) ) { |
| 121 |
$array_messages[] = $result['message']; |
| 122 |
} |
| 123 |
if ( isset( $result['labels_data']['labels'] ) ) { |
| 124 |
$gen_labels[] = $result['labels_data']['labels']; |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
if ( ! empty( $gen_labels ) ) { |
| 130 |
$array_messages[] = $this->merge_bulk_labels( $gen_labels ); |
| 131 |
} |
| 132 |
|
| 133 |
update_option( $this->bulk_option_text_name, $array_messages ); |
| 134 |
|
| 135 |
return $redirect; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Process PostNL in bulk. |
| 140 |
* |
| 141 |
* @param String $redirect Redirect URL after the bulk has been processed. |
| 142 |
* @param String $doaction Chosen action. |
| 143 |
* @param array $object_ids Chose IDs. |
| 144 |
* |
| 145 |
* @return string |
| 146 |
*/ |
| 147 |
public function bulk_action_change_shipping_options( $redirect, $doaction, $object_ids ) { |
| 148 |
if ( 'postnl-change-shipping-options' !== $doaction || empty( $object_ids ) ) { |
| 149 |
return $redirect; |
| 150 |
} |
| 151 |
|
| 152 |
$array_messages = array( |
| 153 |
'user_id' => get_current_user_id(), |
| 154 |
); |
| 155 |
|
| 156 |
$selected_shipping_options = $this->prepare_default_options( $_REQUEST ); |
| 157 |
$zone = strtoupper( sanitize_text_field( $_REQUEST['postnl_shipping_zone'] ) ); |
| 158 |
|
| 159 |
foreach ( $object_ids as $order_id ) { |
| 160 |
$order = wc_get_order( $order_id ); |
| 161 |
$have_label_file = $this->have_label_file( $order ); |
| 162 |
$order_shipping_zone = Utils::get_shipping_zone( $order->get_shipping_country(), $order->get_shipping_state() ); |
| 163 |
$match_shipping_zones = $zone === $order_shipping_zone; |
| 164 |
$match_pickup_zone = 'PICKUP' === $zone && 'NL' === $order_shipping_zone; |
| 165 |
|
| 166 |
if ( $have_label_file ) { |
| 167 |
$array_messages[] = array( |
| 168 |
// Translators: %1$d is the order ID. |
| 169 |
'message' => sprintf( esc_html__( 'Order #%1$d already has a label.', 'postnl-for-woocommerce' ), $order_id ), |
| 170 |
'type' => 'error', |
| 171 |
); |
| 172 |
|
| 173 |
continue; |
| 174 |
} |
| 175 |
|
| 176 |
if ( ! $match_shipping_zones && ! $match_pickup_zone ) { |
| 177 |
$array_messages[] = array( |
| 178 |
// Translators: %1$d is the order ID. |
| 179 |
'message' => sprintf( esc_html__( 'Order #%1$d is from another shipping zone.', 'postnl-for-woocommerce' ), $order_id ), |
| 180 |
'type' => 'error', |
| 181 |
); |
| 182 |
|
| 183 |
continue; |
| 184 |
} |
| 185 |
|
| 186 |
$meta = $order->get_meta( $this->meta_name ); |
| 187 |
|
| 188 |
if ( ! is_array( $meta ) ) { |
| 189 |
$meta = array(); |
| 190 |
} |
| 191 |
|
| 192 |
// Collapse an explicit 24h/48h letterbox choice onto the generic 'letterbox' |
| 193 |
// feature and record the variant as the authoritative merchant choice, which |
| 194 |
// Item_Info::get_letterbox_type() reads to pick product 2928 vs 2948. |
| 195 |
$letterbox_selection = Utils::normalize_letterbox_options( $selected_shipping_options ); |
| 196 |
$meta['backend'] = $letterbox_selection['options']; |
| 197 |
$order->update_meta_data( $this->meta_name, $meta ); |
| 198 |
if ( '' !== $letterbox_selection['type'] ) { |
| 199 |
$order->update_meta_data( '_postnl_letterbox_type', $letterbox_selection['type'] ); |
| 200 |
} |
| 201 |
$order->save(); |
| 202 |
} |
| 203 |
|
| 204 |
update_option( $this->bulk_option_text_name, $array_messages ); |
| 205 |
|
| 206 |
return $redirect; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Prepare default shipping options based on user selection from the bulk modal. |
| 211 |
* |
| 212 |
* @param array $options Selected options by the user. |
| 213 |
* |
| 214 |
* @return array |
| 215 |
*/ |
| 216 |
protected function prepare_default_options( $options ) { |
| 217 |
$zone = sanitize_text_field( $options['postnl_shipping_zone'] ); |
| 218 |
$selected_option = sanitize_text_field( $options[ 'postnl_default_shipping_options_' . strtolower( $zone ) ] ); |
| 219 |
|
| 220 |
return Utils::prepare_shipping_options( $selected_option ); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Merge bulk labels. |
| 225 |
* |
| 226 |
* @param array $gen_labels Generated labels. |
| 227 |
*/ |
| 228 |
public function merge_bulk_labels( $gen_labels ) { |
| 229 |
$label_format = $this->settings->get_label_format(); |
| 230 |
$label_paths = array(); |
| 231 |
|
| 232 |
foreach ( $gen_labels as $idx => $label ) { |
| 233 |
foreach ( $label as $label_type => $label_info ) { |
| 234 |
if ( empty( $label_info['filepath'] ) ) { |
| 235 |
continue 2; |
| 236 |
} |
| 237 |
|
| 238 |
if ( 'A6' === $label_format || 'pdf' !== pathinfo( $label_info['filepath'], PATHINFO_EXTENSION ) ) { |
| 239 |
$label_paths[] = $label_info['filepath']; |
| 240 |
continue 2; |
| 241 |
} |
| 242 |
|
| 243 |
if ( empty( $label_info['merged_files'] ) ) { |
| 244 |
continue 2; |
| 245 |
} |
| 246 |
|
| 247 |
foreach ( $label_info['merged_files'] as $path ) { |
| 248 |
$label_paths[] = $path; |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
$filename = 'postnl-bulk-' . get_current_user_id() . '.' . pathinfo( $label_paths[0], PATHINFO_EXTENSION ); |
| 254 |
|
| 255 |
if ( isset( $_GET['postnl_position_printing_labels'] ) ) { |
| 256 |
$start_position = sanitize_text_field( $_GET['postnl_position_printing_labels'] ); |
| 257 |
} else { |
| 258 |
$start_position = 'top-left'; |
| 259 |
} |
| 260 |
|
| 261 |
$merged_info = $this->merge_labels( $label_paths, $filename, $start_position ); |
| 262 |
|
| 263 |
if ( file_exists( $merged_info ['filepath'] ) ) { |
| 264 |
// We're saving the bulk file path temporarily and access it later during the download process. |
| 265 |
// This information expires in 3 minutes (180 seconds), just enough for the user to see the |
| 266 |
// Displayed link and click it if he or she wishes to download the bulk labels. |
| 267 |
set_transient( '_postnl_bulk_download_labels_file_' . get_current_user_id(), $merged_info ['filepath'], 180 ); |
| 268 |
|
| 269 |
// Construct URL pointing to the download label endpoint (with bulk param). |
| 270 |
$bulk_download_label_url = $this->get_download_bulk_url(); |
| 271 |
|
| 272 |
return array( |
| 273 |
// translators: %1$s is anchor tag opener. %2$s is anchor tag closer. |
| 274 |
'message' => sprintf( esc_html__( 'Bulk PostNL labels file created - %1$sdownload file%2$s', 'postnl-for-woocommerce' ), '<a href="' . esc_url( $bulk_download_label_url ) . '" download>', '</a>' ), |
| 275 |
'type' => 'success', |
| 276 |
); |
| 277 |
} |
| 278 |
|
| 279 |
return array( |
| 280 |
'message' => esc_html__( 'Could not create bulk PostNL label file, download individually.', 'postnl-for-woocommerce' ), |
| 281 |
'type' => 'error', |
| 282 |
); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Generate download label url |
| 287 |
* |
| 288 |
* @return String. |
| 289 |
*/ |
| 290 |
public function get_download_bulk_url() { |
| 291 |
$download_url = add_query_arg( |
| 292 |
array( |
| 293 |
'postnl_label_bulk_nonce' => wp_create_nonce( 'postnl_download_label_bulk_nonce' ), |
| 294 |
), |
| 295 |
home_url() |
| 296 |
); |
| 297 |
|
| 298 |
return $download_url; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Get label file. |
| 303 |
* |
| 304 |
* @return void |
| 305 |
* @since 1.0.0 |
| 306 |
*/ |
| 307 |
public function get_bulk_file() { |
| 308 |
|
| 309 |
if ( empty( $_GET['postnl_label_bulk_nonce'] ) ) { |
| 310 |
return; |
| 311 |
} |
| 312 |
|
| 313 |
// Check nonce before proceed. |
| 314 |
$nonce_result = check_ajax_referer( 'postnl_download_label_bulk_nonce', sanitize_text_field( wp_unslash( $_GET['postnl_label_bulk_nonce'] ) ), false ); |
| 315 |
|
| 316 |
if ( ! current_user_can( 'manage_woocommerce' ) ) { |
| 317 |
return; |
| 318 |
} |
| 319 |
|
| 320 |
$label_path = get_transient( '_postnl_bulk_download_labels_file_' . get_current_user_id() ); |
| 321 |
|
| 322 |
if ( empty( $label_path ) ) { |
| 323 |
return; |
| 324 |
} |
| 325 |
|
| 326 |
$this->download_label( $label_path ); |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Collection of hooks when initiation. |
| 331 |
*/ |
| 332 |
public function enqueue_bulk_assets() { |
| 333 |
$screen = get_current_screen(); |
| 334 |
|
| 335 |
$is_legacy_order = ! empty( $screen->id ) && 'edit-shop_order' === $screen->id && ! empty( $screen->base ) && 'edit' === $screen->base; |
| 336 |
$is_hpos_order = ! empty( $screen->id ) && 'woocommerce_page_wc-orders' === $screen->id && ( empty( $_GET['action'] ) || 'edit' !== $_GET['action'] ); |
| 337 |
if ( $is_legacy_order || $is_hpos_order ) { |
| 338 |
// Enqueue the assets. |
| 339 |
wp_enqueue_style( 'thickbox' ); |
| 340 |
wp_enqueue_script( 'thickbox' ); |
| 341 |
|
| 342 |
wp_enqueue_style( |
| 343 |
'postnl-admin-order-bulk', |
| 344 |
POSTNL_WC_PLUGIN_DIR_URL . '/assets/css/admin-order-bulk.css', |
| 345 |
array(), |
| 346 |
POSTNL_WC_VERSION |
| 347 |
); |
| 348 |
|
| 349 |
wp_enqueue_script( |
| 350 |
'postnl-admin-order-bulk', |
| 351 |
POSTNL_WC_PLUGIN_DIR_URL . '/assets/js/admin-order-bulk.js', |
| 352 |
array( 'thickbox' ), |
| 353 |
POSTNL_WC_VERSION, |
| 354 |
true |
| 355 |
); |
| 356 |
|
| 357 |
wp_enqueue_script( |
| 358 |
'postnl-admin-shipment-track-trace', |
| 359 |
POSTNL_WC_PLUGIN_DIR_URL . '/assets/js/admin-shipment-track-trace.js', |
| 360 |
array( 'jquery' ), |
| 361 |
POSTNL_WC_VERSION, |
| 362 |
true |
| 363 |
); |
| 364 |
|
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Adding additional meta box in order admin page. |
| 370 |
* |
| 371 |
* @param array $fields List of fields for order admin page. |
| 372 |
* @param string $context 'display' when rendering the admin UI, 'save' when persisting. |
| 373 |
*/ |
| 374 |
public function additional_meta_box( $fields, $context = 'display' ) { |
| 375 |
// Trimming is a display concern only. During a save it would silently drop every |
| 376 |
// show_in_bulk = false field (Letterbox 48, ID Check, ...), reverting the shipment |
| 377 |
// to Standard when a label is generated from the legacy orders list bulk action. |
| 378 |
if ( 'save' === $context ) { |
| 379 |
return $fields; |
| 380 |
} |
| 381 |
|
| 382 |
$screen = get_current_screen(); |
| 383 |
|
| 384 |
if ( empty( $screen ) ) { |
| 385 |
return $fields; |
| 386 |
} |
| 387 |
|
| 388 |
if ( 'edit' === $screen->base && 'shop_order' === $screen->post_type && $screen->in_admin() ) { |
| 389 |
return array_filter( |
| 390 |
$fields, |
| 391 |
function ( $field ) { |
| 392 |
return ( ! empty( $field['show_in_bulk'] ) && true === $field['show_in_bulk'] ); |
| 393 |
} |
| 394 |
); |
| 395 |
} |
| 396 |
|
| 397 |
return $fields; |
| 398 |
} |
| 399 |
|
| 400 |
/** |
| 401 |
* Create modal wrapper with given id and fields definition. |
| 402 |
* |
| 403 |
* @param string $modal_id Modal id. |
| 404 |
* @param array $fields Fields to be added to the modal contend. |
| 405 |
* |
| 406 |
* @return void |
| 407 |
*/ |
| 408 |
protected function create_modal_content_wrapper( $modal_id, $fields ) { |
| 409 |
global $thepostid, $post; |
| 410 |
|
| 411 |
$screen = get_current_screen(); |
| 412 |
|
| 413 |
$is_legacy_order = ! empty( $screen->id ) && 'edit-shop_order' === $screen->id && ! empty( $screen->base ) && 'edit' === $screen->base; |
| 414 |
$is_hpos_order = ! empty( $screen->id ) && 'woocommerce_page_wc-orders' === $screen->id && ( empty( $_GET['action'] ) || 'edit' !== $_GET['action'] ); |
| 415 |
|
| 416 |
// Bugfix, warnings shown for Order table results with no Orders. |
| 417 |
if ( $is_legacy_order && empty( $thepostid ) && empty( $post ) ) { |
| 418 |
return; |
| 419 |
} |
| 420 |
|
| 421 |
if ( $is_legacy_order || $is_hpos_order ) { |
| 422 |
?> |
| 423 |
<div id="<?php echo esc_attr( $modal_id ); ?>-modal" style="display:none;"> |
| 424 |
<div class="postnl-modal <?php echo esc_attr( $modal_id . '-content' ); ?>"> |
| 425 |
<?php Utils::fields_generator( $fields ); ?> |
| 426 |
<button type="button" class="button button-primary" |
| 427 |
id="<?php echo esc_attr( $modal_id ); ?>-proceed"><?php esc_html_e( 'Submit', 'postnl-for-woocommerce' ); ?></button> |
| 428 |
</div> |
| 429 |
</div> |
| 430 |
<?php |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Prepare fields for the Change shipping options modal. |
| 436 |
* |
| 437 |
* @return array[] |
| 438 |
*/ |
| 439 |
protected function create_label_fields() { |
| 440 |
$fields = array( |
| 441 |
array( |
| 442 |
'id' => $this->prefix . 'num_labels', |
| 443 |
'type' => 'number', |
| 444 |
'label' => __( 'Number of Labels: ', 'postnl-for-woocommerce' ), |
| 445 |
'placeholder' => '', |
| 446 |
'description' => '', |
| 447 |
'class' => 'short', |
| 448 |
'value' => '1', |
| 449 |
'container' => true, |
| 450 |
'custom_attributes' => |
| 451 |
array( |
| 452 |
'step' => 'any', |
| 453 |
'min' => '0', |
| 454 |
), |
| 455 |
), |
| 456 |
|
| 457 |
); |
| 458 |
|
| 459 |
if ( 'in_box' === $this->settings->get_return_shipment_and_labels() ) { |
| 460 |
$fields[] = array( |
| 461 |
'id' => $this->prefix . 'create_return_label', |
| 462 |
'type' => 'checkbox', |
| 463 |
'label' => __( 'Create Return Label: ', 'postnl-for-woocommerce' ), |
| 464 |
'placeholder' => '', |
| 465 |
'description' => '', |
| 466 |
'container' => true, |
| 467 |
'value' => 'yes', |
| 468 |
); |
| 469 |
} |
| 470 |
|
| 471 |
if ( 'A6' !== $this->settings->get_label_format() ) { |
| 472 |
$fields[] = array( |
| 473 |
'id' => $this->prefix . 'position_printing_labels', |
| 474 |
'type' => 'select', |
| 475 |
'label' => __( 'Start position printing label: ', 'postnl-for-woocommerce' ), |
| 476 |
'placeholder' => '', |
| 477 |
'description' => '', |
| 478 |
'container' => true, |
| 479 |
'value' => 'top-left', |
| 480 |
'options' => array( |
| 481 |
'top-left' => __( 'Top Left', 'postnl-for-woocommerce' ), |
| 482 |
'top-right' => __( 'Top Right', 'postnl-for-woocommerce' ), |
| 483 |
'bottom-left' => __( 'Bottom Left', 'postnl-for-woocommerce' ), |
| 484 |
'bottom-right' => __( 'Bottom Right', 'postnl-for-woocommerce' ), |
| 485 |
), |
| 486 |
); |
| 487 |
} |
| 488 |
|
| 489 |
return $fields; |
| 490 |
} |
| 491 |
|
| 492 |
/** |
| 493 |
* Collection of fields in create label bulk action. |
| 494 |
*/ |
| 495 |
public function modal_create_label() { |
| 496 |
$this->create_modal_content_wrapper( 'postnl-create-label', $this->create_label_fields() ); |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* Get shipping options per given zone from the plugin settings. |
| 501 |
* |
| 502 |
* @param string $zone Zone you want shipping options to, available nl, be, eu, row. |
| 503 |
* |
| 504 |
* @return array |
| 505 |
*/ |
| 506 |
protected function get_available_shipping_options_per_zone( $zone ) { |
| 507 |
return $this->settings->get_setting_fields()[ 'default_shipping_options_' . strtolower( $zone ) ]['options']; |
| 508 |
} |
| 509 |
|
| 510 |
/** |
| 511 |
* Prepare fields for the Change shipping options modal. |
| 512 |
* |
| 513 |
* @return array[] |
| 514 |
*/ |
| 515 |
protected function change_shipping_options_fields() { |
| 516 |
return array( |
| 517 |
array( |
| 518 |
'id' => $this->prefix . 'shipping_zone', |
| 519 |
'type' => 'select', |
| 520 |
'label' => __( 'Shipping zone', 'postnl-for-woocommerce' ), |
| 521 |
'value' => 'nl', |
| 522 |
'container' => true, |
| 523 |
'options' => array( |
| 524 |
'nl' => __( 'Domestic', 'postnl-for-woocommerce' ), |
| 525 |
'be' => __( 'Belgium', 'postnl-for-woocommerce' ), |
| 526 |
'eu' => __( 'EU Parcel', 'postnl-for-woocommerce' ), |
| 527 |
'row' => __( 'Non-EU Shipment', 'postnl-for-woocommerce' ), |
| 528 |
'pickup' => __( 'Pick-up at PostNL', 'postnl-for-woocommerce' ), |
| 529 |
), |
| 530 |
), |
| 531 |
array( |
| 532 |
'id' => $this->prefix . 'default_shipping_options_nl', |
| 533 |
'type' => 'select', |
| 534 |
'label' => __( 'Shipping options domestic', 'postnl-for-woocommerce' ), |
| 535 |
'wrapper_class' => 'conditional nl', |
| 536 |
'container' => true, |
| 537 |
'value' => $this->settings->get_country_option( 'default_shipping_options_' . 'nl' ), |
| 538 |
'options' => $this->get_available_shipping_options_per_zone( 'nl' ), |
| 539 |
), |
| 540 |
array( |
| 541 |
'id' => $this->prefix . 'default_shipping_options_be', |
| 542 |
'type' => 'select', |
| 543 |
'label' => __( 'Belgium', 'postnl-for-woocommerce' ), |
| 544 |
'wrapper_class' => 'conditional be', |
| 545 |
'container' => true, |
| 546 |
'value' => $this->settings->get_country_option( 'default_shipping_options_' . 'be' ), |
| 547 |
'options' => $this->get_available_shipping_options_per_zone( 'be' ), |
| 548 |
), |
| 549 |
array( |
| 550 |
'id' => $this->prefix . 'default_shipping_options_eu', |
| 551 |
'type' => 'select', |
| 552 |
'label' => __( 'Shipping options EU', 'postnl-for-woocommerce' ), |
| 553 |
'wrapper_class' => 'conditional eu', |
| 554 |
'container' => true, |
| 555 |
'value' => $this->settings->get_country_option( 'default_shipping_options_' . 'eu' ), |
| 556 |
'options' => $this->get_available_shipping_options_per_zone( 'eu' ), |
| 557 |
), |
| 558 |
array( |
| 559 |
'id' => $this->prefix . 'default_shipping_options_row', |
| 560 |
'type' => 'select', |
| 561 |
'label' => __( 'Shipping options non-EU', 'postnl-for-woocommerce' ), |
| 562 |
'wrapper_class' => 'conditional row', |
| 563 |
'container' => true, |
| 564 |
'value' => $this->settings->get_country_option( 'default_shipping_options_' . 'row' ), |
| 565 |
'options' => $this->get_available_shipping_options_per_zone( 'row' ), |
| 566 |
), |
| 567 |
array( |
| 568 |
'id' => $this->prefix . 'default_shipping_options_pickup', |
| 569 |
'type' => 'select', |
| 570 |
'label' => __( 'Shipping options for Pick-up at PostNL', 'postnl-for-woocommerce' ), |
| 571 |
'wrapper_class' => 'conditional pickup', |
| 572 |
'container' => true, |
| 573 |
'value' => $this->settings->get_country_option( 'default_shipping_options_' . 'pickup' ), |
| 574 |
'options' => $this->get_available_shipping_options_per_zone( 'pickup' ), |
| 575 |
), |
| 576 |
); |
| 577 |
} |
| 578 |
|
| 579 |
/** |
| 580 |
* Collection of fields in create label bulk action. |
| 581 |
*/ |
| 582 |
public function modal_change_shipping_options() { |
| 583 |
$this->create_modal_content_wrapper( 'postnl-change-shipping-options', $this->change_shipping_options_fields() ); |
| 584 |
} |
| 585 |
|
| 586 |
/** |
| 587 |
* Display messages on order view screen. |
| 588 |
*/ |
| 589 |
public function render_messages() { |
| 590 |
$current_screen = get_current_screen(); |
| 591 |
|
| 592 |
$is_legacy_order = isset( $current_screen->id ) && in_array( |
| 593 |
$current_screen->id, |
| 594 |
array( |
| 595 |
'shop_order', |
| 596 |
'edit-shop_order', |
| 597 |
), |
| 598 |
true |
| 599 |
); |
| 600 |
$is_hpos_order = ! empty( $current_screen->id ) && 'woocommerce_page_wc-orders' === $current_screen->id; |
| 601 |
|
| 602 |
if ( $is_legacy_order || $is_hpos_order ) { |
| 603 |
|
| 604 |
$bulk_action_message_opt = get_option( $this->bulk_option_text_name ); |
| 605 |
|
| 606 |
if ( ( $bulk_action_message_opt ) && is_array( $bulk_action_message_opt ) ) { |
| 607 |
|
| 608 |
// Remove first element from array and verify if it is the user id. |
| 609 |
$user_id = array_shift( $bulk_action_message_opt ); |
| 610 |
if ( get_current_user_id() !== (int) $user_id ) { |
| 611 |
return; |
| 612 |
} |
| 613 |
|
| 614 |
foreach ( $bulk_action_message_opt as $key => $value ) { |
| 615 |
$message = $value['message']; |
| 616 |
$type = wp_kses_post( $value['type'] ); |
| 617 |
|
| 618 |
switch ( $type ) { |
| 619 |
case 'error': |
| 620 |
echo '<div class="notice notice-error is-dismissible"><ul><li>' . wp_kses_post( $message ) . '</li></ul></div>'; |
| 621 |
break; |
| 622 |
case 'success': |
| 623 |
echo '<div class="notice notice-success is-dismissible"><ul><li><strong>' . wp_kses_post( $message ) . '</strong></li></ul></div>'; |
| 624 |
break; |
| 625 |
default: |
| 626 |
echo '<div class="notice notice-warning is-dismissible"><ul><li><strong>' . wp_kses_post( $message ) . '</strong></li></ul></div>'; |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
delete_option( $this->bulk_option_text_name ); |
| 631 |
} |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
/** |
| 636 |
* Add Crete Label button to the action buttons within the order list table. |
| 637 |
* |
| 638 |
* @param array $actions Order actions. |
| 639 |
* @param \WC_Order $order Current order object. |
| 640 |
*/ |
| 641 |
public function add_create_label_actions_button( array $actions, \WC_Order $order ) { |
| 642 |
// Display the button for all orders that have a PostNL as a shipping method. |
| 643 |
if ( ! $this->is_postnl_shipping_method( $order ) ) { |
| 644 |
return $actions; |
| 645 |
} |
| 646 |
|
| 647 |
if ( $this->have_label_file( $order ) ) { |
| 648 |
$actions['postnl-label'] = array( |
| 649 |
'url' => $this->get_download_label_url( $order->get_id() ), |
| 650 |
'name' => esc_html__( 'PostNL Print Label', 'postnl-for-woocommerce' ), |
| 651 |
'action' => 'postnl-action-download-label', |
| 652 |
); |
| 653 |
} else { |
| 654 |
$actions['postnl-label'] = array( |
| 655 |
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=postnl_create_label&order_id=' . $order->get_id() ), 'postnl_create_label' ), |
| 656 |
'name' => esc_html__( 'PostNL Create Label', 'postnl-for-woocommerce' ), |
| 657 |
'action' => 'postnl-action-create-label', |
| 658 |
); |
| 659 |
} |
| 660 |
|
| 661 |
return $actions; |
| 662 |
} |
| 663 |
|
| 664 |
/** |
| 665 |
* Process PostNL by action button. |
| 666 |
*/ |
| 667 |
public function postnl_create_label_ajax() { |
| 668 |
if ( current_user_can( 'edit_shop_orders' ) && isset( $_GET['order_id'] ) ) { |
| 669 |
$order_id = absint( wp_unslash( $_GET['order_id'] ) ); |
| 670 |
|
| 671 |
$array_messages = array( |
| 672 |
'user_id' => get_current_user_id(), |
| 673 |
); |
| 674 |
$_REQUEST['postnl_create_return_label'] = 'yes'; |
| 675 |
$result = $this->generate_label_and_notes( $order_id, $_REQUEST ); |
| 676 |
$array_messages[] = $result['message']; |
| 677 |
|
| 678 |
update_option( $this->bulk_option_text_name, $array_messages ); |
| 679 |
} |
| 680 |
|
| 681 |
wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url( 'edit.php?post_type=shop_order' ) ); |
| 682 |
exit; |
| 683 |
} |
| 684 |
|
| 685 |
/** |
| 686 |
* Generate shipping label and add order note. |
| 687 |
* |
| 688 |
* @param int $order_id Order post ID. |
| 689 |
* @param array $post_data posted data. |
| 690 |
* |
| 691 |
* @return array Array of labels & messages. |
| 692 |
*/ |
| 693 |
public function generate_label_and_notes( $order_id, $post_data ) { |
| 694 |
$result = array(); |
| 695 |
|
| 696 |
try { |
| 697 |
$order = wc_get_order( $order_id ); |
| 698 |
$default_settings = $this->get_shipping_options( $order ); |
| 699 |
foreach ( $default_settings as $name => $value ) { |
| 700 |
if ( $value ) { |
| 701 |
$post_data[ $this->prefix . $name ] = $value; |
| 702 |
} |
| 703 |
} |
| 704 |
$result['labels_data'] = $this->save_meta_value( $order_id, $post_data ); |
| 705 |
|
| 706 |
$tracking_note = $this->get_tracking_note( $order_id ); |
| 707 |
$customer_note = false; |
| 708 |
|
| 709 |
if ( $this->settings->is_woocommerce_email_enabled() && ! empty( $tracking_note ) ) { |
| 710 |
$customer_note = true; |
| 711 |
} |
| 712 |
|
| 713 |
$order->add_order_note( $tracking_note, $customer_note ); |
| 714 |
$label_link = esc_url( $this->get_download_label_url( $order_id ) ); |
| 715 |
$result['message'] = array( |
| 716 |
// Translators: %1$s is the order ID, %2$s is the link to download the file, %3$s is the closing link tag. |
| 717 |
'message' => sprintf( |
| 718 |
esc_html__( '#%1$s : PostNL label has been created - %2$sdownload file%3$s', 'postnl-for-woocommerce' ), |
| 719 |
$order_id, |
| 720 |
'<a href="' . $label_link . '" download>', |
| 721 |
'</a>' |
| 722 |
), |
| 723 |
'type' => 'success', |
| 724 |
); |
| 725 |
} catch ( \Exception $e ) { |
| 726 |
$result['message'] = array( |
| 727 |
'message' => sprintf( '#%1$s : %2$s', $order_id, $e->getMessage() ), |
| 728 |
'type' => 'error', |
| 729 |
); |
| 730 |
} |
| 731 |
|
| 732 |
return $result; |
| 733 |
} |
| 734 |
} |
| 735 |
|