• Resolved thetankgirl

    (@thetankgirl)


    We have quite an important bug here on our hands:
    – when someone enters a housenumber addition (in numbers, not letters), the housenumber is added to the street address.

    For example: “street: Examplestreet” “housenumber:1” “addition: 5”, becomes “street: Examplestreet 1” “housenumber 5”.

    I have tried adding a – before the addition (examplestreet 1-5), but the – is removed during label creation. I have tried putting the housenumber in the second address field, still no difference.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author DHL eCommerce

    (@dhlparcel)

    Hi thetankgirl,

    Thank you for your feedback. That is indeed confusing. Due to addresses in the Netherlands can sometimes include numbers in their street name, the parsing isn’t flawless at the moment. We’ve working on a more permanent solution internally.

    For now, we recommend you to add a dot (.) or a dash (-) and the extra number will be correctly used as an addition.

    We’ve added your feature request to retain any prefixes for additions. Currently it works correctly for the shipping process as the data is correctly passed as ‘addition’, but it looks confusing on the physical label when the dash isn’t visible, even though it was given (shows up as “1 5”). This change should show “1-5” on the label again.

    Let us know if you need additional support and thanks for using our plugin.

    Greetings,
    Plugin Development Team
    DHL eCommerce Netherlands

    Thread Starter thetankgirl

    (@thetankgirl)

    I have tried to edit the sourcecode to try and find a temporary solution, maybe you can check it out and correct me if I’m wrong:

    in class-dhlpwc-model-logic-shipment.php I changed the prepare_street_address function (see the end of the function):

       protected function prepare_street_address($address)
    {
    $skip_addition_check = false;
    $skip_reverse_check = false;
    $cutout = ''; 
    if (!isset($address['street'])) {
            $address['street'] = trim(join(' ', array(
                isset($address['address_1']) ? trim($address['address_1']) : '',
                isset($address['address_2']) ? trim($address['address_2']) : ''
            )));
    
            // Cutout starting special numbers from regular parsing logic
            $parsable_parts = explode(' ', trim($address['street']), 2);
    
            // Check if it has a number with letters
            if (preg_match('/[0-9]+[a-z]+/i', reset($parsable_parts)) === 1) {
                $cutout = reset($parsable_parts) . ' ';
                $skip_reverse_check = true;
                unset($parsable_parts[0]);
    
            // Check if it has a number with more than just letters, but also other available numbers
            } else if (preg_match('/[0-9]+[^0-9]+/', reset($parsable_parts)) === 1 && preg_match('/\d/', end($parsable_parts)) === 1) {
                $cutout = reset($parsable_parts) . ' ';
                $skip_reverse_check = true;
                unset($parsable_parts[0]);
    
            // Check if it has something before a number
            } else if (preg_match('/[^0-9]+[0-9]+/', reset($parsable_parts)) === 1) {
                $cutout = reset($parsable_parts) . ' ';
                $skip_reverse_check = true;
                unset($parsable_parts[0]);
    
            // Check if starts with number (with anything), but also has numbers in the rest of the address
            } else if (preg_match('/[^0-9]*[0-9]+[^0-9]*/', reset($parsable_parts)) === 1 && preg_match('/\d/', end($parsable_parts)) === 1) {
                $cutout = reset($parsable_parts) . ' ';
                $skip_reverse_check = true;
                unset($parsable_parts[0]);
            }
    
            $parsable_street = implode(' ', $parsable_parts);
    
            preg_match('/([^0-9]*)\s*(.*)/', trim($parsable_street), $street_parts);
            $address = array_merge($address, [
                'street' => isset($street_parts[1]) ? trim($street_parts[1]) : '',
                'number' => isset($street_parts[2]) ? trim($street_parts[2]) : '',
                'addition' => '',
            ]);
    
            // Check if $street is empty
            if (strlen($address['street']) === 0 && !$skip_reverse_check) {
                // Try a reverse parse
                preg_match('/([\d]+[\w.-]*)\s*(.*)/i', trim($parsable_street), $street_parts);
                $address['street'] = isset($street_parts[2]) ? trim($street_parts[2]) : '';
                $address['number'] = isset($street_parts[1]) ? trim($street_parts[1]) : '';
                $skip_addition_check = true;
            }
    
            // Check if $number has no numbers
            if (preg_match('/\d/', $address['number']) === 0) {
                $address['street'] = trim($parsable_street);
                $address['number'] = '';
    
            // Addition check
            } else if (!$skip_addition_check) {
                // If there are no letters, but has additional spaced numbers, use last number as number, no addition
                preg_match('/([^a-z]+)\s+([\d]+)$/i', $address['number'], $number_parts);
                if (isset($number_parts[2])) {
                    $address['street'] .= ' ' . $number_parts[1];
                    $address['number'] = $number_parts[2];
    
                // Regular number / addition split
                } else {
                    preg_match('/([\d]+)[ .-]*(.*)/i', $address['number'], $number_parts);
                    $address['number'] = isset($number_parts[1]) ? trim($number_parts[1]) : '';
                    $address['addition'] = isset($number_parts[2]) ? trim($number_parts[2]) : '';
                }
            }
    
            // Reassemble street
            if (isset($address['street'])) {
                $address['street'] = $cutout . $address['street'];
            }
        }
    
        // Be sure these fields are filled
        $address['number'] = isset($address['number']) ? $address['number'] : '';
        $address['addition'] = isset($address['addition']) ? $address['addition'] : '';
    
        // Clean any starting punctuations
        preg_match('/^[[:punct:]\s]+(.*)/', $address['street'], $clean_street);
        if (isset($clean_street[1])) {
            $address['street'] = $clean_street[1];
        }
        preg_match('/^[[:punct:]\s]+(.*)/', $address['number'], $clean_number);
        if (isset($clean_number[1])) {
            $address['number'] = $clean_number[1];
        }
        preg_match('/^[[:punct:]\s]+(.*)/', $address['addition'], $clean_addition);
        if (isset($clean_addition[1])) {
            $address['addition'] = $clean_addition[1];
        }
    if (!empty($address['addition'])) {
    $address['number'] = $address['number'] . '-' . $address['addition'];
    $address['addition'] = ''; 
    // Clear the addition to prevent duplicate concatenation
    }
        return $address;
    }

    Edit: this did not work correctly as it put the housenumber and addition in the same field.

    Plugin Author DHL eCommerce

    (@dhlparcel)

    Hi thetankgirl,

    Note that the code will disappear on the next update. But for what you wish to do, we recommend to add the following at the end of the function:

    if (!empty($address['addition'])) {
    $address['addition'] = '-' . $address['addition'];
    }

    We’re currently preparing the code which retains the original prefixes written on the address, which is not the same as this suggestion.

    Greetings,
    Plugin Development Team
    DHL eCommerce Netherlands

    Thread Starter thetankgirl

    (@thetankgirl)

    This version gave me a wrong housenumber addition in my dhlparcel
    It did gave me an idea and made me do this:

    preg_match('/^[[:punct:]\s]+(.*)/', $address['addition'], $clean_addition);
    if (isset($clean_addition[1])) {
    $address['addition'] = '-' .$clean_addition[1];
    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Housenumber additions (in numbers not letters) wrongly picked up’ is closed to new replies.