Most of our customers create accounts in the frontend. Occasionally, we will create an account for a customer in the admin. I don't know how best to send only these customers their password.
Because Magento doesn't generally send out account passwords in emails, we have removed:
Password: {{htmlescape var=$customer.password}}
from the email template. When we create a customer in the admin and auto-generate the password, using the above function actually sends the password to the customer in plain-text. This might not be ideal, but at least it works. Better might be to send them a password reset link instead.
Using this code in the email template, however, means customers who sign up on the frontend just gets "Password: " with an empty space, which looks broken.
I have been trying to find a conditional we can use in the email template, essentially saying "if the customer account was created in the admin, then output X, otherwise output Y". We can then send a reset password link to all customers who have an account created by us in the admin, but send something else to those customers who signed up themselves on the frontend.
Is it possible to check if an account was created in the admin and use this conditional for the account_new email template?
Solved! Go to Solution.
You need to create a small module that will add an attribute flag for a user created in the admin panel.
First, create a folder for the module.
> app/code/community/MageWorx/Detect
Then, create a configuration file `config.xml`:
> app/code/community/MageWorx/Detect/etc/config.xml
with the following content:
<?xml version="1.0"?> <!-- /** * Copyright © 2016 MageWorx. All rights reserved. * See LICENSE.txt for license details. */ --> <config> <modules> <MageWorx_Detect> <version>1.0.0</version> </MageWorx_Detect> </modules> <global> <models> <mageworx_detect> <class>MageWorx_Detect_Model</class> </mageworx_detect> </models> <blocks> <mageworx_detect> <class>MageWorx_Detect_Block</class> </mageworx_detect> </blocks> <helpers> <mageworx_detect> <class>MageWorx_Detect_Helper</class> </mageworx_detect> </helpers> <resources> <mageworx_detect_setup> <setup> <module>MageWorx_Detect</module> <class>Mage_Customer_Model_Entity_Setup</class> </setup> </mageworx_detect_setup> </resources> </global> <adminhtml> <events> <customer_save_before> <observers> <mageworx_detect> <class>mageworx_detect/observer</class> <method>setCustomerCreatedByAdmin</method> </mageworx_detect> </observers> </customer_save_before> </events> </adminhtml> </config>
Where:
1. MageWorx_Detect – a name of the module
2. mageworx_detect – an alias for the models, helper and blocks
We use the installer Mage_Customer_Model_Entity_Setup for correct work of the module. The mageworx_detect observer on the customer_save_before event in the adminhtml scope let us set a flag to 1. This allows us to check whether a user has been created by an administrator in the email template.
Then, create the default helper file where we save a name of an attribute in a constant:
> app/code/community/MageWorx/Detect/Helper/Data.php
<?php /** * Copyright © 2016 MageWorx. All rights reserved. * See LICENSE.txt for license details. */ class MageWorx_Detect_Helper_Data extends Mage_Core_Helper_Abstract { const CUSTOMER_DETECT_ATTRIBUTE_NAME = 'is_from_admin'; } ?>
Note. If you decide to use the name of the attribute that differs from `is_from_admin`, you will need to replace it in the helper and email template only. Its quite easier than looking for it in the code.
And the installation file (keep in mind the subfolder `mageworx_detect_setup`):
> app/code/community/MageWorx/Detect/sql/mageworx_detect_setup/mysql4-install-1.0.0.php
<?php /** * Copyright © 2016 MageWorx. All rights reserved. * See LICENSE.txt for license details. */ /** @var Mage_Customer_Model_Entity_Setup $installer */ $installer = $this; $installer->startSetup(); $setup = new Mage_Eav_Model_Entity_Setup('core_setup'); $entityTypeId = $setup->getEntityTypeId('customer'); $attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId); $attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId); $attributeName = MageWorx_Detect_Helper_Data::CUSTOMER_DETECT_ATTRIBUTE_NAME; $sortOrder = 200; $installer->removeAttribute('customer',$attributeName); $installer->addAttribute("customer", $attributeName, [ "type" => "varchar", "backend" => "", "label" => "Is customer created by admin", "input" => "hidden", "source" => "", "visible" => false, "required" => false, "default" => "0", "frontend" => "", "unique" => false, "note" => "Is customer created by admin", ]); $attribute = Mage::getSingleton("eav/config")->getAttribute("customer", $attributeName); $setup->addAttributeToGroup( $entityTypeId, $attributeSetId, $attributeGroupId, $attributeName, $sortOrder ); $used_in_forms = []; $used_in_forms[] = "adminhtml_customer"; $attribute->setData("used_in_forms", $used_in_forms) ->setData("is_used_for_customer_segment", true) ->setData("is_system", 0) ->setData("is_user_defined", 1) ->setData("is_visible", 0) ->setData("sort_order", 100); $attribute->save(); $installer->endSetup(); ?>
You should pay attention to the following in the installer:
Then, you need to create an observer file:
> app/code/community/MageWorx/Detect/Model/Observer.php
<?php /** * Copyright © 2016 MageWorx. All rights reserved. * See LICENSE.txt for license details. */ class MageWorx_Detect_Model_Observer { /** * @param $observer */ public function setCustomerCreatedByAdmin($observer) { /** @var Mage_Customer_Model_Customer $customer */ $customer = $observer->getCustomer(); if (!$customer) { return; } $customer->setData(MageWorx_Detect_Helper_Data::CUSTOMER_DETECT_ATTRIBUTE_NAME, '1'); return; } } ?>
It has only one simple method that modifies the value of the attribute to 1 before saving a customer in the admin panel. We don't need an additional check on the front-end because we are utilizing the adminhtml scope for the observer declaration in the configuration file (config.xml) and the default value is 0.
Note. If you want this to be performed only for the first save of a customer, you need to add the additional check whether a customer (`$customer` variable) has an `id`:
if ($customer->getId()) { return; }
To activate the module, add the following file:
> app/etc/modules/MageWorx_Detect.xml
<?xml version="1.0"?> <!-- /** * Copyright © 2016 MageWorx. All rights reserved. * See LICENSE.txt for license details. */ --> <config> <modules> <MageWorx_Detect> <active>true</active> <codePool>community</codePool> </MageWorx_Detect> </modules> </config>
Then, disable the compilation, refresh the cache and check the result.
The new attribute should be used in the following way in the template:
{{depend customer.is_from_admin}} <strong>Password:</strong> {{htmlescape var=$customer.password}} {{/depend}}
Where the depend directive responses for the check of the following attribute value. It should be 0 on the front-end in our case and the check shouldn't pass or 1 for the admin panel, the check should pass and the field `<strong>Password:</strong> {{htmlescape var=$customer.password}}` will be drawn in the email template.
Here are our results of the check:
We load two users where `$customer` is created in the admin panel and `$customer2` is created on the front-end. Let's check the value of our attribute:
The customers in the database:
Then, create the template using the rule above and add the `depend` directive for the `is_from_admin` attribute. Assign this template to all actions we need:
For the customer that is created in the admin panel:
Our template in the debug (we are using russian locale, the "Пароль" line is Password):
An email in the browser windows (preview doesn't work, we stopped the script and displayed the result instead of sending email):
For the customer that is created on the front-end:
Our template in the debug:
The email in the browser window:
If you have any further questions, feel free to contact us.
There is no way to check whether a customer was created from the back-end or front-end – all customers are equal from this point of view. However, you can add hidden attribute "created_by_admin" to user and fill it in only when creating a new user from the admin panel. Hence, you can sort out only needed users by checking this attribute.
@Mageworx Team, apologies for late follow-up, I seem to have missed your reply. Many thanks for the input!
We only need to know if the customer account was created in the frontend or backend when the original confirmation email is sent out, later it doesn't matter. I was thinking that, since there is a password section in the admin, with the "Send Auto-Generated Password" option, there would be some kind of function being run when sending these emails that checks for this
For our purposes, something like being able to check whether the "Send Auto-Generated Password" checkbox is ticked and then if so output something special, otherwise just standard content, would work pretty well.
Perhaps it's not possible, but if you know whether one can create a conditional based on whether this checkbox is ticked and then use it in the email template, that would be fantastic.
We need additional info to answer your question in details.
- What version of Magento do you use?
- Will you use this flag only in the registration confirmation email or somewhere else?
Hi again and thanks for the help!
- We are using 1.9.2.4
- We'll only use it in the registration email, as a way to be able to send somewhat different content to users that register on the frontend and users we add in the backend.
You need to create a small module that will add an attribute flag for a user created in the admin panel.
First, create a folder for the module.
> app/code/community/MageWorx/Detect
Then, create a configuration file `config.xml`:
> app/code/community/MageWorx/Detect/etc/config.xml
with the following content:
<?xml version="1.0"?> <!-- /** * Copyright © 2016 MageWorx. All rights reserved. * See LICENSE.txt for license details. */ --> <config> <modules> <MageWorx_Detect> <version>1.0.0</version> </MageWorx_Detect> </modules> <global> <models> <mageworx_detect> <class>MageWorx_Detect_Model</class> </mageworx_detect> </models> <blocks> <mageworx_detect> <class>MageWorx_Detect_Block</class> </mageworx_detect> </blocks> <helpers> <mageworx_detect> <class>MageWorx_Detect_Helper</class> </mageworx_detect> </helpers> <resources> <mageworx_detect_setup> <setup> <module>MageWorx_Detect</module> <class>Mage_Customer_Model_Entity_Setup</class> </setup> </mageworx_detect_setup> </resources> </global> <adminhtml> <events> <customer_save_before> <observers> <mageworx_detect> <class>mageworx_detect/observer</class> <method>setCustomerCreatedByAdmin</method> </mageworx_detect> </observers> </customer_save_before> </events> </adminhtml> </config>
Where:
1. MageWorx_Detect – a name of the module
2. mageworx_detect – an alias for the models, helper and blocks
We use the installer Mage_Customer_Model_Entity_Setup for correct work of the module. The mageworx_detect observer on the customer_save_before event in the adminhtml scope let us set a flag to 1. This allows us to check whether a user has been created by an administrator in the email template.
Then, create the default helper file where we save a name of an attribute in a constant:
> app/code/community/MageWorx/Detect/Helper/Data.php
<?php /** * Copyright © 2016 MageWorx. All rights reserved. * See LICENSE.txt for license details. */ class MageWorx_Detect_Helper_Data extends Mage_Core_Helper_Abstract { const CUSTOMER_DETECT_ATTRIBUTE_NAME = 'is_from_admin'; } ?>
Note. If you decide to use the name of the attribute that differs from `is_from_admin`, you will need to replace it in the helper and email template only. Its quite easier than looking for it in the code.
And the installation file (keep in mind the subfolder `mageworx_detect_setup`):
> app/code/community/MageWorx/Detect/sql/mageworx_detect_setup/mysql4-install-1.0.0.php
<?php /** * Copyright © 2016 MageWorx. All rights reserved. * See LICENSE.txt for license details. */ /** @var Mage_Customer_Model_Entity_Setup $installer */ $installer = $this; $installer->startSetup(); $setup = new Mage_Eav_Model_Entity_Setup('core_setup'); $entityTypeId = $setup->getEntityTypeId('customer'); $attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId); $attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId); $attributeName = MageWorx_Detect_Helper_Data::CUSTOMER_DETECT_ATTRIBUTE_NAME; $sortOrder = 200; $installer->removeAttribute('customer',$attributeName); $installer->addAttribute("customer", $attributeName, [ "type" => "varchar", "backend" => "", "label" => "Is customer created by admin", "input" => "hidden", "source" => "", "visible" => false, "required" => false, "default" => "0", "frontend" => "", "unique" => false, "note" => "Is customer created by admin", ]); $attribute = Mage::getSingleton("eav/config")->getAttribute("customer", $attributeName); $setup->addAttributeToGroup( $entityTypeId, $attributeSetId, $attributeGroupId, $attributeName, $sortOrder ); $used_in_forms = []; $used_in_forms[] = "adminhtml_customer"; $attribute->setData("used_in_forms", $used_in_forms) ->setData("is_used_for_customer_segment", true) ->setData("is_system", 0) ->setData("is_user_defined", 1) ->setData("is_visible", 0) ->setData("sort_order", 100); $attribute->save(); $installer->endSetup(); ?>
You should pay attention to the following in the installer:
Then, you need to create an observer file:
> app/code/community/MageWorx/Detect/Model/Observer.php
<?php /** * Copyright © 2016 MageWorx. All rights reserved. * See LICENSE.txt for license details. */ class MageWorx_Detect_Model_Observer { /** * @param $observer */ public function setCustomerCreatedByAdmin($observer) { /** @var Mage_Customer_Model_Customer $customer */ $customer = $observer->getCustomer(); if (!$customer) { return; } $customer->setData(MageWorx_Detect_Helper_Data::CUSTOMER_DETECT_ATTRIBUTE_NAME, '1'); return; } } ?>
It has only one simple method that modifies the value of the attribute to 1 before saving a customer in the admin panel. We don't need an additional check on the front-end because we are utilizing the adminhtml scope for the observer declaration in the configuration file (config.xml) and the default value is 0.
Note. If you want this to be performed only for the first save of a customer, you need to add the additional check whether a customer (`$customer` variable) has an `id`:
if ($customer->getId()) { return; }
To activate the module, add the following file:
> app/etc/modules/MageWorx_Detect.xml
<?xml version="1.0"?> <!-- /** * Copyright © 2016 MageWorx. All rights reserved. * See LICENSE.txt for license details. */ --> <config> <modules> <MageWorx_Detect> <active>true</active> <codePool>community</codePool> </MageWorx_Detect> </modules> </config>
Then, disable the compilation, refresh the cache and check the result.
The new attribute should be used in the following way in the template:
{{depend customer.is_from_admin}} <strong>Password:</strong> {{htmlescape var=$customer.password}} {{/depend}}
Where the depend directive responses for the check of the following attribute value. It should be 0 on the front-end in our case and the check shouldn't pass or 1 for the admin panel, the check should pass and the field `<strong>Password:</strong> {{htmlescape var=$customer.password}}` will be drawn in the email template.
Here are our results of the check:
We load two users where `$customer` is created in the admin panel and `$customer2` is created on the front-end. Let's check the value of our attribute:
The customers in the database:
Then, create the template using the rule above and add the `depend` directive for the `is_from_admin` attribute. Assign this template to all actions we need:
For the customer that is created in the admin panel:
Our template in the debug (we are using russian locale, the "Пароль" line is Password):
An email in the browser windows (preview doesn't work, we stopped the script and displayed the result instead of sending email):
For the customer that is created on the front-end:
Our template in the debug:
The email in the browser window:
If you have any further questions, feel free to contact us.
Sincere apologies, I never got to reply to this! I found it to be a phenomenal answer, very thorough and educational - I'm very appreciative for the time you took to explain the setup and present the code to get things running. It was very impressive to see how you engage with the community by offering this walkthrough reply!
We are now running Magento 1.9.3.2 and this works great, so again, many many thanks for your help with this!!