How To Add labels In Customer Address Fields Prestashop

In this tutorial I show you step by step how to add labels in customer address fields, to achive this functionality you need to override base function which generate the address format and create the new format you need. I will show you a basic example where we will add a new label for every field in the address. The new format should appear on order confirmation email, order invoice and in customer my account page where he can see his addresses.

Create labels for customer address

Important: This example is tested on Prestashop 1.7 but maybe it will work on other previews version, you can make a try.

First of all: go in override -> classes and create a new file AddressFormat.php

In this function we will override the base function called generateAddress();

So our file should look like the following:

class AddressFormat extends AddressFormatCore{
  public static function generateAddress(Address $address, $patternRules = array(), $newLine = "\r\n", $separator = ' ', $style = array())
    {
        $addressFields = AddressFormat::getOrderedAddressFields($address->id_country);
        $addressFormatedValues = AddressFormat::getFormattedAddressFieldsValues($address, $addressFields);
        $addressText = '';
        foreach ($addressFields as $line) {
            if (($patternsList = preg_split(self::_CLEANING_REGEX_, $line, -1, PREG_SPLIT_NO_EMPTY))) {
                $tmpText = '';
                foreach ($patternsList as $pattern) {
                    if ((!array_key_exists('avoid', $patternRules)) ||
                                (is_array($patternRules) && array_key_exists('avoid', $patternRules) && !in_array($pattern, $patternRules['avoid']))) {
                        $tmpText .= (isset($addressFormatedValues[$pattern]) && !empty($addressFormatedValues[$pattern])) ?
                                (((isset($style[$pattern])) ?
                                    (sprintf($style[$pattern], $addressFormatedValues[$pattern])) :
                                    $addressFormatedValues[$pattern]).$separator) : '';
                    }
                }
                $tmpText = trim($tmpText);
        
                $addressText .= (!empty($tmpText)) ? '<b>'.ucfirst(array_search ($tmpText, $addressFormatedValues)).'</b>: '.$tmpText.$newLine: '';
            }
        }
        $addressText = preg_replace('/'.preg_quote($newLine, '/').'$/i', '', $addressText);
        $addressText = rtrim($addressText, $separator);
        return $addressText;
    }
}

As I said is a basic example because here, we just add the key which represent the name of the input. $addressFormatedValues is an array which contain the main address but in array format, so we just search in this array by value that's mean "John" and get the key which is Firstname for instance. And we do this for all fields. This method is tested and it works on prestashop >= 1.7. I am sure there are other methods more advanced but this method take max 5 minutes to implement and it is working great.

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
bart
bart
5 years ago

Thank You so much! 🙂

1
0
Would love your thoughts, please comment.x
()
x