Plugin Directory

Changeset 3807488


Ignore:
Timestamp:
11/17/2026 01:28:28 PM (less than one hour ago)
Author:
posturinn
Message:

Release 1.5.2

Location:
posturinn
Files:
2 deleted
12 edited
1 copied

Legend:

Unmodified
Added
Removed
  • posturinn/tags/1.5.2/inc/hooks.php

    r3570342 r3807488  
    7272            wp_enqueue_script('postis-remodal', POSTIS_URL."/js/remodal.js", array('jquery'), '1.1.3', true);
    7373           
    74             wp_enqueue_script('postis-gm-js', POSTIS_URL."/js/postis-googlemap.js", array('jquery'), '1.1.3', true);
    75         }
    76     }
    77 }
    78 
    79 
    80 /**
    81 * Render Extra Content After Checkout Form (Google Map)
    82 **/
    83 function postis_hooks_after_checkout_form(){
    84    
     74            wp_enqueue_script('postis-gm-js', POSTIS_URL."/js/postis-googlemap.js", array('jquery'), postis_get_plugin_version(), true);
     75        }
     76    }
     77}
     78
     79
     80/**
     81* Build the Google Map postbox list (also refreshed through checkout fragments)
     82**/
     83function postis_get_googlemap_locations_html(){
     84
     85    $empty_list = '<ul class="postis-googlemap-locations"></ul>';
     86
     87    if ( ! WC()->session ) {
     88        return $empty_list;
     89    }
     90
    8591    // Get the exact same postbox services that the dropdown uses
    8692    $postbox_services = WC()->session->get('postis_postbox_services', array());
    87    
    88     if (!empty($postbox_services)) {
    89        
    90         // We have the same data as the dropdown, now we need to get coordinates
    91         // We'll need to call the postboxes API to get lat/lng for each service
    92         $postboxes_with_coords = array();
    93        
    94         // Get postboxes API data for coordinates
     93
     94    if (empty($postbox_services)) {
     95        return $empty_list;
     96    }
     97
     98    // Only DPO/DNO services are postboxes
     99    $postbox_service_list = array();
     100    foreach ($postbox_services as $service) {
     101        $base_service_id = substr($service['deliveryServiceId'], 0, 3);
     102        if ($base_service_id === 'DPO' || $base_service_id === 'DNO') {
     103            $postbox_service_list[] = $service;
     104        }
     105    }
     106
     107    if (empty($postbox_service_list)) {
     108        return $empty_list;
     109    }
     110
     111    // Coordinates come from the postboxes API; cached because this runs on every checkout refresh
     112    $postboxes_api_data = get_transient('postis_postboxes_coords');
     113
     114    if (empty($postboxes_api_data) || !is_array($postboxes_api_data)) {
     115        $postboxes_api_data = array();
     116
    95117        $endpoint_url = postis_get_endpoints('postboxes');
    96118        $postis_apikey = postis_get_api_key();
    97119        $domain = postis_get_homeurl();
    98        
     120
    99121        $req_headers = array(
    100122            "headers" => array(
    101                 'x-api-key' => $postis_apikey, 
     123                'x-api-key' => $postis_apikey,
    102124                'domain_name' => $domain,
    103125                'user-agent' => 'WooCommerce/'.postis_get_woocommerce_version().' | Postis/'.postis_get_plugin_version(),
    … …  
    106128            )
    107129        );
    108        
     130
    109131        $response = wp_remote_get($endpoint_url, $req_headers);
    110         $postboxes_api_data = array();
    111        
    112         if (!is_wp_error($response) && $response['response']['code'] == 200) {
     132
     133        if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) == 200) {
    113134            $postboxes_response = json_decode(wp_remote_retrieve_body($response), true);
    114            
    115             if (isset($postboxes_response['postboxes'])) {
     135
     136            if (!empty($postboxes_response['postboxes'])) {
    116137                // Index by postbox ID for quick lookup
    117138                foreach ($postboxes_response['postboxes'] as $postbox) {
    118139                    $postboxes_api_data[$postbox['postboxId']] = $postbox;
    119140                }
     141
     142                set_transient('postis_postboxes_coords', $postboxes_api_data, HOUR_IN_SECONDS);
    120143            }
    121144        }
    122        
    123         // Now combine the session services with coordinates from API
    124         foreach ($postbox_services as $service) {
    125             $service_id = $service['deliveryServiceId'];
    126             $base_service_id = substr($service_id, 0, 3);
    127            
    128             if ($base_service_id === 'DPO' || $base_service_id === 'DNO') {
    129                 // Extract postbox ID (remove DPO/DNO prefix)
    130                 $postbox_id = substr($service_id, 3);
    131                
    132                 // Get coordinates from API data
    133                 $coords = isset($postboxes_api_data[$postbox_id]) ? $postboxes_api_data[$postbox_id] : array();
    134                
    135                 $postboxes_with_coords[] = array(
    136                     'deliveryServiceId' => $service_id, // Full ID like DPO9556
    137                     'postboxId' => $postbox_id,
    138                     'name' => $service['nameLong'],
    139                     'postcode' => isset($coords['postcode']) ? $coords['postcode'] : '',
    140                     'address' => isset($coords['address']) ? $coords['address'] : '',
    141                     'town' => isset($coords['town']) ? $coords['town'] : '',
    142                     'latitude' => isset($coords['latitude']) ? $coords['latitude'] : '',
    143                     'longitude' => isset($coords['longitude']) ? $coords['longitude'] : '',
    144                 );
    145             }
    146         }
    147        
    148         if (!empty($postboxes_with_coords)) {
     145    }
     146
     147    // Combine the session services with coordinates from API
     148    $postboxes_with_coords = array();
     149    foreach ($postbox_service_list as $service) {
     150        $service_id = $service['deliveryServiceId'];
     151        // Extract postbox ID (remove DPO/DNO prefix)
     152        $postbox_id = substr($service_id, 3);
     153
     154        $coords = isset($postboxes_api_data[$postbox_id]) ? $postboxes_api_data[$postbox_id] : array();
     155
     156        $postboxes_with_coords[] = array(
     157            'deliveryServiceId' => $service_id, // Full ID like DPO9556
     158            'postboxId' => $postbox_id,
     159            'name' => $service['nameLong'],
     160            'postcode' => isset($coords['postcode']) ? $coords['postcode'] : '',
     161            'address' => isset($coords['address']) ? $coords['address'] : '',
     162            'town' => isset($coords['town']) ? $coords['town'] : '',
     163            'latitude' => isset($coords['latitude']) ? $coords['latitude'] : '',
     164            'longitude' => isset($coords['longitude']) ? $coords['longitude'] : '',
     165        );
     166    }
     167
     168    ob_start();
     169    ?>
     170    <ul class="postis-googlemap-locations">
     171        <?php
     172        foreach ($postboxes_with_coords as $index => $meta) {
     173            $postboxId = $meta['postboxId'];
     174            $deliveryServiceId = $meta['deliveryServiceId'];
     175            $postcode = $meta['postcode'];
     176            $name = $meta['name'];
     177            $latitude = $meta['latitude'];
     178            $longitude = $meta['longitude'];
     179        ?>
     180              <li class="postis-googlemap-location"
     181                  data-postboxes="<?php echo esc_attr(json_encode($meta, JSON_FORCE_OBJECT)); ?>"
     182                  data-lat="<?php echo esc_attr($latitude); ?>"
     183                  data-long="<?php echo esc_attr($longitude); ?>"
     184                  data-postbox-id="<?php echo esc_attr($postboxId); ?>"
     185                  data-delivery-service-id="<?php echo esc_attr($deliveryServiceId); ?>">
     186                  <span><?php echo esc_html($name); ?></span>
     187                  <span>(<?php echo esc_html($postcode); ?>)</span>
     188              </li>
     189        <?php
     190        }
     191        ?>
     192    </ul>
     193    <?php
     194    return ob_get_clean();
     195}
     196
     197
     198/**
     199* Render Extra Content After Checkout Form (Google Map)
     200**/
     201function postis_hooks_after_checkout_form(){
     202
     203    // Always render the dialog: it sits outside the parts WooCommerce refreshes,
     204    // so the list inside is kept current through the checkout fragments instead.
    149205    ?>
    150206    <div data-remodal-id="postis_googlemap_modal" role="dialog">
    … …  
    155211            <div class="postis-col-wrapper">
    156212                <div class="postis-postboxes-section">
    157                     <ul>
    158                         <?php
    159                         foreach ($postboxes_with_coords as $index => $meta) {
    160                             $postboxId = $meta['postboxId'];
    161                             $deliveryServiceId = $meta['deliveryServiceId'];
    162                             $postcode = $meta['postcode'];
    163                             $name = $meta['name'];
    164                             $latitude = $meta['latitude'];
    165                             $longitude = $meta['longitude'];
    166                         ?>
    167                               <li class="postis-googlemap-location"
    168                                   data-postboxes="<?php echo esc_attr(json_encode($meta, JSON_FORCE_OBJECT)); ?>"
    169                                   data-lat="<?php echo esc_attr($latitude); ?>"
    170                                   data-long="<?php echo esc_attr($longitude); ?>"
    171                                   data-postbox-id="<?php echo esc_attr($postboxId); ?>"
    172                                   data-delivery-service-id="<?php echo esc_attr($deliveryServiceId); ?>">
    173                                   <span><?php echo esc_html($name); ?></span>
    174                                   <span>(<?php echo esc_html($postcode); ?>)</span>
    175                               </li>
    176                         <?php
    177                         }
    178                         ?>
    179                     </ul>
     213                    <?php echo postis_get_googlemap_locations_html(); ?>
    180214                    <button data-remodal-action="confirm" class="postis-postbox-js remodal-confirm"><?php echo __("Choose Postbox", "postis"); ?></button>
    181215                </div>
    … …  
    187221    </div>
    188222    <?php
    189         }
    190     }
    191 }
     223}
     224
     225
     226/**
     227* Refresh the Google Map postbox list on checkout updates
     228**/
     229function postis_hooks_googlemap_fragments( $fragments ){
     230
     231    $fragments['ul.postis-googlemap-locations'] = postis_get_googlemap_locations_html();
     232
     233    return $fragments;
     234}
     235
     236
     237/**
     238* Keep the customer's chosen Pósturinn service when the rates change
     239**/
     240function postis_hooks_keep_chosen_shipping_service( $default, $rates, $chosen_method ){
     241
     242    $pattern = '/^(postis:)?([A-Z]{3})[0-9]*$/';
     243
     244    // Only act on our own rates (others look like local_pickup:1, flat_rate:3, free_shipping).
     245    if ( ! is_string( $chosen_method ) || ! preg_match( $pattern, $chosen_method, $chosen_match ) ) {
     246        return $default;
     247    }
     248
     249    if ( ! is_array( $rates ) ) {
     250        return $default;
     251    }
     252
     253    // WooCommerce resets to the first rate whenever the rate list changes, and our
     254    // rate IDs vary per postcode, so match on the service instead of the exact ID.
     255    if ( isset( $rates[ $chosen_method ] ) ) {
     256        return $chosen_method;
     257    }
     258
     259    // DPH and DPL are both home delivery.
     260    $same_service = array( 'DPL' => 'DPH' );
     261    $chosen_code  = isset( $same_service[ $chosen_match[2] ] ) ? $same_service[ $chosen_match[2] ] : $chosen_match[2];
     262
     263    foreach ( $rates as $rate_id => $rate ) {
     264        if ( ! is_object( $rate ) || $rate->get_method_id() !== 'postis' || ! preg_match( $pattern, $rate_id, $rate_match ) ) {
     265            continue;
     266        }
     267
     268        $rate_code = isset( $same_service[ $rate_match[2] ] ) ? $same_service[ $rate_match[2] ] : $rate_match[2];
     269
     270        if ( $rate_code === $chosen_code ) {
     271            return $rate_id;
     272        }
     273    }
     274
     275    return $default;
     276}
  • posturinn/tags/1.5.2/inc_hpos/admin.class.php

    r3390272 r3807488  
    736736   
    737737    function bulk_admin_notices() {
    738         global $post_type, $pagenow;
    739 
    740         if ('edit.php' !== $pagenow || 'shop_order' !== $post_type || !isset($_REQUEST['bulk_action'])) {
     738        $screen = function_exists('get_current_screen') ? get_current_screen() : null;
     739
     740        // With HPOS the orders list is admin.php?page=wc-orders, not edit.php?post_type=shop_order.
     741        if (!$screen || wc_get_page_screen_id('shop-order') !== $screen->id || !isset($_REQUEST['bulk_action'])) {
    741742            return;
    742743        }
  • posturinn/tags/1.5.2/inc_hpos/hooks.php

    r3570342 r3807488  
    7373            wp_enqueue_script('postis-remodal', POSTIS_URL."/js/remodal.js", array('jquery'), '1.1.3', true);
    7474           
    75             wp_enqueue_script('postis-gm-js', POSTIS_URL."/js/postis-googlemap.js", array('jquery'), '1.1.3', true);
    76         }
    77     }
    78 }
    79 
    80 
    81 /**
    82 * Render Extra Content After Checkout Form (Google Map)
    83 **/
    84 function postis_hooks_after_checkout_form(){
    85    
     75            wp_enqueue_script('postis-gm-js', POSTIS_URL."/js/postis-googlemap.js", array('jquery'), postis_get_plugin_version(), true);
     76        }
     77    }
     78}
     79
     80
     81/**
     82* Build the Google Map postbox list (also refreshed through checkout fragments)
     83**/
     84function postis_get_googlemap_locations_html(){
     85
     86    $empty_list = '<ul class="postis-googlemap-locations"></ul>';
     87
     88    if ( ! WC()->session ) {
     89        return $empty_list;
     90    }
     91
    8692    // Get the exact same postbox services that the dropdown uses
    8793    $postbox_services = WC()->session->get('postis_postbox_services', array());
    88    
    89     if (!empty($postbox_services)) {
    90        
    91         // We have the same data as the dropdown, now we need to get coordinates
    92         // We'll need to call the postboxes API to get lat/lng for each service
    93         $postboxes_with_coords = array();
    94        
    95         // Get postboxes API data for coordinates
     94
     95    if (empty($postbox_services)) {
     96        return $empty_list;
     97    }
     98
     99    // Only DPO/DNO services are postboxes
     100    $postbox_service_list = array();
     101    foreach ($postbox_services as $service) {
     102        $base_service_id = substr($service['deliveryServiceId'], 0, 3);
     103        if ($base_service_id === 'DPO' || $base_service_id === 'DNO') {
     104            $postbox_service_list[] = $service;
     105        }
     106    }
     107
     108    if (empty($postbox_service_list)) {
     109        return $empty_list;
     110    }
     111
     112    // Coordinates come from the postboxes API; cached because this runs on every checkout refresh
     113    $postboxes_api_data = get_transient('postis_postboxes_coords');
     114
     115    if (empty($postboxes_api_data) || !is_array($postboxes_api_data)) {
     116        $postboxes_api_data = array();
     117
    96118        $endpoint_url = postis_get_endpoints('postboxes');
    97119        $postis_apikey = postis_get_api_key();
    98120        $domain = postis_get_homeurl();
    99        
     121
    100122        $req_headers = array(
    101123            "headers" => array(
    102                 'x-api-key' => $postis_apikey, 
     124                'x-api-key' => $postis_apikey,
    103125                'domain_name' => $domain,
    104126                'user-agent' => 'WooCommerce/'.postis_get_woocommerce_version().' | Postis/'.postis_get_plugin_version(),
    … …  
    107129            )
    108130        );
    109        
     131
    110132        $response = wp_remote_get($endpoint_url, $req_headers);
    111         $postboxes_api_data = array();
    112        
    113         if (!is_wp_error($response) && $response['response']['code'] == 200) {
     133
     134        if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) == 200) {
    114135            $postboxes_response = json_decode(wp_remote_retrieve_body($response), true);
    115            
    116             if (isset($postboxes_response['postboxes'])) {
     136
     137            if (!empty($postboxes_response['postboxes'])) {
    117138                // Index by postbox ID for quick lookup
    118139                foreach ($postboxes_response['postboxes'] as $postbox) {
    119140                    $postboxes_api_data[$postbox['postboxId']] = $postbox;
    120141                }
     142
     143                set_transient('postis_postboxes_coords', $postboxes_api_data, HOUR_IN_SECONDS);
    121144            }
    122145        }
    123        
    124         // Now combine the session services with coordinates from API
    125         foreach ($postbox_services as $service) {
    126             $service_id = $service['deliveryServiceId'];
    127             $base_service_id = substr($service_id, 0, 3);
    128            
    129             if ($base_service_id === 'DPO' || $base_service_id === 'DNO') {
    130                 // Extract postbox ID (remove DPO/DNO prefix)
    131                 $postbox_id = substr($service_id, 3);
    132                
    133                 // Get coordinates from API data
    134                 $coords = isset($postboxes_api_data[$postbox_id]) ? $postboxes_api_data[$postbox_id] : array();
    135                
    136                 $postboxes_with_coords[] = array(
    137                     'deliveryServiceId' => $service_id, // Full ID like DPO9556
    138                     'postboxId' => $postbox_id,
    139                     'name' => $service['nameLong'],
    140                     'postcode' => isset($coords['postcode']) ? $coords['postcode'] : '',
    141                     'address' => isset($coords['address']) ? $coords['address'] : '',
    142                     'town' => isset($coords['town']) ? $coords['town'] : '',
    143                     'latitude' => isset($coords['latitude']) ? $coords['latitude'] : '',
    144                     'longitude' => isset($coords['longitude']) ? $coords['longitude'] : '',
    145                 );
    146             }
    147         }
    148        
    149         if (!empty($postboxes_with_coords)) {
     146    }
     147
     148    // Combine the session services with coordinates from API
     149    $postboxes_with_coords = array();
     150    foreach ($postbox_service_list as $service) {
     151        $service_id = $service['deliveryServiceId'];
     152        // Extract postbox ID (remove DPO/DNO prefix)
     153        $postbox_id = substr($service_id, 3);
     154
     155        $coords = isset($postboxes_api_data[$postbox_id]) ? $postboxes_api_data[$postbox_id] : array();
     156
     157        $postboxes_with_coords[] = array(
     158            'deliveryServiceId' => $service_id, // Full ID like DPO9556
     159            'postboxId' => $postbox_id,
     160            'name' => $service['nameLong'],
     161            'postcode' => isset($coords['postcode']) ? $coords['postcode'] : '',
     162            'address' => isset($coords['address']) ? $coords['address'] : '',
     163            'town' => isset($coords['town']) ? $coords['town'] : '',
     164            'latitude' => isset($coords['latitude']) ? $coords['latitude'] : '',
     165            'longitude' => isset($coords['longitude']) ? $coords['longitude'] : '',
     166        );
     167    }
     168
     169    ob_start();
     170    ?>
     171    <ul class="postis-googlemap-locations">
     172        <?php
     173        foreach ($postboxes_with_coords as $index => $meta) {
     174            $postboxId = $meta['postboxId'];
     175            $deliveryServiceId = $meta['deliveryServiceId'];
     176            $postcode = $meta['postcode'];
     177            $name = $meta['name'];
     178            $latitude = $meta['latitude'];
     179            $longitude = $meta['longitude'];
     180        ?>
     181              <li class="postis-googlemap-location"
     182                  data-postboxes="<?php echo esc_attr(json_encode($meta, JSON_FORCE_OBJECT)); ?>"
     183                  data-lat="<?php echo esc_attr($latitude); ?>"
     184                  data-long="<?php echo esc_attr($longitude); ?>"
     185                  data-postbox-id="<?php echo esc_attr($postboxId); ?>"
     186                  data-delivery-service-id="<?php echo esc_attr($deliveryServiceId); ?>">
     187                  <span><?php echo esc_html($name); ?></span>
     188                  <span>(<?php echo esc_html($postcode); ?>)</span>
     189              </li>
     190        <?php
     191        }
     192        ?>
     193    </ul>
     194    <?php
     195    return ob_get_clean();
     196}
     197
     198
     199/**
     200* Render Extra Content After Checkout Form (Google Map)
     201**/
     202function postis_hooks_after_checkout_form(){
     203
     204    // Always render the dialog: it sits outside the parts WooCommerce refreshes,
     205    // so the list inside is kept current through the checkout fragments instead.
    150206    ?>
    151207    <div data-remodal-id="postis_googlemap_modal" role="dialog">
    … …  
    156212            <div class="postis-col-wrapper">
    157213                <div class="postis-postboxes-section">
    158                     <ul>
    159                         <?php
    160                         foreach ($postboxes_with_coords as $index => $meta) {
    161                             $postboxId = $meta['postboxId'];
    162                             $deliveryServiceId = $meta['deliveryServiceId'];
    163                             $postcode = $meta['postcode'];
    164                             $name = $meta['name'];
    165                             $latitude = $meta['latitude'];
    166                             $longitude = $meta['longitude'];
    167                         ?>
    168                               <li class="postis-googlemap-location"
    169                                   data-postboxes="<?php echo esc_attr(json_encode($meta, JSON_FORCE_OBJECT)); ?>"
    170                                   data-lat="<?php echo esc_attr($latitude); ?>"
    171                                   data-long="<?php echo esc_attr($longitude); ?>"
    172                                   data-postbox-id="<?php echo esc_attr($postboxId); ?>"
    173                                   data-delivery-service-id="<?php echo esc_attr($deliveryServiceId); ?>">
    174                                   <span><?php echo esc_html($name); ?></span>
    175                                   <span>(<?php echo esc_html($postcode); ?>)</span>
    176                               </li>
    177                         <?php
    178                         }
    179                         ?>
    180                     </ul>
     214                    <?php echo postis_get_googlemap_locations_html(); ?>
    181215                    <button data-remodal-action="confirm" class="postis-postbox-js remodal-confirm"><?php echo __("Choose Postbox", "postis"); ?></button>
    182216                </div>
    … …  
    188222    </div>
    189223    <?php
    190         }
    191     }
    192 }
     224}
     225
     226
     227/**
     228* Refresh the Google Map postbox list on checkout updates
     229**/
     230function postis_hooks_googlemap_fragments( $fragments ){
     231
     232    $fragments['ul.postis-googlemap-locations'] = postis_get_googlemap_locations_html();
     233
     234    return $fragments;
     235}
     236
     237
     238/**
     239* Keep the customer's chosen Pósturinn service when the rates change
     240**/
     241function postis_hooks_keep_chosen_shipping_service( $default, $rates, $chosen_method ){
     242
     243    $pattern = '/^(postis:)?([A-Z]{3})[0-9]*$/';
     244
     245    // Only act on our own rates (others look like local_pickup:1, flat_rate:3, free_shipping).
     246    if ( ! is_string( $chosen_method ) || ! preg_match( $pattern, $chosen_method, $chosen_match ) ) {
     247        return $default;
     248    }
     249
     250    if ( ! is_array( $rates ) ) {
     251        return $default;
     252    }
     253
     254    // WooCommerce resets to the first rate whenever the rate list changes, and our
     255    // rate IDs vary per postcode, so match on the service instead of the exact ID.
     256    if ( isset( $rates[ $chosen_method ] ) ) {
     257        return $chosen_method;
     258    }
     259
     260    // DPH and DPL are both home delivery.
     261    $same_service = array( 'DPL' => 'DPH' );
     262    $chosen_code  = isset( $same_service[ $chosen_match[2] ] ) ? $same_service[ $chosen_match[2] ] : $chosen_match[2];
     263
     264    foreach ( $rates as $rate_id => $rate ) {
     265        if ( ! is_object( $rate ) || $rate->get_method_id() !== 'postis' || ! preg_match( $pattern, $rate_id, $rate_match ) ) {
     266            continue;
     267        }
     268
     269        $rate_code = isset( $same_service[ $rate_match[2] ] ) ? $same_service[ $rate_match[2] ] : $rate_match[2];
     270
     271        if ( $rate_code === $chosen_code ) {
     272            return $rate_id;
     273        }
     274    }
     275
     276    return $default;
     277}
  • posturinn/tags/1.5.2/js/postis-googlemap.js

    r3302192 r3807488  
    22jQuery(function($) {
    33    /**
    4      * Load Google Map Modal
     4     * Get Google Map Modal (null when the dialog isn't in the DOM)
    55     */
    6     var inst = $('[data-remodal-id=postis_googlemap_modal]').remodal();
     6    function getPostboxModal() {
     7        var $el = $('[data-remodal-id=postis_googlemap_modal]');
     8        return $el.length ? $el.remodal() : null;
     9    }
    710
    811    /**
    … …  
    1013     */
    1114    $(document.body).on('updated_checkout', function() {
     15        // The postbox list was just replaced by the checkout fragment, so refresh the markers.
     16        if (typeof map !== 'undefined' && map) {
     17            addInitialPostboxMarkers();
     18            map.setCenter({ lat: 64.9995555, lng: -18.9839165 });
     19            map.setZoom(6);
     20        } else if (typeof google !== 'undefined' && google.maps && document.getElementById('postis-map')) {
     21            initMap();
     22        }
     23
    1224        const shipping_wrapper = $('.woocommerce-shipping-methods');
    1325        const get_shipping_method = shipping_wrapper.find('.shipping_method:checked').val();
    … …  
    2537        ) {
    2638            $('#postis_dpo_postbox_field').hide();
    27             inst.open();
     39            var inst = getPostboxModal();
     40            if (inst) {
     41                inst.open();
     42            }
    2843        }
    2944    });
    … …  
    7489    $(document).on('click', '.postis-googlemap-modal-btn', function(e) {
    7590        e.preventDefault();
    76         inst.open();
     91        var inst = getPostboxModal();
     92        if (inst) {
     93            inst.open();
     94        }
    7795    });
    7896});
    … …  
    86104
    87105function initMap() {
     106    if (typeof google === 'undefined' || !document.getElementById("postis-map")) {
     107        return;
     108    }
     109
    88110    const Iceland = { lat: 64.9995555, lng: -18.9839165 };
    89111    map = new google.maps.Map(document.getElementById("postis-map"), {
    … …  
    168190
    169191// Load map once window is ready
    170 google.maps.event.addDomListener(window, 'load', initMap);
     192if (typeof google !== 'undefined' && google.maps) {
     193    google.maps.event.addDomListener(window, 'load', initMap);
     194}
  • posturinn/tags/1.5.2/readme.txt

    r3570342 r3807488  
    33Tags: shipping, posturinn, icelandic post shipping, shipping rates, woocommerce
    44Requires at least: 4.3
    5 Tested up to: 7.0
     5Tested up to: 7.1
    66Requires PHP: 7.4
    7 Stable tag: 1.5.1
     7Stable tag: 1.5.2
    88License: GPL2
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
    … …  
    1717
    1818This plugin is only for companies that have a registered user in Mappan.
     19
     20Tested up to PHP 8.2.
    1921
    2022== Installation ==
    … …  
    2628
    2729== Changelog ==
     30= 1.5.2 =
     31Fixed the "Choose Postbox" map popup not opening on the first checkout visit, and its postbox list now follows postcode changes
     32The chosen Pósturinn shipping method is kept when the postcode changes instead of jumping to another method
     33Fixed bulk "Pósturinn - Print Shipment PDF" and other bulk action notices not showing on the HPOS orders screen
     34
    2835= 1.5.1 =
    2936minor changes
  • posturinn/tags/1.5.2/wc-posturinn-shipping-api.php

    r3570342 r3807488  
    33 * Plugin Name: Pósturinn's Shipping with WooCommerce
    44 * Description: This plugin is an WooCommerce extension to extend shipping method with Pósturinn's - Iceland
    5  * Version: 1.5.1
     5 * Version: 1.5.2
    66 * Author: Pósturinn
    77 * Author URI: https://postur.is
    … …  
    8787        if ($gm_enable == 'yes') {
    8888            add_action( 'woocommerce_after_checkout_form', 'postis_hooks_after_checkout_form', 20 );
    89         }
     89            add_filter( 'woocommerce_update_order_review_fragments', 'postis_hooks_googlemap_fragments' );
     90        }
     91
     92        add_filter( 'woocommerce_shipping_chosen_method', 'postis_hooks_keep_chosen_shipping_service', 10, 3 );
    9093       
    9194        $plugin_name = plugin_basename( __FILE__ );
  • posturinn/trunk/inc/hooks.php

    r3570342 r3807488  
    7272            wp_enqueue_script('postis-remodal', POSTIS_URL."/js/remodal.js", array('jquery'), '1.1.3', true);
    7373           
    74             wp_enqueue_script('postis-gm-js', POSTIS_URL."/js/postis-googlemap.js", array('jquery'), '1.1.3', true);
    75         }
    76     }
    77 }
    78 
    79 
    80 /**
    81 * Render Extra Content After Checkout Form (Google Map)
    82 **/
    83 function postis_hooks_after_checkout_form(){
    84    
     74            wp_enqueue_script('postis-gm-js', POSTIS_URL."/js/postis-googlemap.js", array('jquery'), postis_get_plugin_version(), true);
     75        }
     76    }
     77}
     78
     79
     80/**
     81* Build the Google Map postbox list (also refreshed through checkout fragments)
     82**/
     83function postis_get_googlemap_locations_html(){
     84
     85    $empty_list = '<ul class="postis-googlemap-locations"></ul>';
     86
     87    if ( ! WC()->session ) {
     88        return $empty_list;
     89    }
     90
    8591    // Get the exact same postbox services that the dropdown uses
    8692    $postbox_services = WC()->session->get('postis_postbox_services', array());
    87    
    88     if (!empty($postbox_services)) {
    89        
    90         // We have the same data as the dropdown, now we need to get coordinates
    91         // We'll need to call the postboxes API to get lat/lng for each service
    92         $postboxes_with_coords = array();
    93        
    94         // Get postboxes API data for coordinates
     93
     94    if (empty($postbox_services)) {
     95        return $empty_list;
     96    }
     97
     98    // Only DPO/DNO services are postboxes
     99    $postbox_service_list = array();
     100    foreach ($postbox_services as $service) {
     101        $base_service_id = substr($service['deliveryServiceId'], 0, 3);
     102        if ($base_service_id === 'DPO' || $base_service_id === 'DNO') {
     103            $postbox_service_list[] = $service;
     104        }
     105    }
     106
     107    if (empty($postbox_service_list)) {
     108        return $empty_list;
     109    }
     110
     111    // Coordinates come from the postboxes API; cached because this runs on every checkout refresh
     112    $postboxes_api_data = get_transient('postis_postboxes_coords');
     113
     114    if (empty($postboxes_api_data) || !is_array($postboxes_api_data)) {
     115        $postboxes_api_data = array();
     116
    95117        $endpoint_url = postis_get_endpoints('postboxes');
    96118        $postis_apikey = postis_get_api_key();
    97119        $domain = postis_get_homeurl();
    98        
     120
    99121        $req_headers = array(
    100122            "headers" => array(
    101                 'x-api-key' => $postis_apikey, 
     123                'x-api-key' => $postis_apikey,
    102124                'domain_name' => $domain,
    103125                'user-agent' => 'WooCommerce/'.postis_get_woocommerce_version().' | Postis/'.postis_get_plugin_version(),
    … …  
    106128            )
    107129        );
    108        
     130
    109131        $response = wp_remote_get($endpoint_url, $req_headers);
    110         $postboxes_api_data = array();
    111        
    112         if (!is_wp_error($response) && $response['response']['code'] == 200) {
     132
     133        if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) == 200) {
    113134            $postboxes_response = json_decode(wp_remote_retrieve_body($response), true);
    114            
    115             if (isset($postboxes_response['postboxes'])) {
     135
     136            if (!empty($postboxes_response['postboxes'])) {
    116137                // Index by postbox ID for quick lookup
    117138                foreach ($postboxes_response['postboxes'] as $postbox) {
    118139                    $postboxes_api_data[$postbox['postboxId']] = $postbox;
    119140                }
     141
     142                set_transient('postis_postboxes_coords', $postboxes_api_data, HOUR_IN_SECONDS);
    120143            }
    121144        }
    122        
    123         // Now combine the session services with coordinates from API
    124         foreach ($postbox_services as $service) {
    125             $service_id = $service['deliveryServiceId'];
    126             $base_service_id = substr($service_id, 0, 3);
    127            
    128             if ($base_service_id === 'DPO' || $base_service_id === 'DNO') {
    129                 // Extract postbox ID (remove DPO/DNO prefix)
    130                 $postbox_id = substr($service_id, 3);
    131                
    132                 // Get coordinates from API data
    133                 $coords = isset($postboxes_api_data[$postbox_id]) ? $postboxes_api_data[$postbox_id] : array();
    134                
    135                 $postboxes_with_coords[] = array(
    136                     'deliveryServiceId' => $service_id, // Full ID like DPO9556
    137                     'postboxId' => $postbox_id,
    138                     'name' => $service['nameLong'],
    139                     'postcode' => isset($coords['postcode']) ? $coords['postcode'] : '',
    140                     'address' => isset($coords['address']) ? $coords['address'] : '',
    141                     'town' => isset($coords['town']) ? $coords['town'] : '',
    142                     'latitude' => isset($coords['latitude']) ? $coords['latitude'] : '',
    143                     'longitude' => isset($coords['longitude']) ? $coords['longitude'] : '',
    144                 );
    145             }
    146         }
    147        
    148         if (!empty($postboxes_with_coords)) {
     145    }
     146
     147    // Combine the session services with coordinates from API
     148    $postboxes_with_coords = array();
     149    foreach ($postbox_service_list as $service) {
     150        $service_id = $service['deliveryServiceId'];
     151        // Extract postbox ID (remove DPO/DNO prefix)
     152        $postbox_id = substr($service_id, 3);
     153
     154        $coords = isset($postboxes_api_data[$postbox_id]) ? $postboxes_api_data[$postbox_id] : array();
     155
     156        $postboxes_with_coords[] = array(
     157            'deliveryServiceId' => $service_id, // Full ID like DPO9556
     158            'postboxId' => $postbox_id,
     159            'name' => $service['nameLong'],
     160            'postcode' => isset($coords['postcode']) ? $coords['postcode'] : '',
     161            'address' => isset($coords['address']) ? $coords['address'] : '',
     162            'town' => isset($coords['town']) ? $coords['town'] : '',
     163            'latitude' => isset($coords['latitude']) ? $coords['latitude'] : '',
     164            'longitude' => isset($coords['longitude']) ? $coords['longitude'] : '',
     165        );
     166    }
     167
     168    ob_start();
     169    ?>
     170    <ul class="postis-googlemap-locations">
     171        <?php
     172        foreach ($postboxes_with_coords as $index => $meta) {
     173            $postboxId = $meta['postboxId'];
     174            $deliveryServiceId = $meta['deliveryServiceId'];
     175            $postcode = $meta['postcode'];
     176            $name = $meta['name'];
     177            $latitude = $meta['latitude'];
     178            $longitude = $meta['longitude'];
     179        ?>
     180              <li class="postis-googlemap-location"
     181                  data-postboxes="<?php echo esc_attr(json_encode($meta, JSON_FORCE_OBJECT)); ?>"
     182                  data-lat="<?php echo esc_attr($latitude); ?>"
     183                  data-long="<?php echo esc_attr($longitude); ?>"
     184                  data-postbox-id="<?php echo esc_attr($postboxId); ?>"
     185                  data-delivery-service-id="<?php echo esc_attr($deliveryServiceId); ?>">
     186                  <span><?php echo esc_html($name); ?></span>
     187                  <span>(<?php echo esc_html($postcode); ?>)</span>
     188              </li>
     189        <?php
     190        }
     191        ?>
     192    </ul>
     193    <?php
     194    return ob_get_clean();
     195}
     196
     197
     198/**
     199* Render Extra Content After Checkout Form (Google Map)
     200**/
     201function postis_hooks_after_checkout_form(){
     202
     203    // Always render the dialog: it sits outside the parts WooCommerce refreshes,
     204    // so the list inside is kept current through the checkout fragments instead.
    149205    ?>
    150206    <div data-remodal-id="postis_googlemap_modal" role="dialog">
    … …  
    155211            <div class="postis-col-wrapper">
    156212                <div class="postis-postboxes-section">
    157                     <ul>
    158                         <?php
    159                         foreach ($postboxes_with_coords as $index => $meta) {
    160                             $postboxId = $meta['postboxId'];
    161                             $deliveryServiceId = $meta['deliveryServiceId'];
    162                             $postcode = $meta['postcode'];
    163                             $name = $meta['name'];
    164                             $latitude = $meta['latitude'];
    165                             $longitude = $meta['longitude'];
    166                         ?>
    167                               <li class="postis-googlemap-location"
    168                                   data-postboxes="<?php echo esc_attr(json_encode($meta, JSON_FORCE_OBJECT)); ?>"
    169                                   data-lat="<?php echo esc_attr($latitude); ?>"
    170                                   data-long="<?php echo esc_attr($longitude); ?>"
    171                                   data-postbox-id="<?php echo esc_attr($postboxId); ?>"
    172                                   data-delivery-service-id="<?php echo esc_attr($deliveryServiceId); ?>">
    173                                   <span><?php echo esc_html($name); ?></span>
    174                                   <span>(<?php echo esc_html($postcode); ?>)</span>
    175                               </li>
    176                         <?php
    177                         }
    178                         ?>
    179                     </ul>
     213                    <?php echo postis_get_googlemap_locations_html(); ?>
    180214                    <button data-remodal-action="confirm" class="postis-postbox-js remodal-confirm"><?php echo __("Choose Postbox", "postis"); ?></button>
    181215                </div>
    … …  
    187221    </div>
    188222    <?php
    189         }
    190     }
    191 }
     223}
     224
     225
     226/**
     227* Refresh the Google Map postbox list on checkout updates
     228**/
     229function postis_hooks_googlemap_fragments( $fragments ){
     230
     231    $fragments['ul.postis-googlemap-locations'] = postis_get_googlemap_locations_html();
     232
     233    return $fragments;
     234}
     235
     236
     237/**
     238* Keep the customer's chosen Pósturinn service when the rates change
     239**/
     240function postis_hooks_keep_chosen_shipping_service( $default, $rates, $chosen_method ){
     241
     242    $pattern = '/^(postis:)?([A-Z]{3})[0-9]*$/';
     243
     244    // Only act on our own rates (others look like local_pickup:1, flat_rate:3, free_shipping).
     245    if ( ! is_string( $chosen_method ) || ! preg_match( $pattern, $chosen_method, $chosen_match ) ) {
     246        return $default;
     247    }
     248
     249    if ( ! is_array( $rates ) ) {
     250        return $default;
     251    }
     252
     253    // WooCommerce resets to the first rate whenever the rate list changes, and our
     254    // rate IDs vary per postcode, so match on the service instead of the exact ID.
     255    if ( isset( $rates[ $chosen_method ] ) ) {
     256        return $chosen_method;
     257    }
     258
     259    // DPH and DPL are both home delivery.
     260    $same_service = array( 'DPL' => 'DPH' );
     261    $chosen_code  = isset( $same_service[ $chosen_match[2] ] ) ? $same_service[ $chosen_match[2] ] : $chosen_match[2];
     262
     263    foreach ( $rates as $rate_id => $rate ) {
     264        if ( ! is_object( $rate ) || $rate->get_method_id() !== 'postis' || ! preg_match( $pattern, $rate_id, $rate_match ) ) {
     265            continue;
     266        }
     267
     268        $rate_code = isset( $same_service[ $rate_match[2] ] ) ? $same_service[ $rate_match[2] ] : $rate_match[2];
     269
     270        if ( $rate_code === $chosen_code ) {
     271            return $rate_id;
     272        }
     273    }
     274
     275    return $default;
     276}
  • posturinn/trunk/inc_hpos/admin.class.php

    r3390272 r3807488  
    736736   
    737737    function bulk_admin_notices() {
    738         global $post_type, $pagenow;
    739 
    740         if ('edit.php' !== $pagenow || 'shop_order' !== $post_type || !isset($_REQUEST['bulk_action'])) {
     738        $screen = function_exists('get_current_screen') ? get_current_screen() : null;
     739
     740        // With HPOS the orders list is admin.php?page=wc-orders, not edit.php?post_type=shop_order.
     741        if (!$screen || wc_get_page_screen_id('shop-order') !== $screen->id || !isset($_REQUEST['bulk_action'])) {
    741742            return;
    742743        }
  • posturinn/trunk/inc_hpos/hooks.php

    r3570342 r3807488  
    7373            wp_enqueue_script('postis-remodal', POSTIS_URL."/js/remodal.js", array('jquery'), '1.1.3', true);
    7474           
    75             wp_enqueue_script('postis-gm-js', POSTIS_URL."/js/postis-googlemap.js", array('jquery'), '1.1.3', true);
    76         }
    77     }
    78 }
    79 
    80 
    81 /**
    82 * Render Extra Content After Checkout Form (Google Map)
    83 **/
    84 function postis_hooks_after_checkout_form(){
    85    
     75            wp_enqueue_script('postis-gm-js', POSTIS_URL."/js/postis-googlemap.js", array('jquery'), postis_get_plugin_version(), true);
     76        }
     77    }
     78}
     79
     80
     81/**
     82* Build the Google Map postbox list (also refreshed through checkout fragments)
     83**/
     84function postis_get_googlemap_locations_html(){
     85
     86    $empty_list = '<ul class="postis-googlemap-locations"></ul>';
     87
     88    if ( ! WC()->session ) {
     89        return $empty_list;
     90    }
     91
    8692    // Get the exact same postbox services that the dropdown uses
    8793    $postbox_services = WC()->session->get('postis_postbox_services', array());
    88    
    89     if (!empty($postbox_services)) {
    90        
    91         // We have the same data as the dropdown, now we need to get coordinates
    92         // We'll need to call the postboxes API to get lat/lng for each service
    93         $postboxes_with_coords = array();
    94        
    95         // Get postboxes API data for coordinates
     94
     95    if (empty($postbox_services)) {
     96        return $empty_list;
     97    }
     98
     99    // Only DPO/DNO services are postboxes
     100    $postbox_service_list = array();
     101    foreach ($postbox_services as $service) {
     102        $base_service_id = substr($service['deliveryServiceId'], 0, 3);
     103        if ($base_service_id === 'DPO' || $base_service_id === 'DNO') {
     104            $postbox_service_list[] = $service;
     105        }
     106    }
     107
     108    if (empty($postbox_service_list)) {
     109        return $empty_list;
     110    }
     111
     112    // Coordinates come from the postboxes API; cached because this runs on every checkout refresh
     113    $postboxes_api_data = get_transient('postis_postboxes_coords');
     114
     115    if (empty($postboxes_api_data) || !is_array($postboxes_api_data)) {
     116        $postboxes_api_data = array();
     117
    96118        $endpoint_url = postis_get_endpoints('postboxes');
    97119        $postis_apikey = postis_get_api_key();
    98120        $domain = postis_get_homeurl();
    99        
     121
    100122        $req_headers = array(
    101123            "headers" => array(
    102                 'x-api-key' => $postis_apikey, 
     124                'x-api-key' => $postis_apikey,
    103125                'domain_name' => $domain,
    104126                'user-agent' => 'WooCommerce/'.postis_get_woocommerce_version().' | Postis/'.postis_get_plugin_version(),
    … …  
    107129            )
    108130        );
    109        
     131
    110132        $response = wp_remote_get($endpoint_url, $req_headers);
    111         $postboxes_api_data = array();
    112        
    113         if (!is_wp_error($response) && $response['response']['code'] == 200) {
     133
     134        if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) == 200) {
    114135            $postboxes_response = json_decode(wp_remote_retrieve_body($response), true);
    115            
    116             if (isset($postboxes_response['postboxes'])) {
     136
     137            if (!empty($postboxes_response['postboxes'])) {
    117138                // Index by postbox ID for quick lookup
    118139                foreach ($postboxes_response['postboxes'] as $postbox) {
    119140                    $postboxes_api_data[$postbox['postboxId']] = $postbox;
    120141                }
     142
     143                set_transient('postis_postboxes_coords', $postboxes_api_data, HOUR_IN_SECONDS);
    121144            }
    122145        }
    123        
    124         // Now combine the session services with coordinates from API
    125         foreach ($postbox_services as $service) {
    126             $service_id = $service['deliveryServiceId'];
    127             $base_service_id = substr($service_id, 0, 3);
    128            
    129             if ($base_service_id === 'DPO' || $base_service_id === 'DNO') {
    130                 // Extract postbox ID (remove DPO/DNO prefix)
    131                 $postbox_id = substr($service_id, 3);
    132                
    133                 // Get coordinates from API data
    134                 $coords = isset($postboxes_api_data[$postbox_id]) ? $postboxes_api_data[$postbox_id] : array();
    135                
    136                 $postboxes_with_coords[] = array(
    137                     'deliveryServiceId' => $service_id, // Full ID like DPO9556
    138                     'postboxId' => $postbox_id,
    139                     'name' => $service['nameLong'],
    140                     'postcode' => isset($coords['postcode']) ? $coords['postcode'] : '',
    141                     'address' => isset($coords['address']) ? $coords['address'] : '',
    142                     'town' => isset($coords['town']) ? $coords['town'] : '',
    143                     'latitude' => isset($coords['latitude']) ? $coords['latitude'] : '',
    144                     'longitude' => isset($coords['longitude']) ? $coords['longitude'] : '',
    145                 );
    146             }
    147         }
    148        
    149         if (!empty($postboxes_with_coords)) {
     146    }
     147
     148    // Combine the session services with coordinates from API
     149    $postboxes_with_coords = array();
     150    foreach ($postbox_service_list as $service) {
     151        $service_id = $service['deliveryServiceId'];
     152        // Extract postbox ID (remove DPO/DNO prefix)
     153        $postbox_id = substr($service_id, 3);
     154
     155        $coords = isset($postboxes_api_data[$postbox_id]) ? $postboxes_api_data[$postbox_id] : array();
     156
     157        $postboxes_with_coords[] = array(
     158            'deliveryServiceId' => $service_id, // Full ID like DPO9556
     159            'postboxId' => $postbox_id,
     160            'name' => $service['nameLong'],
     161            'postcode' => isset($coords['postcode']) ? $coords['postcode'] : '',
     162            'address' => isset($coords['address']) ? $coords['address'] : '',
     163            'town' => isset($coords['town']) ? $coords['town'] : '',
     164            'latitude' => isset($coords['latitude']) ? $coords['latitude'] : '',
     165            'longitude' => isset($coords['longitude']) ? $coords['longitude'] : '',
     166        );
     167    }
     168
     169    ob_start();
     170    ?>
     171    <ul class="postis-googlemap-locations">
     172        <?php
     173        foreach ($postboxes_with_coords as $index => $meta) {
     174            $postboxId = $meta['postboxId'];
     175            $deliveryServiceId = $meta['deliveryServiceId'];
     176            $postcode = $meta['postcode'];
     177            $name = $meta['name'];
     178            $latitude = $meta['latitude'];
     179            $longitude = $meta['longitude'];
     180        ?>
     181              <li class="postis-googlemap-location"
     182                  data-postboxes="<?php echo esc_attr(json_encode($meta, JSON_FORCE_OBJECT)); ?>"
     183                  data-lat="<?php echo esc_attr($latitude); ?>"
     184                  data-long="<?php echo esc_attr($longitude); ?>"
     185                  data-postbox-id="<?php echo esc_attr($postboxId); ?>"
     186                  data-delivery-service-id="<?php echo esc_attr($deliveryServiceId); ?>">
     187                  <span><?php echo esc_html($name); ?></span>
     188                  <span>(<?php echo esc_html($postcode); ?>)</span>
     189              </li>
     190        <?php
     191        }
     192        ?>
     193    </ul>
     194    <?php
     195    return ob_get_clean();
     196}
     197
     198
     199/**
     200* Render Extra Content After Checkout Form (Google Map)
     201**/
     202function postis_hooks_after_checkout_form(){
     203
     204    // Always render the dialog: it sits outside the parts WooCommerce refreshes,
     205    // so the list inside is kept current through the checkout fragments instead.
    150206    ?>
    151207    <div data-remodal-id="postis_googlemap_modal" role="dialog">
    … …  
    156212            <div class="postis-col-wrapper">
    157213                <div class="postis-postboxes-section">
    158                     <ul>
    159                         <?php
    160                         foreach ($postboxes_with_coords as $index => $meta) {
    161                             $postboxId = $meta['postboxId'];
    162                             $deliveryServiceId = $meta['deliveryServiceId'];
    163                             $postcode = $meta['postcode'];
    164                             $name = $meta['name'];
    165                             $latitude = $meta['latitude'];
    166                             $longitude = $meta['longitude'];
    167                         ?>
    168                               <li class="postis-googlemap-location"
    169                                   data-postboxes="<?php echo esc_attr(json_encode($meta, JSON_FORCE_OBJECT)); ?>"
    170                                   data-lat="<?php echo esc_attr($latitude); ?>"
    171                                   data-long="<?php echo esc_attr($longitude); ?>"
    172                                   data-postbox-id="<?php echo esc_attr($postboxId); ?>"
    173                                   data-delivery-service-id="<?php echo esc_attr($deliveryServiceId); ?>">
    174                                   <span><?php echo esc_html($name); ?></span>
    175                                   <span>(<?php echo esc_html($postcode); ?>)</span>
    176                               </li>
    177                         <?php
    178                         }
    179                         ?>
    180                     </ul>
     214                    <?php echo postis_get_googlemap_locations_html(); ?>
    181215                    <button data-remodal-action="confirm" class="postis-postbox-js remodal-confirm"><?php echo __("Choose Postbox", "postis"); ?></button>
    182216                </div>
    … …  
    188222    </div>
    189223    <?php
    190         }
    191     }
    192 }
     224}
     225
     226
     227/**
     228* Refresh the Google Map postbox list on checkout updates
     229**/
     230function postis_hooks_googlemap_fragments( $fragments ){
     231
     232    $fragments['ul.postis-googlemap-locations'] = postis_get_googlemap_locations_html();
     233
     234    return $fragments;
     235}
     236
     237
     238/**
     239* Keep the customer's chosen Pósturinn service when the rates change
     240**/
     241function postis_hooks_keep_chosen_shipping_service( $default, $rates, $chosen_method ){
     242
     243    $pattern = '/^(postis:)?([A-Z]{3})[0-9]*$/';
     244
     245    // Only act on our own rates (others look like local_pickup:1, flat_rate:3, free_shipping).
     246    if ( ! is_string( $chosen_method ) || ! preg_match( $pattern, $chosen_method, $chosen_match ) ) {
     247        return $default;
     248    }
     249
     250    if ( ! is_array( $rates ) ) {
     251        return $default;
     252    }
     253
     254    // WooCommerce resets to the first rate whenever the rate list changes, and our
     255    // rate IDs vary per postcode, so match on the service instead of the exact ID.
     256    if ( isset( $rates[ $chosen_method ] ) ) {
     257        return $chosen_method;
     258    }
     259
     260    // DPH and DPL are both home delivery.
     261    $same_service = array( 'DPL' => 'DPH' );
     262    $chosen_code  = isset( $same_service[ $chosen_match[2] ] ) ? $same_service[ $chosen_match[2] ] : $chosen_match[2];
     263
     264    foreach ( $rates as $rate_id => $rate ) {
     265        if ( ! is_object( $rate ) || $rate->get_method_id() !== 'postis' || ! preg_match( $pattern, $rate_id, $rate_match ) ) {
     266            continue;
     267        }
     268
     269        $rate_code = isset( $same_service[ $rate_match[2] ] ) ? $same_service[ $rate_match[2] ] : $rate_match[2];
     270
     271        if ( $rate_code === $chosen_code ) {
     272            return $rate_id;
     273        }
     274    }
     275
     276    return $default;
     277}
  • posturinn/trunk/js/postis-googlemap.js

    r3302192 r3807488  
    22jQuery(function($) {
    33    /**
    4      * Load Google Map Modal
     4     * Get Google Map Modal (null when the dialog isn't in the DOM)
    55     */
    6     var inst = $('[data-remodal-id=postis_googlemap_modal]').remodal();
     6    function getPostboxModal() {
     7        var $el = $('[data-remodal-id=postis_googlemap_modal]');
     8        return $el.length ? $el.remodal() : null;
     9    }
    710
    811    /**
    … …  
    1013     */
    1114    $(document.body).on('updated_checkout', function() {
     15        // The postbox list was just replaced by the checkout fragment, so refresh the markers.
     16        if (typeof map !== 'undefined' && map) {
     17            addInitialPostboxMarkers();
     18            map.setCenter({ lat: 64.9995555, lng: -18.9839165 });
     19            map.setZoom(6);
     20        } else if (typeof google !== 'undefined' && google.maps && document.getElementById('postis-map')) {
     21            initMap();
     22        }
     23
    1224        const shipping_wrapper = $('.woocommerce-shipping-methods');
    1325        const get_shipping_method = shipping_wrapper.find('.shipping_method:checked').val();
    … …  
    2537        ) {
    2638            $('#postis_dpo_postbox_field').hide();
    27             inst.open();
     39            var inst = getPostboxModal();
     40            if (inst) {
     41                inst.open();
     42            }
    2843        }
    2944    });
    … …  
    7489    $(document).on('click', '.postis-googlemap-modal-btn', function(e) {
    7590        e.preventDefault();
    76         inst.open();
     91        var inst = getPostboxModal();
     92        if (inst) {
     93            inst.open();
     94        }
    7795    });
    7896});
    … …  
    86104
    87105function initMap() {
     106    if (typeof google === 'undefined' || !document.getElementById("postis-map")) {
     107        return;
     108    }
     109
    88110    const Iceland = { lat: 64.9995555, lng: -18.9839165 };
    89111    map = new google.maps.Map(document.getElementById("postis-map"), {
    … …  
    168190
    169191// Load map once window is ready
    170 google.maps.event.addDomListener(window, 'load', initMap);
     192if (typeof google !== 'undefined' && google.maps) {
     193    google.maps.event.addDomListener(window, 'load', initMap);
     194}
  • posturinn/trunk/readme.txt

    r3570342 r3807488  
    33Tags: shipping, posturinn, icelandic post shipping, shipping rates, woocommerce
    44Requires at least: 4.3
    5 Tested up to: 7.0
     5Tested up to: 7.1
    66Requires PHP: 7.4
    7 Stable tag: 1.5.1
     7Stable tag: 1.5.2
    88License: GPL2
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
    … …  
    1717
    1818This plugin is only for companies that have a registered user in Mappan.
     19
     20Tested up to PHP 8.2.
    1921
    2022== Installation ==
    … …  
    2628
    2729== Changelog ==
     30= 1.5.2 =
     31Fixed the "Choose Postbox" map popup not opening on the first checkout visit, and its postbox list now follows postcode changes
     32The chosen Pósturinn shipping method is kept when the postcode changes instead of jumping to another method
     33Fixed bulk "Pósturinn - Print Shipment PDF" and other bulk action notices not showing on the HPOS orders screen
     34
    2835= 1.5.1 =
    2936minor changes
  • posturinn/trunk/wc-posturinn-shipping-api.php

    r3570342 r3807488  
    33 * Plugin Name: Pósturinn's Shipping with WooCommerce
    44 * Description: This plugin is an WooCommerce extension to extend shipping method with Pósturinn's - Iceland
    5  * Version: 1.5.1
     5 * Version: 1.5.2
    66 * Author: Pósturinn
    77 * Author URI: https://postur.is
    … …  
    8787        if ($gm_enable == 'yes') {
    8888            add_action( 'woocommerce_after_checkout_form', 'postis_hooks_after_checkout_form', 20 );
    89         }
     89            add_filter( 'woocommerce_update_order_review_fragments', 'postis_hooks_googlemap_fragments' );
     90        }
     91
     92        add_filter( 'woocommerce_shipping_chosen_method', 'postis_hooks_keep_chosen_shipping_service', 10, 3 );
    9093       
    9194        $plugin_name = plugin_basename( __FILE__ );
Note: See TracChangeset for help on using the changeset viewer.