Results 1 to 10 of 109

Threaded View

  1. #29
    Join Date
    Jan 2004
    Posts
    66,419
    Blog Entries
    7
    Plugin Contributions
    277

    Default Re: ZipShip - Support Thread

    Here's a radical experiment.

    1. For pattern matching, I wonder if we can skip using the *'s idea and just assume a "prefix". That is, for M***** just list M, and for L4**** just use L4.

    2. In any listed postal code, if you add a "!" at the beginning or end of the code, it will exclude that one. eg: M4B1Y! or !M4B1Y would exclude all M4B1Y* codes.

    3. I also added stripping of hyphens ... so in theory this can also work for US zip codes where someone wants to add specificity in zip-plus-four format. (The original code trimmed all zips to the "minimum allowed length" in order to do matching, which kills the ability to do pattern-matching and negation.)

    One caveat to note: if a postalcode pattern is present in more than one zone, the "last" non-excluded match will be used for calculating rates. (eg: if it matches in zones 1 and 3, then the rates from zone 3 will apply, ignoring its existence in zone 1).



    Here's the updated code which does all this.

    Code:
                    $this->dest_zone = false;
                    $this->default_zone = false;
                    // $this->dest_zipcode = substr(strtoupper($order->delivery['postcode']), 0, (int)ENTRY_POSTCODE_MIN_LENGTH);
                    $this->dest_zipcode = strtoupper(str_replace([' ', '-'], '', $order->delivery['postcode']));
                    for ($i = 1; $i <= $this->num_zones; $i++) {
                        $current_table = "MODULE_SHIPPING_ZIPSHIP_CODES_$i";
                        if (defined($current_table)) {
                            $zipcode_table = constant($current_table);
                            if ($zipcode_table === '00000') {
                                $this->default_zone = $i;
                            } else {
                                // Read codes from config
                                $zipcodes = explode(',', str_replace([' ', '-'], '', strtoupper($zipcode_table)));
    
                                // sort codes, first by alpha, then by reverse-length so we process longest-first
                                usort($zipcodes, function($a, $b){
                                    $a = trim($a, '!');
                                    $b = trim($b, '!');
                                   return strcmp($a, $b);
                                });
                                usort($zipcodes, function($a, $b){
                                    $a = trim($a, '!');
                                    $b = trim($b, '!');
                                   return (strlen($a) < strlen($b)) ? 1 : -1;
                                });
    
                                // Check for exclusion patterns defined with a starting or ending ! symbol
                                foreach($zipcodes as $check_against) {
                                    if ($check_against === '!' || strpos($check_against, '!') === false) continue; // if no ! skip checking this iteration
                                    if (strpos($this->dest_zipcode, trim($check_against, '!')) === 0) { // strip ! to do matching
                                        $this->dest_zone = false;
    
                                        // If exclusion detected, use "continue" to end this foreach and skip the next one as well
                                        continue 2;
                                    }
                                }
    
                                // Pass if the supplied code matches a defined prefix pattern
                                foreach($zipcodes as $check_against) {
                                    if (strpos($check_against, '!') !== false) continue;
                                    if (strpos($this->dest_zipcode, $check_against) === 0) {
                                        $this->dest_zone = $i;
    
                                        // If a match is found, end this foreach
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    if ($this->dest_zone === false && $this->default_zone === false) {
                        $this->enabled = false;
                    }
                }
            }
        }
    
        public function quote($method = '')
    Last edited by DrByte; 15 Dec 2020 at 10:21 PM. Reason: updated usort to be called twice; better PHP 8.0 compatibility
    .

    Zen Cart - putting the dream of business ownership within reach of anyone!
    Donate to: DrByte directly or to the Zen Cart team as a whole

    Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
    Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

 

 

Similar Threads

  1. Hebrew Support - latest release [Support Thread]
    By eranariel in forum Addon Language Packs
    Replies: 20
    Last Post: 23 Apr 2025, 08:49 AM
  2. SysCheck [support thread]
    By swguy in forum All Other Contributions/Addons
    Replies: 36
    Last Post: 24 Oct 2020, 05:28 AM
  3. v151 Ship2Pay Support thread
    By Design75 in forum All Other Contributions/Addons
    Replies: 9
    Last Post: 5 Nov 2019, 01:14 PM
  4. goMobile Support Thread
    By steveyork136 in forum Addon Templates
    Replies: 29
    Last Post: 26 Aug 2015, 11:56 AM
  5. ZJ Silver Support Thread
    By anthonyd in forum Addon Templates
    Replies: 220
    Last Post: 5 Nov 2010, 03:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg
Zen-Cart, Internet Selling Services, Klamath Falls, OR