Pull custom customer attribute and send in email? Magento 2.2
-
12-01-2021 - |
Question
I am working in Magento 2.2 and I am using an extension that allows the site admin to confirm new accounts before they are active. The extension can be found here: https://github.com/enrico69/magento2-customer-activation
When a new user registers the admin is sent an email that shows the email address of the new user.
This code sends the email:
public function send($customer)
{
$siteOwnerEmail = $this->scopeConfigInterface->getValue(
'trans_email/ident_sales/email',
ScopeInterface::SCOPE_STORE,
$customer->getStoreId()
);
$this->transportBuilder->setTemplateIdentifier('enrico69_activation_email_notification')
->setTemplateOptions(
[
'area' => Area::AREA_FRONTEND,
'store' => $customer->getStoreId(),
]
)
->setTemplateVars(['email' => $customer->getEmail()]);
$this->transportBuilder->addTo($siteOwnerEmail);
$this->transportBuilder->setFrom(
[
'name'=> $this->storeManagerInterface->getStore($customer->getStoreId())->getName(),
'email' => $siteOwnerEmail
]
);
$this->transportBuilder->getTransport()->sendMessage();
}
That file that emails the code to the admin is at: https://github.com/enrico69/magento2-customer-activation/blob/master/Model/AdminNotification.php
And I believe line 70 below controls pulling the customers email:
->setTemplateVars(['email' => $customer->getEmail()]);
I also found the line below in https://github.com/enrico69/magento2-customer-activation/blob/master/Observer/UserActivation.php that prepares the code.
$newCustomer = $this->customerRepository->get($customer->getEmail());
I am trying to pull custom Customer attributes values added to my eav_attribute table. The attribute is "company_name" and is already created. I found the following code which I modified here: http://www.w3solver.com/magento2-addcreate-and-get-custom-customer-attribute-value/
So I created the line below in https://github.com/enrico69/magento2-customer-activation/blob/master/Observer/UserActivation.php
$newCustomer = $this->customerRepository->get($customer->getCustomAttribute('company_name')->getValue());
And added the line below in https://github.com/enrico69/magento2-customer-activation/blob/master/Model/AdminNotification.php
->setTemplateVars(['companyname' => $customer->getCustomAttribute('company_name')->getValue()]);
But this still isn't working. Can anyone offer some clues to get me on the correct path? Thanks!
EDIT: Figured this out and the answer is below.
Solution
I got this working using the following code.
In the AdminNotification.php file at: https://github.com/enrico69/magento2-customer-activation/blob/master/Model/AdminNotification.php
I replaced the following lines:
$this->transportBuilder->setTemplateIdentifier('enrico69_activation_email_notification')
->setTemplateOptions(
[
'area' => Area::AREA_FRONTEND,
'store' => $customer->getStoreId(),
]
)
->setTemplateVars(['email' => $customer->getEmail()]);
With:
->setTemplateVars(
[
'email' => $customer->getEmail(),
'company_name' => $customer->getCustomAttribute('company_name')->getValue()
]
);
And in the frontend/email/new-customer-notification.html file at: https://github.com/enrico69/magento2-customer-activation/blob/master/view/frontend/email/new-customer-notification.html
I replaced:
A new customer registered : {{var email|raw}} ! The account is waiting for approval.
With:
A new customer registered : {{var email|raw}} Company name: {{var company_name|raw}} The account is waiting for approval.
I couldn't find how at add multiple variables to ->setTemplateVars anywhere so I hope this helps someone! Thanks!