id ) ) {
return;
}
if ( ! empty( $screen->id ) && 'woocommerce_page_wc-settings' === $screen->id && ! empty( $_GET['section'] ) && POSTNL_SETTINGS_ID === wp_unslash( $_GET['section'] ) ) {
wp_enqueue_script(
'postnl-admin-settings',
POSTNL_WC_PLUGIN_DIR_URL . '/assets/js/admin-settings.js',
array( 'jquery' ),
POSTNL_WC_VERSION,
true
);
wp_enqueue_style(
'postnl-admin-settings-styles',
POSTNL_WC_PLUGIN_DIR_URL . '/assets/css/admin-settings.css',
array(),
POSTNL_WC_VERSION
);
}
if ( ( 'shop_order' === $screen->id && 'post' === $screen->base ) || 'woocommerce_page_wc-orders' === $screen->id ) {
wp_enqueue_style( 'postnl-admin-order-single', POSTNL_WC_PLUGIN_DIR_URL . '/assets/css/admin-order-single.css', array(), POSTNL_WC_VERSION );
wp_enqueue_script(
'postnl-admin-order-single',
POSTNL_WC_PLUGIN_DIR_URL . '/assets/js/admin-order-single.js',
array( 'jquery' ),
POSTNL_WC_VERSION,
true
);
wp_enqueue_script(
'postnl-admin-shipment-track-trace',
POSTNL_WC_PLUGIN_DIR_URL . '/assets/js/admin-shipment-track-trace.js',
array( 'jquery' ),
POSTNL_WC_VERSION,
true
);
wp_localize_script(
'postnl-admin-order-single',
'postnl_admin_order_obj',
array(
'prefix' => $this->prefix,
'fields' => array_map(
function ( $meta_field ) {
return empty( $meta_field['id'] ) ? '' : $meta_field['id'];
},
$this->meta_box_fields(),
),
)
);
}
}
/**
* Adding meta box in order admin page.
*
* @param String $post_type Post type for current admin page.
* @param WP_POST|WC_Order $post_or_order_object Either WP_Post or WC_Order object.
*/
public function add_meta_box( $post_type, $post_or_order_object ) {
$order = $this->init_order_object( $post_or_order_object );
if ( ! is_a( $order, 'WC_Order' ) || ! $this->is_postnl_shipping_method( $order ) ) {
return;
}
// translators: %s will be replaced by service name.
add_meta_box(
'woocommerce-shipment-postnl-label',
esc_html__( 'Label & Tracking', 'postnl-for-woocommerce' ),
array(
$this,
'meta_box_html',
),
Utils::get_order_screen_id(),
'side',
'high'
);
}
/**
* Add value to meta box fields.
*
* @param WC_Order $order current order object.
*
* @return array
*/
public function add_meta_box_value( $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) {
return array();
}
$meta_fields = $this->meta_box_fields( $order );
$order_data = $order->get_meta( $this->meta_name );
$option_map = Mapping::option_available_list();
$from_country = Utils::get_base_country();
$to_country = $order->get_shipping_country();
$to_state = $order->get_shipping_state();
$destination = Utils::get_shipping_zone( $to_country, $to_state );
// The persisted backend collapses the 24h/48h choice onto the generic
// 'letterbox' feature. Swap in the resolved variant so the override loop
// below re-checks (and, once a label exists, locks) the correct checkbox
// instead of re-checking the generic 24h box on top of the pre-selected
// 48h box, which would show both letterbox variants as selected.
if ( isset( $order_data['backend'] ) && is_array( $order_data['backend'] ) ) {
$order_data['backend'] = $this->apply_letterbox_display_variant( $order_data['backend'], $order );
}
foreach ( $meta_fields as $index => $field ) {
if ( isset( $field['nonce'] ) && true === $field['nonce'] ) {
continue;
}
$field_name = Utils::remove_prefix_field( $this->prefix, $field['id'] );
if ( ! empty( $order_data['frontend'][ $field_name ] ) ) {
$meta_fields[ $index ]['value'] = $order_data['frontend'][ $field_name ];
}
if ( isset( $order_data['backend'][ $field_name ] ) ) {
if ( $this->have_label_file( $order ) ) {
$meta_fields[ $index ]['custom_attributes']['disabled'] = 'disabled';
}
$meta_fields[ $index ]['value'] = $order_data['backend'][ $field_name ];
}
if ( isset( $option_map[ $from_country ][ $destination ] ) ) {
$meta_fields[ $index ]['standard_feat'] = in_array( $field_name, $option_map[ $from_country ][ $destination ] );
} else {
$meta_fields[ $index ]['standard_feat'] = false;
}
}
return $meta_fields;
}
/**
* Filter the fields to display the available fields only.
*
* @param Array $meta_fields Order meta fields.
* @param Array $available_options Available fields based on the countries and chosen option in checkout page.
*
* @return array
*/
public function filter_available_fields( $meta_fields, $available_options ) {
$meta_fields = array_filter(
$meta_fields,
function ( $field ) use ( $available_options ) {
$field_name = Utils::remove_prefix_field( $this->prefix, $field['id'] );
if ( true === $field['standard_feat'] ) {
return true;
}
if ( true === $field['const_field'] ) {
return true;
}
return in_array( $field_name, $available_options, true );
}
);
return $meta_fields;
}
/**
* Get dropoff points information.
*
* @param WC_Order $order Order object.
*
* @return Array.
*/
public function get_pickup_points_info( $order ) {
return $this->get_order_frontend_info( $order, 'dropoff_points_' );
}
/**
* Get delivery day information.
*
* @param WC_Order $order Order object.
*
* @return Array.
*/
public function get_delivery_day_info( $order ) {
return $this->get_order_frontend_info( $order, 'delivery_day_' );
}
/**
* Generate the dropoff points html information.
*
* @param WC_Order $order Order object.
*/
public function generate_delivery_type_html( $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) {
return;
}
$delivery_type = $this->get_delivery_type( $order );
if ( empty( $delivery_type ) ) {
return;
}
?>
$info_val ) {
// Convert to the Dutch date format, falling back to the stored value
// when it does not parse as Y-m-d so a malformed date cannot fatal the
// whole order edit screen via date_format( false, ... ).
$date_obj = date_create_from_format( 'Y-m-d', $info_val );
$dutch_date = ( false !== $date_obj ) ? date_format( $date_obj, 'd/m/Y' ) : $info_val;
?>