7

Essentially looking to add customer email to our customer's receipts (invoices) and sales order emails that are generated to dispatch/couriers as well etc.

With a bit of searching I've established the best way to do this is simply to add this variable through the magento admin system area. So below:

System > Configuration > Customers > Customer Configuration > Address Templates

This brings up all of the address templates fields for text, html, pdf etc.

So I've added what I believe to be the variable name need into each box here.

So for example html is now:

{{depend prefix}}{{var prefix}} {{/depend}}{{var firstname}} {{depend middlename}}{{var middlename}} {{/depend}}{{var lastname}}{{depend suffix}} {{var suffix}}{{/depend}}
{{depend company}}{{var company}}
{{/depend}} {{if street1}}{{var street1}}
{{/if}} {{depend street2}}{{var street2}}
{{/depend}} {{depend street3}}{{var street3}}
{{/depend}} {{depend street4}}{{var street4}}
{{/depend}} {{if city}}{{var city}}, {{/if}}{{if region}}{{var region}}, {{/if}}{{if postcode}}{{var postcode}}{{/if}}
{{var country}}
{{depend telephone}}Mobile: {{var telephone}}{{/depend}} {{depend fax}}
Landline: {{var fax}}{{/depend}} {{depend vat_id}}
VAT: {{var vat_id}}{{/depend}} {{depend email}}Email: {{var email}}{{/depend}}

So the key line for me being the below:

{{depend email}}Email: {{var email}}{{/depend}}

Other than this 1 change, is there anything else I'm failing to do to make this variable be added to the shipping address templates?

2
  • Anyone able to offer any help on this at all? Still having no joy finding the answer to this question.... seems to be straight forward enough, but isn't unfortunately..... hoping it's a case of establishing correct variable and that no table join or anything like that required.... ?
    – MagentoMac
    Jul 28, 2014 at 20:50
  • Still stumbling with this.... this page has some clues, but not sure how accurate the variables are: magentocommerce.com/wiki/modules_reference/english/… I've tried just "email", as well as "customer_email" and "data.email" nothing pulling anything through as yet.......
    – MagentoMac
    Aug 26, 2014 at 16:30

7 Answers 7

9

You can get the customer email like this in the invoice or order templates:

{{var order.getCustomerEmail()}}

SO you can try to add

Email: {{var order.getCustomerEmail()}}

after

{{var order.shipping_address.format('html')}}

[Edit]
For pdf you should replace this from Mage_Sales_Model_Order_Pdf_Abstract::insertOrder

$shippingAddress = $this->_formatAddress($order->getShippingAddress()->format('pdf'));

with

$shippingAddress = $this->_formatAddress($order->getShippingAddress()->format('pdf').'|'.Mage::helper('sales')->__('Email:').$order->getCustomerEmail());  

Of course you shouldn't edit the core code.
You have to copy the file to the local folder. You cannot do a simple override because it's an abstract class.

2
  • Did you test that? Using that in the PDF address template does not show the customer e-mail on the PDF. It just shows "Email: ".
    – Simon
    Sep 2, 2014 at 5:59
  • @Simon.See my update.
    – Marius
    Sep 2, 2014 at 6:09
4

There is an easier way to add it to all customer configuration emails. Just rewrite Mage_Customer_Helper_Address and it to the customer address attribute list. So open up

System > Configuration > Customers > Customer Configuration > Address Templates

and add

{{depends email}}{{var email}}{{/depends}}

to the layouts wherever you need it.

Create your app/code/community/Elgentos/AddressWithEmail/etc/config.xml file;

<?xml version="1.0"?>
<config>
    <modules>
        <Elgentos_AddressWithEmail>
            <version>0.1.0</version>
        </Elgentos_AddressWithEmail>
    </modules>
    <global>
        <helpers>
            <customer>
                <rewrite>
                    <address>Elgentos_AddressWithEmail_Helper_Customer_Address</address>
                </rewrite>
            </customer>
        </helpers>
    </global>
</config>

Then create the helper at app/code/community/Elgentos/AddressWithEmail/Helper/Customer/Address.php;

<?php

class Elgentos_AddressWithEmail_Helper_Customer_Address extends Mage_Customer_Helper_Address
{

    public function getAttributes()
    {
        $this->_attributes = parent::getAttributes();

        if(!isset($this->_attributes['email'])) {
            $config = Mage::getSingleton('eav/config');
            $this->_attributes['email'] = $config->getAttribute('customer', 'email');
        }

        return $this->_attributes;
    }

}

and lastly create your app/etc/modules/Elgentos_AddressWithEmail.xml module declaration file;

<?xml version="1.0"?>
<config>
    <modules>
        <Elgentos_AddressWithEmail>
            <active>true</active>
            <codePool>community</codePool>
            <depends>
                <Mage_Customer/>

            </depends>
        </Elgentos_AddressWithEmail>
    </modules>
</config>

Clear your cache and you're done!

You can find the full composerised extension on Github; https://github.com/elgentos/AddressWithEmail

1
  • This module worked perfectly! Jul 5, 2018 at 19:49
2

The email is unfortunately not a customer address field, and can be found in all entities that are related with addresses - quotes, orders, customers - except addresses themselves :-) What you are trying to do is not straight-forward, but can be more or less achieved. You will need a custom module for that.

Everything happens in Mage_Customer_Block_Address_Renderer_Default, in the render() method. As you can see, rendered variables are loaded from EAV configuration, and since the email is not a customer address attribute, it won't be available while rendering. Still, you can find a way to override that render() method and retrieve the email address from there.

You can of course rewrite the class entirely using Magento rewrites in your config.xml, like this:

<config>
    <blocks>
        <customer>
            <rewrite>
                <address_renderer_default>Your_Module_Block_Address_Renderer</address_renderer_default>
            </rewrite>
        </customer>
    </blocks>
</config>

This is not necessary though, and it's not really best practice.

Magento lets you define a renderer for each address format (text, html, oneline, pdf, etc). Format renderers are defined in the config.xml like this:

<config>
    <global>
        <blocks>
            <your_module>
                <class>Your_Module_Block</class>
            </your_module>
        </blocks>
        <customer>
            <address>
                <formats>
                    <html>
                        <renderer>your_module/address_renderer</renderer>
                    </html>
                </formats>
            </address>
        </customer>
    </global>
</config>

Now that we have defined a custom address renderer for the HTML format, we can implement its logic like this:

// File path: app/code/[codePool]/Your/Module/Block/Address/Renderer.php
class Your_Module_Block_Address_Renderer extends Mage_Customer_Block_Address_Renderer_Default
{
    public function render(Mage_Customer_Model_Address_Abstract $address, $format = null)
    {
        $email = $address->getEmail(); // Works for quote and order addresses
        // Rest of your logic goes here...
    }
}

I know this looks like a lot, but it works for sure. I couldn't find any easier way by looking at Magento Core, hope this helps.

1

for getting customer gmail at orde template use

below code

 {{htmlescape var=$order.getCustomerEmail()}}  on you html  emailtemplate
1

f you want to add customer email to PDF invoices, here are the steps:

Copy a local copy of file Abstract.php by copying from

app/code/core/Mage/Sales/Model/Order/Pdf/Abstract.php

to

app/code/local/Mage/Sales/Model/Order/Pdf/Abstract.php

If you want to show customer email on Billing Address (Sold to), on the local copy of the above file. Make the following changes around line 313:

<?php
//replace
$billingAddress =
$this->_formatAddress($order->getBillingAddress()->format('pdf'));

//with
$billingAddress =
$this->_formatAddress($order->getBillingAddress()->format('pdf').Mage::helper("sales")->__('Email:
'. $order ->getCustomerEmail()));
?>

If you want to show the customer's email on the Shipping Address (Ship to), do the following changes on the same file around line 331

<?php
//replace
$shippingAddress =
$this->_formatAddress($order->getShippingAddress()->format('pdf'));

//with
$shippingAddress =
$this->_formatAddress($order->getShippingAddress()->format('pdf').Mage::helper("sales")->__('Email:
'. $order ->getCustomerEmail()));
?>

Clear the cache. It should work to add customer email to PDF invoices.

1

Just override the DefaultRenderer.php project\magento\app\code\Modulename\Rewrite\etc\di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Block\Address\Renderer\DefaultRenderer"
        type="Modulename\Rewrite\Magento\Customer\Block\Address\Renderer\DefaultRenderer"/>
</config>

project\magento\app\code\Modulename\Rewrite\etc\module.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Modulename_Rewrite" setup_version="2.0.1">
</module>

D:\ribstore\magento\app\code\Modulename\Rewrite\Magento\Customer\Block\Address\Renderer\DefaultRenderer.php

<?php

namespace Sellerdeck\Rewrite\Magento\Customer\Block\Address\Renderer;

use Magento\Customer\Model\Address\Mapper; use Magento\Customer\Model\Metadata\ElementFactory;

class DefaultRenderer extends \Magento\Customer\Block\Address\Renderer\DefaultRenderer {

protected $_customerMetadataService;

public function __construct(
    \Magento\Framework\View\Element\Context $context,
ElementFactory $elementFactory,
    \Magento\Directory\Model\CountryFactory $countryFactory,
    \Magento\Customer\Api\AddressMetadataInterface $metadataService,
Mapper $addressMapper,
    \Magento\Customer\Api\CustomerMetadataInterface $customerMetadataService,
    array $data = []
)
{
    parent::__construct($context, $elementFactory, $countryFactory, $metadataService, $addressMapper, $data);
    $this->_customerMetadataService = $customerMetadataService;
}

/**
 * {@inheritdoc}
 *
 * @SuppressWarnings(PHPMD.CyclomaticComplexity)
 * @SuppressWarnings(PHPMD.NPathComplexity)
 */
public function renderArray($addressAttributes, $format = null)
{
    switch ($this->getType()->getCode()) {
        case 'html':
            $dataFormat = ElementFactory::OUTPUT_FORMAT_HTML;
            break;
        case 'pdf':
            $dataFormat = ElementFactory::OUTPUT_FORMAT_PDF;
            break;
        case 'oneline':
            $dataFormat = ElementFactory::OUTPUT_FORMAT_ONELINE;
            break;
        default:
            $dataFormat = ElementFactory::OUTPUT_FORMAT_TEXT;
            break;
    }

    $attributesMetadata = $this->_addressMetadataService->getAllAttributesMetadata();
    $attributesMetadata[] = $this->_customerMetadataService->getAttributeMetadata('email');
    $data = [];
    foreach ($attributesMetadata as $attributeMetadata) {
        if (!$attributeMetadata->isVisible()) {
            continue;
        }
        $attributeCode = $attributeMetadata->getAttributeCode();
        if ($attributeCode == 'country_id' && isset($addressAttributes['country_id'])) {
            $data['country'] = $this->_countryFactory->create()->loadByCode(
                $addressAttributes['country_id']
            )->getName();
        } elseif ($attributeCode == 'region' && isset($addressAttributes['region'])) {
            $data['region'] = __($addressAttributes['region']);
        } elseif (isset($addressAttributes[$attributeCode])) {
            $value = $addressAttributes[$attributeCode];
            $dataModel = $this->_elementFactory->create($attributeMetadata, $value, 'customer_address');
            $value = $dataModel->outputValue($dataFormat);
            if ($attributeMetadata->getFrontendInput() == 'multiline') {
                $values = $dataModel->outputValue(ElementFactory::OUTPUT_FORMAT_ARRAY);
                // explode lines
                foreach ($values as $k => $v) {
                    $key = sprintf('%s%d', $attributeCode, $k + 1);
                    $data[$key] = $v;
                }
            }
            $data[$attributeCode] = $value;
        }
    }
    if ($this->getType()->getEscapeHtml()) {
        foreach ($data as $key => $value) {
            $data[$key] = $this->escapeHtml($value);
        }
    }
    $format = $format !== null ? $format : $this->getFormatArray($addressAttributes);
    return $this->filterManager->template($format, ['variables' => $data]);
}

}

finally upgrade, compilation, deploy, then reindex and cache flesh In Admin just add at System > Configuration > Customers > Customer Configuration > Address Templates

{{depend email}}Email: {{var email}}{{/depend}}
1
  • I tested this solution, works for me well..Thanks!
    – nuwaus
    Sep 10, 2019 at 12:32
0

I guess you have $order available. You can use that to access customer information.

See Get customer account data in shipment PDF and Get customer account details for invoice PDF for alternative solutions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.