Add the state to your Shop’s Address in PrestaShop Invoices

PrestaShop’s invoices always lacked the state in the shop’s address information. Let’s see how to take it back!

Watch the screencast

Text Version

Assigning the state id to the shop’saddress

Open up OrderInvoice.php, located in classes/order. Locate the method named getCurrentFormattedShopAddress, it’s the one responsible for creating the shop’s address in the invoice. You will notice that there is no “id_state” assigned to the address object, so what we need to do is append the following line:

$address->id_state = Configuration::get('PS_SHOP_STATE_ID', null, null, $id_shop);

Right after the $address->id_country one. The end result will look like this:

    public static function getCurrentFormattedShopAddress($id_shop = null)
    {
        $address = new Address();
        $address->company = Configuration::get('PS_SHOP_NAME', null, null, $id_shop);
        $address->address1 = Configuration::get('PS_SHOP_ADDR1', null, null, $id_shop);
        $address->address2 = Configuration::get('PS_SHOP_ADDR2', null, null, $id_shop);
        $address->postcode = Configuration::get('PS_SHOP_CODE', null, null, $id_shop);
        $address->city = Configuration::get('PS_SHOP_CITY', null, null, $id_shop);
        $address->phone = Configuration::get('PS_SHOP_PHONE', null, null, $id_shop);
        $address->id_country = Configuration::get('PS_SHOP_COUNTRY_ID', null, null, $id_shop);
        $address->id_state = Configuration::get('PS_SHOP_STATE_ID', null, null, $id_shop);


        return AddressFormat::generateAddress($address, array(), '<br />', ' ');
    }

This is everything it needs, but all of the old addresses will stay broken unless we do something for it!

Fixing old addresses without state

Open the file named HTMLTemplateInvoice.php, located in classes/pdf. The __construct method is what we are interested in, so right before the “// header informations” comment, add the following

OrderInvoice::fixAllShopAddresses();

This would in theory fix all of the broken addresses. However, since the method only fills those entries without an address associated, we need to edit the function itself. As you can see, it’s part of the OrderInvoice class, so let’s hop back to it, and find it at the very end:

    public static function fixAllShopAddresses()
    {
        $shopIds = Shop::getShops(false, null, true);
        $db = Db::getInstance();
        foreach ($shopIds as $idShop) {
            $address = static::getCurrentFormattedShopAddress($idShop);
            $escapedAddress = $db->escape($address, true, true);

            $db->execute('UPDATE `'._DB_PREFIX_.'order_invoice` INNER JOIN `'._DB_PREFIX_.'orders` USING (`id_order`)
                SET `shop_address` = \''.$escapedAddress.'\' WHERE `shop_address` IS NULL AND `id_shop` = '.$idShop);
        }
    }

See that part with the executing query, at the end of the function? We have to modify the query as follows:


            $db->execute('UPDATE `'._DB_PREFIX_.'order_invoice` INNER JOIN `'._DB_PREFIX_.'orders` USING (`id_order`)
                SET `shop_address` = \''.$escapedAddress.'\' WHERE `id_shop` = '.$idShop);

Basically, we got rid of all the conditions apart from the shop one, so that all entries are updated. At this point you can try to load any old invoice to see the result. Bear in mind you have to do it at least twice, as that code we just added runs after the first generation process..

Once it’s done you can revert back these last two changes, to avoid regenerating addresses every time.

You like the tuts and want to say "thank you"? Well, you can always feel free to donate:

You like the tuts and want to say "thank you"? Well, you can always feel free to donate: